Link List
Link List
Linked list is a special type of data structure where all data elements are
linked to one another. Linked list is the collection of nodes and every nodes
contains two parts data part and address part.
Link list consists a series of structure. Each structure consists of a data field
and address field. Data field consists data part and the address field contains
the address of the successors.
Dynamic Data Structure: The size of linked list increase and decrease
during program execution.
No memory wastage: In linked list memory will be allocated at the time
of program execution so no memory wastage.
Easily insert and delete data: In linked list you can insert any data at
specific position and also delete any data from specific position.
Need more memory: For store data in linked list you need more memory
space, you need memory space for both data and address part.
Types of Linked List
In this type of liked list each node holds two-pointer field. Pointers exist
between adjacent nodes in both directions.The list can be traversed either
forward or backward.
In circular linked list the first and last node are adjacent.
Implementation
1.In c language, memory can be acquired through the malloc() and calloc()
function.
2.Memory acquired during run-time can be freed through the free() function.
1. node *nw;
2. nw=(node*)malloc(sizeof(node));
3. nw->data=A,B,C;
4. nw->next=100,200,Null;
1. #include<stdio.h>
2. #include<conio.h>
3. #include<alloc.h>
4. struct node
5. {
6. int no;
7. struct node *next;
8. };
9. struct node *first;
10.void creatlist()
11.{
12. char ch='y';
13. struct node *ptr,*nw;
14. while(ch!='n')
15. {
16. printf("\nEnter item in list");
17. nw=(struct node*)malloc(sizeof(struct node));
18. scanf("%d",&nw->no);
19. nw->next=0;
20. if(first==0)
21. {
22. first=nw;
23. }
24. else
25. {
26. for(ptr=first ;ptr->next!=0;ptr=ptr->next);
27. {
28. ptr->next=nw;
29. }
30. }
31. printf("\nDo you want to countinue y\n");
32. ch=getch();
33. }
34.}
35.void display()
36.{
37. struct node *ptr;
38. printf("Item int list are");
39. for(ptr=first;ptr!=0;ptr=ptr->next)
40. {
41. printf("\n%d",ptr->no);
42. }
43.}
44.void main()
45.{
46. clrscr();
47. first=0;
48. creatlist();
49. display();
50. getch();
51.}
Creating a linked list
1. #include<stdio.h>
2. #include<conio.h>
3. #include<alloc.h>
4. struct node
5. {
6. int data;
7. struct node * next;
8. };
9. void main()
10.{
11. struct node * nw, * head;
12. int i, n;
13. clrscr();
14. head = 0 ;
15. printf("Enter the size of list");
16. scanf("%d",&n);
17. for(i=0;i < n;i++)
18. {
19. printf("Enter the Element");
20. nw = (struct node *)malloc(sizeof(struct node));
21. scanf("%d",&(nw->data));
22. nw->next = head;
23. head = nw;
24. }
25. nw = head;
26. while(nw)
27. {
28. printf("%d\n", nw->data);
29. nw = nw->next ;
30. }
31.getch();
32.}
Algorithm
1. Step 1. Create a new node and assign the address to any node say ptr.
2. Step 2. OVERFLOW,IF(PTR = NULL)
3. write : OVERFLOW and EXIT.
4. Step 3. ASSIGN INFO[PTR] = ITEM
5. Step 4. IF(START = NULL)
6. ASSIGN NEXT[PTR] = NULL
7. ELSE
8. ASSIGN NEXT[PTR] = START
9. Step 5. ASSIGN START = PTR
10.Step 6. EXIT
Algorithm
1. InsertAtlocDll(info,next,start,end,loc,size)
2. 1.set nloc = loc-1 , n=1
3. 2.create a new node and address in assigned to ptr.
4. 3.check[overflow] if(ptr=NULL)
5. write:overflow and exit
6. 4.set Info[ptr]=item;
7. 5.if(start=NULL)
8. set next[ptr] = NULL
9. set start = ptr
10. else if(nloc<=size)
11. repeat steps a and b while(n != nloc)
12.
13. a. loc = next[loc]
14. b. n = n+1
15. [end while]
16. next[ptr] = next[loc]
17. next[loc] = ptr
18. else
19. set last = start;
20. repeat step (a) while(next[last]!= NULL)
21. a. last=next[last]
22. [end while]
23. last->next = ptr ;
24. [end if]
25.6.Exit.
1. DELETE AT BEG(INFO,NEXT,START)
2. 1.IF(START=NULL)
3. 2.ASSIGN PTR = STRAT
4. 3.ASSIGN TEMP = INFO[PTR]
5. 4.ASSIGN START = NEXT[PTR]
6. 5.FREE(PTR)
7. 6.RETURN(TEMP)
Algorithm for Deletion at Last Node of the Linked LIst
1. Delete End(info,next,start)
2. 1.if(start=NULL)
3. Print Underflow and Exit.
4. 2.if(next[start]==NULL)
5. Set ptr =start and start=NULL.
6. set temp = info[ptr].
7. else cptr = start and ptr = next[start].
8. Repeat steps(a) and (b) while(next[ptr]!=NULL)
9. (a) set cptr = ptr
10. (b) set ptr = next[ptr]
11. [end while]
12.set next[cptr] = NULL
13. temp = info[ptr]
14. [end if]
15.3. free(ptr)
16.4. return temp
17.5. exit
In this type of liked list each node holds two-pointer field. Pointers exist
between adjacent nodes in both directions.The list can be traversed either
forward or backward.
1. NEXT holds the memory location of the Previous Node in the List.
2. PREV holds the memory location of the Next Node in the List.
3. DATA holds Data.
1.Doubly Linked List are more convenient than Singly Linked List since we
maintain links for bi-directional traversing
2.We can traverse in both directions and display the contents in the whole List.
3.In Doubly Linked List we can traverse from Head to Tail as well as Tail to
Head.
4. Each Node contains two fields, called Links , that are references to the
previous and to the Next Node in the sequence of Nodes.
5. The previous link of the first node and the next link of the last node points to
NULL.
1. InsertAtBegDll(info,prev,next,start,end)
2. 1.create a new node and address in assigned to ptr.
3. 2.check[overflow] if(ptr=NULL)
4. write:overflow and exit
5. 3.set Info[ptr]=item;
6. 4.if(start=NULL)
7. set prev[ptr] = next[ptr] = NULL
8. set start = end = ptr
9. else
10. set prev[ptr] = NULL
11. next[ptr] = start
12. set prev[start] = ptr
13. set start = ptr
14. [end if]
15.5.Exit.
1. InsertAtlocDll(info,prev,next,start,end,loc,size)
2. 1.set nloc = loc-1 , n=1
3. 2.create a new node and address in assigned to ptr.
4. 3.check[overflow] if(ptr=NULL)
5. write:overflow and exit
6. 4.set Info[ptr]=item;
7. 5.if(start=NULL)
8. set prev[ptr] = next[ptr] = NULL
9. set start = end = ptr
10. else if(nloc<=size)
11. repeat steps a and b while(n != nloc)
12.
13. a. loc = next[loc]
14. b. n = n+1
15. [end while]
16. next[ptr] = next[loc]
17. prev[ptr] = loc
18. prev[next[loc]] = ptr
19. next[loc] = ptr
20. else
21. set prev[ptr] = end
22. next[end] = ptr
23. set ptr[next] = NULL
24. set end = ptr
25. [end if]
26.6.Exit.
1. InsertAtEndDll(info,prev,next,start,end)
2. 1.create a new node and address in assigned to ptr.
3. 2.check[overflow] if(ptr=NULL)
4. write:overflow and exit
5. 3.set Info[ptr]=item;
6. 4.if(start=NULL)
7. set prev[ptr] = next[ptr] = NULL
8. set start = end = ptr
9. else
10. set prev[ptr] = end
11. next[end] = ptr
12. set ptr[next] = NULL
13. set end = ptr
14. [end if]
15.5.Exit.
Circular list is a list in which the link field of the last node is made to point to
the start/first node of the list.
There are three situation for inserting element in Circular linked list.
1.Insertion at the front of Circular linked list.
2.Insertion in the middle of the Circular linked list.
3.Insertion at the end of the Circular linked list.
1. InsertAtlocDll(info,next,start,end,loc,size)
2. 1.set nloc = loc-1 , n=1
3. 2.create a new node and address in assigned to ptr.
4. 3.check[overflow] if(ptr=NULL)
5. write:overflow and exit
6. 4.set Info[ptr]=item;
7. 5.if(start=NULL)
8. set next[ptr] = NULL
9. set start = ptr
10. else if(nloc<=size)
11. repeat steps a and b while(n != nloc)
12.
13. a. loc = next[loc]
14. b. n = n+1
15. [end while]
16. next[ptr] = next[loc]
17. next[loc] = ptr
18. else
19. set last = start;
20. repeat step (a) while(next[last]!= NULL)
21. a. last=next[last]
22. [end while]
23. last->next = ptr ;
24. [end if]
25.6.Exit.
Insertion at the end of Circular linked list
Procedure for insertion a node at the end of list
Step1. Create the new node
Step2. Set the new node’s next to itself (circular!)
Step3. If the list is empty,return new node.
Step4. Set our new node’s next to the front.
Step5. Set tail’s next to our new node.
Step6. Return the end of the list.