链表:线性表的链式表示和实现
单链表的定义和表示
**特点:**用一组任意的存储单元(可以连续,也可以不连续)存储线性表的数据元素。
结点(node):①数据域:存储数据元素信息;②指针域:存储直接后继存储位置的域。【单链表中最后一个结点指针为null】
struct Node{
int val;
struct Node *next;
}
创建链表:
struct Node*creatList(){
struct Node *pHead=(struct Node*)malloc(sizeof(struct Node));
pHead->next=NULL;
return pHead;
}
创建结点:
struct Node*creatList(int data){
struct Node *newNode=(struct Node *)malloc(sizeof(struct Node));
newNode=data;
newNode->next=NULL;
}
链表打印:
void printList(struct Node *HeadList){
struct Node *pPrint=HeadList->next;
while(pPrint){
printf("%d",&pPrintf->val);
pPtint=pPrint->next;
}
printf("\n");
}
头插法:
void insertList(int data){
struct Node*newNode=creatList(data);
newNode->next=pHead->next;
pHead->next=newNode;
}
尾插法:
void insert(int data){
struct Node*newNode=creatList(data);
pHead->next=newNode;
newNode->next=NULL;
}