链表的插入,删除比较方便,在给定前驱节点的时候,时间复杂度为O(1)
查找比较麻烦,要根据头指针一个一个往下找,时间复杂度为O(n)
单链表
头插法
#include "iostream"
typedef struct LinkNode{
LinkNode *next;
int data;
} ;
LinkNode *linkNodeInsert(){
LinkNode *head = (LinkNode *) malloc(sizeof (LinkNode));
LinkNode *s;
head -> next = NULL;
for (int i = 1; i <= 5; i++){
s = (LinkNode *) malloc(sizeof (LinkNode));
s -> data = i;
s -> next = head -> next;
head -> next = s;
// printf("%d\n", s -> data);
}
// printf("%d\n", head -> next -> data);
return head;
}
void display(LinkNode *head){
while (head){
printf("%d", head -> next -> data);
head = head -> next;
}
}
int main(){
LinkNode *head;
head = linkNodeInsert();
display(head);
}