单链表删除整表_单链表删除
單鏈表刪除整表
Deletion can be at various positions like:
刪除可以在各個位置進行,例如:
Deleting the first node
刪除第一個節(jié)點
Deleting the last node
刪除最后一個節(jié)點
Deleting the intermediate node
刪除中間節(jié)點
刪除單個鏈表中的第一個節(jié)點 (Deleting the first node in single linked list)
In such case, the head node is to be removed and the next node needs to be assigned as updated head node.
在這種情況下,將刪除頭節(jié)點,并且需要將下一個節(jié)點分配為更新的頭節(jié)點。
Create a temporary node, say temp which points to the head node (first node) of the list.
創(chuàng)建一個臨時節(jié)點,例如temp ,它指向列表的頭節(jié)點(第一個節(jié)點)。
Move head node pointer to the immediate next node and delete (dispose) the temp node.
將頭節(jié)點指針移動到緊鄰的下一個節(jié)點,然后刪除(布置)臨時節(jié)點。
刪除單個鏈接列表中的最后一個節(jié)點 (Deleting the last node in the single linked list)
In such case the last node is to be removed from list. The steps are following:
在這種情況下,最后一個節(jié)點將從列表中刪除。 步驟如下:
We need to keep track of the previous node of last node. That's why while traversing to the last node we need to set a prev node also which will point to the previous node of the tail node( last node) after traversal.
我們需要跟蹤最后一個節(jié)點的前一個節(jié)點。 這就是為什么在遍歷到最后一個節(jié)點時,我們還需要設(shè)置一個prev節(jié)點,該節(jié)點將在遍歷后指向尾節(jié)點(最后一個節(jié)點)的前一個節(jié)點。
So, we have two pointer, one tail node pointing to the last node and another is prev node pointing to the previous node of the last node.
因此,我們有兩個指針,一個尾節(jié)點指向最后一個節(jié)點,另一個是上一個節(jié)點指向最后一個節(jié)點的前一個節(jié)點。
Set the next pointer of the prev node to NULL and delete the tail node. (last node)
將上一個節(jié)點為NULL的下一個指針,并刪除尾節(jié)點。 (最后一個節(jié)點)
刪除單個鏈接列表中的中間節(jié)點 (Deleting an intermediate node in the single linked list)
In such case an intermediate node is to be deleted. The approach is quite similar to the previous.
在這種情況下,中間節(jié)點將被刪除。 該方法與以前的方法非常相似。
Similar to the previous case, a curr node and a prev node is to be maintained. Curr node will point to the node to be deleted and prev node will point to the previous node.
與前一種情況類似,將保留curr節(jié)點和prev節(jié)點。 Curr節(jié)點將指向要刪除的節(jié)點,而prev節(jié)點將指向上一個節(jié)點。
Set the next pointer of the prev node to the next pointer of curr node.
在上一個節(jié)點的next指針設(shè)置為CURR節(jié)點的下一個指針。
Delete the curr node.
刪除curr節(jié)點。
鏈表中刪除的C實現(xiàn) (C implementation of deletion in a linked list)
#include <stdio.h> #include <stdlib.h>struct node{int data; // data fieldstruct node *next; };void traverse(struct node* head){struct node* current=head; // current node set to headint count=0; // to count total no of nodesprintf("\n traversing the list\n");while(current!=NULL){ //traverse until current node isn't NULLcount++; //increase node countprintf("%d ",current->data);current=current->next; // go to next node}printf("\ntotal no of nodes : %d\n",count); }struct node* creatnode(int d){struct node* temp=malloc(sizeof(struct node));temp->data=d;temp->next=NULL;return temp; }int main(){printf("creating the linked list by inserting new nodes at the begining\n");printf("enter 0 to stop building the list, else enter any integer\n");int k,count=1,x;struct node* curr,*temp,*prev;scanf("%d",&k);struct node* head=creatnode(k); //buliding list, first nodescanf("%d",&k);///inserting at begining//while(k){curr=creatnode(k);curr->next=head; //inserting each new node at the begininghead=curr;scanf("%d",&k);}traverse(head); // displaying the listdeleting the first nodeprintf("\ndeleting the first node.............\n");temp=head; // first node assigned to temphead=head->next; // head node is updatedfree(temp); // deleting the first nodetraverse(head); // displaying the listprintf("\nfirst node deleted...............\n");/deleting the last nodeprintf("\ndeleting the last node.............\n");temp=head->next;prev=head;while(temp->next!=NULL){temp=temp->next;prev=prev->next;} // after traversal temp points to the last //node and prev to the previous node of the last nodeprev->next=NULL;free(temp);traverse(head);printf("\last node deleted................\n");///deleting any intermediate node in the listprintf("\n enter the position of the node you want to delete\n");scanf("%d",&x);temp=head->next;prev=head;while(count!=x-1){temp=temp->next;prev=prev->next;count++;} // temp pointing to the node to be deleted and prev to the previous node prev->next=temp->next;free(temp);traverse(head);return 0;}Output
輸出量
creating the linked list by inserting new nodes at the begining enter 0 to stop building the list, else enter any integer 1 2 3 4 5 6 7 8 9 0 traversing the list 9 8 7 6 5 4 3 2 1 total no of nodes : 9 deleting the first node.............traversing the list 8 7 6 5 4 3 2 1 total no of nodes : 8 first node deleted............... deleting the last node............. traversing the list 8 7 6 5 4 3 2 total no of nodes : 7 last node deleted................ enter the position of the node you want to delete 5 traversing the list 8 7 6 5 3 2 total no of nodes : 6翻譯自: https://www.includehelp.com/data-structure-tutorial/single-linked-list-deletion.aspx
單鏈表刪除整表
總結(jié)
以上是生活随笔為你收集整理的单链表删除整表_单链表删除的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue ani_ANI的完整形式是什么?
- 下一篇: Java Scanner next()方