Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 12 LINKED LIST SRM-MCA.

Similar presentations


Presentation on theme: "CHAPTER 12 LINKED LIST SRM-MCA."— Presentation transcript:

1 CHAPTER 12 LINKED LIST SRM-MCA

2 Session Objectives Explain Data Structures Explain link list
Explain linked list declaration Traverse a linked list Insert nodes into a link list Types of linked list

3 Data structures syntax
A data structure is a group of data elements grouped together under one name. These data elements, known as members, can have different types and different lengths. syntax struct structure_name { member_type1 member_name1; member_type2 member_name2; member_type3 member_name3; } Stru_varname; Where structure_name is a name for the structure type, stru_name can be a set of valid identifiers for objects that have the type of this structure. Within braces { } there is a list with the data members, each one is specified with a type and a valid identifier as its name.

4 // program to Accept the employee details from the End user
#include<stdio.h> #include<conio.h> #include<malloc.h> struct emp { int eno; char ename[25]; int sal; }; void main() struct emp *e; char ch='y'; clrscr(); while(ch=='y')

5 { e=(struct emp *) malloc(sizeof(struct emp)); printf("\n Enter the employee number"); scanf("%d",&e->eno); printf("\n Enter the employee name"); scanf("%s",e->ename); printf("\n Enter the employee salary"); scanf("%d",&e->sal); printf("\n Do you wish to continue[Y/N]?"); fflush(stdin); scanf("%c",&ch); } getch();

6 LINKED LIST : It is a dynamic data structure. This implementation uses pointers. Each element of the list is called as node. Each element points to the next element. In linked list, a node can be inserted or deleted at any position. Basic Terms : (i) Node (ii) Null Pointer (iii) External pointer (iv) Empty List Operation on Linked list : (i) Creation (ii) Traversing (iii) Insertion (a) Insertion at beginning (b) Insertion at end (c) Insertion at any position (iv) Searching (v) Deletion (a) Deletion at beginning (b) Deletion at end (c) Deletion at any position

7 Linked List Declaration
A linked list storing information of players of a cricket team can be declared as : Note That next is declared as a pointer to the structure player itself. struct player { char name[30]; struct player *next; }; Remember, that the last node in the list should have its pointer, next, set to the value NULL.

8 Diagrammatically

9 // program to Add New Records in Single linked list using First to
// Last method simply defined as to form the new Datastructure #include<stdio.h> #include<conio.h> #include<malloc.h> void accept(); struct emp { int eno; char ename[25]; int sal; struct emp *next; }; // start - Ist Record, New1 - Current record, Temp - prevoius record struct emp *start=NULL,*new1=NULL,*temp=NULL,*new2,*dptr; void main() char ch='y'; int no,recno; clrscr(); while(ch=='y') new1=(struct emp *) malloc(sizeof(struct emp)); printf("\n Enter the employee number"); scanf("%d",&new1->eno); Continued…,

10 printf("\n Enter the employee name"); scanf("%s",new1->ename);
printf("\n Enter the employee salary"); scanf("%d",&new1->sal); if (start==NULL) // Give Ist record as Input,to enter the following if true { start=new1; //current record is assigned to start start->next=NULL; //current record next is set to NULL.because it is a // first record temp=new1; //current record is assigned to temp(point previous record) } else temp->next=new1; // new1 have a current record &assigned to prevoius next // have a NULL value changed to address temp=new1; //current i/p record is assigned to temp ex. 1st record to II temp->next=NULL; printf("\n Do you wish to continue[Y/N]?"); fflush(stdin); scanf("%c",&ch); Continued…,

11 printf("\n %d %s %d",new1->eno,new1->ename,new1->sal); }
// View ALL Records for(new1=start;new1!=NULL; new1=new1->next) // new1 initialize Ist record { printf("\n %d %s %d",new1->eno,new1->ename,new1->sal); } //Viewing particular records printf("\n Enter the employee number to be displayed"); scanf("%d",&no); if (no==new1->eno) // Insert the record at the middle position printf("\n ENter the Record No is to be inserted"); scanf("%d",&recno); accept(); for(new2=start;new2!=NULL;new2=new2->next) if(recno==start->eno) // to search if it is Ist record new1->next=start; start=new1; Continued…,

12 else if (recno==new2->eno && new2->next !=NULL) {
temp->next=new1; new1->next=new2; } // to check if it is last record &also to insert that place. else if (recno==new2->eno && new2->next==NULL) { new2->next=new1; new1->next=NULL; temp=new2; // display all records for(new2=start;new2!=NULL; new2=new2->next) // new2 initialize Ist record printf("\n %d %s %d",new2->eno,new2->ename,new2->sal); Continued…,

13 printf("\n Enter the record number to delete"); scanf("%d",&recno);
//Deletion process printf("\n Enter the record number to delete"); scanf("%d",&recno); for(new2=start;new2!=NULL;new2=new2->next) { if(recno==start->eno) start=start->next; } else if (recno==new2->eno && new2->next !=NULL) temp->next=new2->next; else if(recno==new2->eno && new2->next==NULL) {temp->next=NULL;} temp=new2; //display all the records for(new2=start;new2!=NULL; new2=new2->next) // new2 initialize Ist record printf("\n %d %s %d",new2->eno,new2->ename,new2->sal); Continued…,

14 printf("\n Enter the employee number to be modified");
// Modify process printf("\n Enter the employee number to be modified"); scanf("%d",&no); for(new1=start;new1!=NULL; new1=new1->next) //new1 init.Ist record & search { if (no==new1->eno) printf("\n Enter the Empno,Empname and salary"); scanf("\n %d %s %d",&new1->eno,new1->ename,&new1->sal); } //display all the records for(new1=start;new1!=NULL; new1=new1->next) // new1 initialize Ist record printf("\n %d %s %d",new1->eno,new1->ename,new1->sal); //Memory deallocating process new1=start; Continued…,

15 while(1){ if(new1==NULL) break; dptr=new1; new1=new1->next; free(dptr); } dptr=NULL; new1=NULL; temp=NULL; start=NULL; void accept() { new1=(struct emp *) malloc(sizeof(struct emp)); printf("\n Enter the employee number"); scanf("%d",&new1->eno); printf("\n Enter the employee name"); scanf("%s",new1->ename); printf("\n Enter the employee salary"); scanf("%d",&new1->sal);}}


Download ppt "CHAPTER 12 LINKED LIST SRM-MCA."

Similar presentations


Ads by Google