Algorithm to insert a node at the beginning of single linked list:
Let headbe a pointerthat storesthe addressof firstnode
step1: Readthe value to insertedintolinkedlist
step2: create a newnode usingmallocfunction
step3: Assignthe value readtothe data fieldof newnode (newnode->data=value)
step4: setthe nextof newnode tohead (newnode->next=head)
step5: setthe headpointertonewnode (head=newnode)
Single linkedlistBefore insertionofnewnode
Single linkedlistAfterinsertionof newnode:
1. creationof newnode at address 600 and value to be insertedinto linkedlist= 60
2. Linking newnode to existinglinkedlist
Algorithm to insert a node at the end of single linked list:
Let headbe a pointerthat storesthe addressof firstnode
step1: Readthe value to insertedintolinkedlist
step2: create a newnode usingmallocfunction
step3: Assignthe value readtothe data fieldof newnode
step4: if there isno linkedlistthennewnode becomesfirstnode
step5: otherwise gotothe lastnode inlinkedlist
Step5.1: attach newnode tolastnode (lastnode ->next=newnode)
Step5.2: newnode->next=NULL
head
100 200
200 NULL10 20
newnode
60
20
head
600
100
100
100 200
200
60 10 20
NULL
Single linkedlistBefore insertionofnewnode
Single linkedlistAfterinsertionof newnode:
1. creationof newnode at address 600 and value to be insertedinto linkedlist= 60
2. Linking newnode to existinglinkedlist
Algorithm to insert a node at a given position of single linked list:
Let headbe a pointerthat storesthe addressof firstnode
step1: Readthe value to insertedintolinkedlistandthe positionatwhichnewnodehastobe inserted
step2: create a newnode usingmallocfunction
step3: Assignthe value readtothe data fieldof newnode
step4: store the node before the givenposition inapointerc
step5: newnode->next=c->next
step6: c->next= newnode
Single linkedlistBefore insertionofnewnode
Single linkedlistAfterinsertionof newnode:
1. creationof newnode at address 600 and value to be insertedinto linkedlist= 60 position=2
head
100 200
200 NULL10 20
newnode
60
20
head
100
100
200
200 600
600
10 20 60
head
NULL
100 200
200 NULL10 20
newnode
60
2. Linking newnode to existinglinkedlist
Algorithm to delete a node at the beginning of single linked list:
Let headbe a pointerthat storesthe addressof firstnode
step1: If there isno linkedlistthenprintLinkedlistisempty
step2: If linkedlist exists
step2.1: delete the firstnode
step2.2: secondnode becomesfirstnode
Single linkedlistBefore deletion
Single linkedlistAfterdeletion:
Algorithm to delete a node at the end of single linked list:
Let headbe a pointerthat storesthe addressof firstnode
step1: If there isno linkedlistthenprintLinkedlist isempty
step2: If linkedlistexists
step2.1: move tolast but one node andstore the addressof lastbut one node inpointerc
step2.2: store the addressof last node inpointerpand detele the lastnode (free(p))
step2.3: c->next= NULL
20
c
600
100
200
600
200
60
10 20
NULL
NULL
head
100 200
20010 20
head
200
100NULL20
NULL
Single linkedlistBefore deletion
Single linkedlistAfterdeletion:
Algorithm to delete a node at given position of single linked list:
Let headbe a pointerthat storesthe addressof firstnode
step1: If there isno linkedlistthenprintLinkedlistisempty
step2: If linkedlistexists
step2.1: Readthe positionatwhichthe node has to deleted
step2.2: Store the addressof node before the tobe deletednodeinpointerc
step2.3: c->next= c->next->next
step2.4: delete the node atgivenposition
Single linkedlistBefore deletion
Single linkedlistAfterdeletingthe node at position2 :
Algorithm to display the elements of single linked list
Let headbe a pointerthat storesthe addressof firstnode
step1: If there isno linkedlistthenprintLinkedlistisempty
step2: If linkedlistexistsstore the addressof firstnode inpointerc( c = head)
step2.1: printthe data fieldinc (printc->data)
step2.2 move c to store the address of nextnode ( c = c-> next)
step3: go to step2 until c is notequal to NULL
NULL
head
100 200
20010 20
head
100
100NULL10
NULL
NULL
C
100 200
20010 20 300
300
30 NULL
NULL
head
100 300
30010 30 NULL
Singlelinked list

