Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

18,316

Published on

Includes various functions that can be performed on single linked list with animation

Includes various functions that can be performed on single linked list with animation

Published in: Education
55 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Views
Total Views
18,316
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
155
31
Likes
55
Embeds 0
No embeds

No notes for slide

### Transcript

• 1.
• A list implemented by each item having a link to the next item.
• Head points to the first node.
• Last node points to NULL.
Linked List 10 20 30 10 NULL Node A Node B Node C Node D P
• 2.
• Linked List is a series of nodes.
• Each node consists of 2 parts viz. Data part & pointer part.
• Pointer part stores the address of next node.
Node DATA POINTER 10 NODE
• 3. Declarations
• p : always points to the first node
• q: used for traversing the list, initialised with p
• temp : used for refering the newly created node
• data: consists data of a node
• E.g: q->data
• num: Number entered by the user
• position: indicates the position of the node
• counter: counts the no of node
• 4.
• Declare Node struct for nodes
• data: int-type data in this example
• next: a pointer to the next node in the list
• struct node
• {
• int data; //data
• struct node *link; // pointer
• };
Structure
• If list is empty i.e. P==NULL then create a new node with data and set its pointer to NULL
• If list is not empty i.e. P!=NULL then
• Traverse till the last node
• create a node with data
• set its pointer to NULL
• 6. If list is empty i.e. P==NULL
• struct node *P=null;
• struct node *q=p;
P Q NULL
• 7. If list is empty i.e. P==NULL P Q NULL
• 8. If list is empty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node))
• 9. If list is empty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node)) P->data=num 10
• 10. If list is empty i.e. P==NULL P Q p=(struct node *) malloc(sizeof(struct node)) P->data=num 10 P->link=NULL NULL
• 11. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
• 12. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
• Traversal :
true
• 13. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
• Traversal :
false
• 14. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C P Q
• Creating a node :
• q->link=(struct node *) malloc(sizeof(struct node))
10 Node D NULL
• 15. DISPLAY
• If P==NULL then list is empty
• Else
• Traverse till the last node
• while(q!=NULL)
• {
• printf(&quot; %d: data=%d&quot;,i,q->data);
• }
• 16. If list is empty P Q NULL
• struct node *P=null;
• struct node *q=p;
• If(p==NULL)
• o/p : list is empty
• 17. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
• Traversal & output :
• while(q!=NULL)
• {
• printf(“%d ”,q->data);
• }
true o/p : 10 o/p : 20
• 18. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
• Traversal & output :
• while(q!=NULL)
• {
• printf(“%d ”,q->data);
• }
o/p : 10 o/p : 20 o/p : 30
• 19. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B NULL Node C P Q
• Traversal & output :
• while(q!=NULL)
• {
• printf(“%d ”,q->data);
• }
false o/p : 10 o/p : 20 o/p : 30
• 20. INSERT
• If position is 1 then create a node with data and set its pointer to first node i.e. q as it also points to first node.
• If position is after the first node then traverse till the node before the insertion position where the node is to be inserted. Save the link of previous node in a temp. Overwrite the
• 21. If position is 1 10 20 30 Node A Node B Node C NULL Q P
• 22. If position is 1 10 20 30 Node A Node B Node C NULL Q P p=(struct node *) malloc(sizeof(struct node));
• 23. If position is 1 10 20 30 Node A Node B Node C NULL Q P p=(struct node *) malloc(sizeof(struct node)); p->data=num 5
• 24. If position is 1 10 20 30 Node A Node B Node C NULL Q P p=(struct node *) malloc(sizeof(struct node)); p->data=num; 5 p->link=q;
• 25. If position is greater than 1 10 20 30 Node A Node B Node C NULL Q P Suppose a node is to be inserted at position 3.
• 26. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; }
• 27. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp
• 28. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp
• 29. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp Node BC q->link=(struct node *) malloc(sizeof(struct node));
• 30. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp Node BC q->link=(struct node *) malloc(sizeof(struct node)); 25 q->link->data=num;
• 31. 10 20 30 Node A Node B Node C NULL P Q for(i=1;i<=3-2;i++) { q=q->link; } temp Node BC q->link=(struct node *) malloc(sizeof(struct node)); 25 q->link->data=num; q->link->link=temp;
• 32. COUNT
• Initialise counter=0;
• Traverse till q!=NULL by q=q->link & simultaneously increment the counter with 1
• Return the value of counter
• 33. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
• Traversal & count :
• counter=0;
• while(q!=NULL)
• {
• counter++;
• }
• return counter;
true counter= 1 counter=2
• 34. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B Node C NULL P Q
• Traversal & count :
• counter=0;
• while(q!=NULL)
• {
• counter++;
• }
• return counter;
counter=1 counter=2 counter=3
• 35. If list is not empty i.e. p!=NULL 10 20 30 Node A Node B NULL Node C P Q
• Traversal & count :
• counter=0;
• while(q!=NULL)
• {
• counter++;
• }
• return counter;
false counter=3 counter=1 counter=2
• 36. REVERSE
• struct node *q=p;
• int i,k,l,no_of_nodes;
• no_of_nodes=k=l=count();
• for(i=1;i<=no_of_nodes;i++)
• {
• for(k=1;k<l;k++)
• {
• }
• printf(&quot;%d &quot;,q->data);
• q=p;
• l--;
• }
• Traverse till the no of nodes i.e till last node on first attempt.
• After first attempt keep traversing by reducing the traversal with 1 & display the output.
• 37. SEARCH
• num is the number to be searched
• position =1
• Traverse the list till the last node & keep comparing the values of list with num the moment it matches return the position which is incremented by everytime.
• 38. SEARCH
• Suppose 30 is to be searched.
• position=1;
• while(q!=NULL) {
• if(q->data==num)
• return(position);
• position++;
10 20 30 Node A Node B Node C P Q 40 NULL Node D position=1 position=2 position=3 position=4
• 39. SEARCH
• Suppose 30 is to be searched.
• position=1;
• while(q!=NULL) {
• if(q->data==num)
• return(position);
• position++;
10 20 30 Node A Node B Node C P Q 40 NULL Node D true position=1 position=2 position=3 position=4
• 40. REMOVE
• If position is 1
• Position is other than 1
• 41. If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q
• 42. If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q
• 43. If position is 1 10 20 30 10 NULL Node A Node B Node C Node D P Q
• 44. If position is 1 20 30 10 NULL Node B Node C Node D P Q
• free(q)
• 45.
• Suppose 3 node is to be removed, position=3
• for(i=1;i<=position-2;i++)
• {
• }
10 20 30 Node A Node B Node C P Q 40 NULL Node D
• 46.
• Suppose 3 node is to be removed, position=3
• for(i=1;i<=position-2;i++)
• {
• }
10 20 30 Node A Node B Node C P Q 40 NULL Node D
• 47.
• Suppose 3 node is to be removed, position=3
• for(i=1;i<=position-2;i++)
• {
• }