1 / 17

DEPARTMENT OF TECHNICAL EDUCATION ANDHRA PRADESH Name of the Staff Member : A Santhosh Kumar

DEPARTMENT OF TECHNICAL EDUCATION ANDHRA PRADESH Name of the Staff Member : A Santhosh Kumar Designation : Lecturer Branch : Computer Engineering Institute : VMR Polytechnic, Rampur, Warangal Semester : III Subject : Data Structures through ‘C’. Subject Code : 9CM305

Download Presentation

DEPARTMENT OF TECHNICAL EDUCATION ANDHRA PRADESH Name of the Staff Member : A Santhosh Kumar

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DEPARTMENT OF TECHNICAL EDUCATION ANDHRA PRADESH Name of the Staff Member : A Santhosh Kumar Designation : Lecturer Branch : Computer Engineering Institute : VMR Polytechnic, Rampur, Warangal Semester : III Subject : Data Structures through ‘C’. Subject Code : 9CM305 Topic : Linear Data Structures Duration : 150 min Sub Topic : Inserting on Single Linked List Teaching Aids to be used : Power point Presentation, Animations Revised by : Bapuji naik 9CM305.9 to11

  2. Recap In the last class we came to know - How a singly linked list looks? - Basic operation on a linked list - Program for creation of a singly Linked List 9CM305.9 to11

  3. Objectives: On the completion of this period, you would be able to know - Inserting elements in a singly linked list (SLL) 9CM305.9 to11

  4. Inserting a node into a SLL • Where can a new node be inserted in a linked list? • In how many ways you can insert a new node in a list? • May be - At the beginning - In the middle - At the end - At arbitrary place 9CM305.9 to11

  5. Inserting a node into a SLL • There are many ways you might want to insert a new node into a list: • As the new first element • As the new last element • Before a given node (specified by a reference) • After a given node • Before a given value • After a given value • All are possible, but differ in difficulty 9CM305.9 to11

  6. Inserting a node into a SLL • Overall there are 3 ways to insert a new node into a list: • At the beginning • At the last • In the arbitrary place after a node 9CM305.9 to11

  7. Inserting a new node in a singly linked list Program for Inserting nodes – Declaration part #include<stdio.h> #include<stdlib.h> typedef struct node { int info; struct node*link; } NODE; NODE *first = NULL; void front_insert (int ); void arbit_insert (int, int); void end_insert (int ); 9CM305.9 to11

  8. Inserting a new node in a singly linked list main program–part switch (ch) { case 1: front_insert (ele); break; case 2:printf(“enter the elemrnt after which to insert ”); scanf(“%d”,&pre_ele) arbit_insert (pre_ele,ele); break; case 3: end_insert (ele) break; } } while (ch!=4); exit( ); } main() { int ch, ele, pre_ele; do { printf ( “ \n enter the data element to be inserted “ ); scanf (“%d”, &ele); printf ( “\n Enter 1: to insert at the beginning” ); Printf( “\n2: in arbitrary 3: at the end & press 4: exit ” ); scanf (“%d”, &ch); 9CM305.9 to11

  9. 11 first 22 44 X 33 Inserting in a singly linked list Inserting a new node at the beginning temp 9CM305.9 to11

  10. Inserting in a singly linked list Function – Inserting a new node at the beginning void front_insert( int ele) { NODE * temp; temp = (NODE*) malloc(sizeof(NODE)); temp -> info = ele; temp ->link = first; first=temp; return; } 9CM305.9 to11

  11. node 55 header 44 77 22 Inserting after (animation) Find the node you want to insert after First,copy the link from the node that's already in the list Then, change the link in the node that's already in the list 9CM305.9 to11

  12. Inserting in a singly linked list Function – Inserting a new node at arbitrary void arbit_ insert(pre_ele,ele) int pre_ele, ele; { NODE *temp, *prev, *target; temp = (NODE*)(malloc(sizeof(NODE)); temp -> info = ele; if (first == NULL) {first = temp; first -> link =NULL; return;} target = first; while(target -> info!=prev_ele && target!= NULL) target=target -> link; if(target==NULL) { printf(“NODE is not found”); return;} else {temp -> link=target-> link; target -> link=temp;} return; } 9CM305.9 to11

  13. 11 first 44 22 33 Inserting in a singly linked list Inserting a new node at the end temp X X 9CM305.9 to11

  14. Inserting in a singly linked list Function – Inserting a new node at the end void end_insert(int ele) { NODE* temp*, end; temp = (NODE*)malloc(sizeof(NODE)); temp -> info=ele; if(first==NULL) {first=temp; first -> link =NULL; return;} end = first; while (end -> link !=NULL) end = end -> link ; end -> link=temp; temp -> link=NULL; return; } 9CM305.9 to11

  15. Summary We have understood in this class • In how many ways a new node can be inserted. • At the beginning • At the last • In the arbitrary place after a node • Programs for inserting a new node in a singly linked list 9CM305.9 to11

  16. Exercises • Write a C program to insert a new node before specified node in a singly linked list. • Create a linked list of 5 elements using C 9CM305.9 to11

  17. Frequently asked questions • Write a C function to insert a new node at the beginning in a singly linked list. • Explain with a program how a new node is inserted in a singly linked list. 9CM305.9 to11

More Related