Singlelinked list

  • 1.
    Algorithm to inserta node at the beginning of single linked list: Let headbe a pointerthat storesthe addressof firstnode step1: Readthe value to insertedintolinkedlist step2: create a newnode usingmallocfunction step3: Assignthe value readtothe data fieldof newnode (newnode->data=value) step4: setthe nextof newnode tohead (newnode->next=head) step5: setthe headpointertonewnode (head=newnode) Single linkedlistBefore insertionofnewnode Single linkedlistAfterinsertionof newnode: 1. creationof newnode at address 600 and value to be insertedinto linkedlist= 60 2. Linking newnode to existinglinkedlist Algorithm to insert a node at the end of single linked list: Let headbe a pointerthat storesthe addressof firstnode step1: Readthe value to insertedintolinkedlist step2: create a newnode usingmallocfunction step3: Assignthe value readtothe data fieldof newnode step4: if there isno linkedlistthennewnode becomesfirstnode step5: otherwise gotothe lastnode inlinkedlist Step5.1: attach newnode tolastnode (lastnode ->next=newnode) Step5.2: newnode->next=NULL head 100 200 200 NULL10 20 newnode 60 20 head 600 100 100 100 200 200 60 10 20 NULL
  • 2.
    Single linkedlistBefore insertionofnewnode SinglelinkedlistAfterinsertionof newnode: 1. creationof newnode at address 600 and value to be insertedinto linkedlist= 60 2. Linking newnode to existinglinkedlist Algorithm to insert a node at a given position of single linked list: Let headbe a pointerthat storesthe addressof firstnode step1: Readthe value to insertedintolinkedlistandthe positionatwhichnewnodehastobe inserted step2: create a newnode usingmallocfunction step3: Assignthe value readtothe data fieldof newnode step4: store the node before the givenposition inapointerc step5: newnode->next=c->next step6: c->next= newnode Single linkedlistBefore insertionofnewnode Single linkedlistAfterinsertionof newnode: 1. creationof newnode at address 600 and value to be insertedinto linkedlist= 60 position=2 head 100 200 200 NULL10 20 newnode 60 20 head 100 100 200 200 600 600 10 20 60 head NULL 100 200 200 NULL10 20 newnode 60
  • 3.
    2. Linking newnodeto existinglinkedlist Algorithm to delete a node at the beginning of single linked list: Let headbe a pointerthat storesthe addressof firstnode step1: If there isno linkedlistthenprintLinkedlistisempty step2: If linkedlist exists step2.1: delete the firstnode step2.2: secondnode becomesfirstnode Single linkedlistBefore deletion Single linkedlistAfterdeletion: Algorithm to delete a node at the end of single linked list: Let headbe a pointerthat storesthe addressof firstnode step1: If there isno linkedlistthenprintLinkedlist isempty step2: If linkedlistexists step2.1: move tolast but one node andstore the addressof lastbut one node inpointerc step2.2: store the addressof last node inpointerpand detele the lastnode (free(p)) step2.3: c->next= NULL 20 c 600 100 200 600 200 60 10 20 NULL NULL head 100 200 20010 20 head 200 100NULL20 NULL
  • 4.
    Single linkedlistBefore deletion SinglelinkedlistAfterdeletion: Algorithm to delete a node at given position of single linked list: Let headbe a pointerthat storesthe addressof firstnode step1: If there isno linkedlistthenprintLinkedlistisempty step2: If linkedlistexists step2.1: Readthe positionatwhichthe node has to deleted step2.2: Store the addressof node before the tobe deletednodeinpointerc step2.3: c->next= c->next->next step2.4: delete the node atgivenposition Single linkedlistBefore deletion Single linkedlistAfterdeletingthe node at position2 : Algorithm to display the elements of single linked list Let headbe a pointerthat storesthe addressof firstnode step1: If there isno linkedlistthenprintLinkedlistisempty step2: If linkedlistexistsstore the addressof firstnode inpointerc( c = head) step2.1: printthe data fieldinc (printc->data) step2.2 move c to store the address of nextnode ( c = c-> next) step3: go to step2 until c is notequal to NULL NULL head 100 200 20010 20 head 100 100NULL10 NULL NULL C 100 200 20010 20 300 300 30 NULL NULL head 100 300 30010 30 NULL