0% found this document useful (0 votes)
8 views16 pages

Link List

link list in data structure

Uploaded by

Pooja Goel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
8 views16 pages

Link List

link list in data structure

Uploaded by

Pooja Goel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

Linked 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.

Representation of link list

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.

Advantages of linked list

 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.

Dis-Advantages of linked list

 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

1. Singly Linked list


2. Doubly Linked list
3. Circular Linked list

Singly Linked List


In this type of linked list two nodes are linked with each other in sequential
linear manner. Movement in forward direction is possible.

Doubly 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.

Circular Linked List

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;

Implementation Of Linked list

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.}

Counting nodes in a list

1. int count(node nw)


2. {
3. int i;
4. i=0;
5. while(nw!=NULL)
6. {
7. i=i+1;
8. nw=nw->next;
9. }
10.}

Insertion In Linked list

There are three situation for inserting element in list.

1. Insertion at the front of list.


2. Insertion in the middle of the list.
3. Insertion at the end of the list.

Procedure For Inserting an element to linked list


Step-1: Get the value for NEW node to be added to the list and its position.
Step-2: Create a NEW, empty node by calling malloc(). If malloc() returns no
error then go to step-3 or else say "Memory shortage".
Step-3: insert the data value inside the NEW node's data field.
Step-4: Add this NEW node at the desired position (pointed by the "location")
in the LIST.
Step-5: Go to step-1 till you have more values to be added to the LIST.

Insertion at the front of list

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

Insertion Node in given location Linked List

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.

Deletion In Singly linked list


There are three situation for Deleting element in list.
1.Deletion at beginning of the list.
2.Deletion at the middle of the list.
3.Deletion at the end of the list.

Algorithm for Deletion at beginning of the list

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

Doubly 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.
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.

Some Points About Doubly Linked-List

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.

Insertion in doubly linked list


There are three situation for inserting element in list.
1.Insertion at the front of list.
2.Insertion in the middle of the list.
3.Insertion at the end of the list.

Insertion At Begining in doubly linked list


Algorithm

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.

Insertion At Location in doubly linked list


Algorithm

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.

Insertion At Last in doubly linked list


Algorithm

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 linked list

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.

Points to Circular Linked List


1.A circular linked list is a linked list in which the head element's previous
pointer points to the tail element and the tail element's next pointer points to the
head element.
2.A circularly linked list node looks exactly the same as a linear singly linked
list.
Insertion In Circular Linked 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.

Insertion at the front of Circular linked list


Procedure for insertion a node at the beginning 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.

Algorithm for Insertion at the front of Circular linked list

1. node* AddFront(node* tail, int num)


2. {
3. node *temp = (node*)malloc(sizeof(node));
4. temp->data = num;
5. temp->next = temp;
6. if (tail == NULL)
7. return temp;
8. temp->next = tail->next;
9. tail->next = temp;
10. return tail;
11.}

Algorithm for Insertion at location of 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.

Algorithm for Insertion at the End of Circular linked list

1. node* AddEnd(node* tail, int num)


2. {
3. node *temp = (node*)malloc(sizeof(node));
4. temp->data = num;
5. temp->next = temp;
6. if (tail == NULL)
7. return temp;
8. temp->next = tail->next;
9. tail->next = temp;
10. return temp;
11.}

You might also like