链表之打基础–基本操作(必会)
前言:没有系统学过链表的同学过来看看,看看链表的操作,主要是单链表,因为我们以后刷力扣的题目,也主要是单链表的题目,所以,它对我们至关重要哦;如果学过的小伙伴,可以复习复习,毕竟温故而知新,可以为所欲为(bushi)
基本操作:
1.动态申请一个节点
2.单链表打印
3.单链表尾插
4.单链表的头插
5.单链表的尾删
6.单链表头删
7.单链表查找
8.单链表在pos位置之后插入x
9.单链表的销毁
开始了哦
1.动态申请一个节点
SListNode*BuySListNode(SLTDateType x){
SListNode* newnode =(SListNode*)malloc(sizeof(SListNode));if(newnode ==NULL){exit(-1);}
newnode->data = x;return newnode;}
2.单链表打印
voidSListPrint(SListNode* plist){if(plist ==NULL){printf("NULL\n");return;}else{while(plist){printf("%d->", plist->data);
plist = plist->next;}printf("NULL\n");}}
3.单链表尾插
voidSListPushBack(SListNode** pplist, SLTDateType x){
SListNode* tail =*pplist;
SListNode* newnode =BuySListNode(x);
newnode->next =NULL;if(tail ==NULL){*pplist = newnode;}else{while(tail->next){
tail = tail->next;}
tail->next = newnode;}}
4.单链表的头插
voidSListPushFront(SListNode** pplist, SLTDateType x){
SListNode* newnode =BuySListNode(x);
newnode->next =*pplist;*pplist = newnode;}
5.单链表的尾删
voidSListPopBack(SListNode** pplist){assert(*pplist);
SListNode* tail =*pplist;
SListNode* Pretail =NULL;if(tail->next ==NULL){*pplist =NULL;return;}else{while(tail->next){
Pretail = tail;
tail = tail->next;}free(tail);
tail =NULL;
Pretail->next =NULL;}}
6.单链表头删
voidSListPopFront(SListNode** pplist){assert(*pplist);
SListNode* front =*pplist;*pplist = front->next;free(front);
front =NULL;}
7.单链表查找
SListNode*SListFind(SListNode* plist, SLTDateType x){assert(plist);
SListNode* pos = plist;while(pos && pos->data != x){
pos = pos->next;}return pos;}
8.单链表在pos位置之后插入x
voidSListInsertAfter(SListNode* pos, SLTDateType x){assert(pos);
SListNode* newnode =BuySListNode(x);
newnode->next = pos->next;
pos->next = newnode;}
9.单链表的销毁
voidSListDestory(SListNode** pplist){
SListNode* node =*pplist;
SListNode* PreNode =NULL;while(node){
PreNode = node->next;free(node);
node = PreNode;}}
好啦,以上就是链表的基础操作啦,希望大家的数据结构更上一层楼!
ps:今天第一次,开车开出外地,一开好几个小时,200多公里的路程,回到家里好累好累,但也很奇怪,开车从不晕车,但不开车就晕车,why?!,一个男生晕车很丢人的啊,怎么搞的啊!!!!
版权归原作者 张遇桥 所有, 如有侵权,请联系我们删除。