SRMMCA Session Objectives Explain Data Structures Explain link
SRM-MCA
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
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 structure_name { member_type 1 member_name 1; member_type 2 member_name 2; member_type 3 member_name 3; } 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.
// 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')
{ 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(); }
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
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. Remember, that the last node in the list should have its pointer, next, set to the value NULL.
Diagrammatically
// 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, New 1 - Current record, Temp - prevoius record struct emp *start=NULL, *new 1=NULL, *temp=NULL, *new 2, *dptr; void main() { char ch='y'; int no, recno; clrscr(); while(ch=='y') { new 1=(struct emp *) malloc(sizeof(struct emp)); printf("n Enter the employee number"); scanf("%d", &new 1 ->eno); Continued…,
printf("n Enter the employee name"); scanf("%s", new 1 ->ename); printf("n Enter the employee salary"); scanf("%d", &new 1 ->sal); if (start==NULL) // Give Ist record as Input, to enter the following if true { start=new 1; //current record is assigned to start->next=NULL; //current record next is set to NULL. because it is a // first record temp=new 1; //current record is assigned to temp(point previous record) } else { temp->next=new 1; // new 1 have a current record &assigned to prevoius next // have a NULL value changed to address temp=new 1; //current i/p record is assigned to temp ex. 1 st record to II temp->next=NULL; } printf("n Do you wish to continue[Y/N]? "); fflush(stdin); scanf("%c", &ch); Continued…, }
// View ALL Records for(new 1=start; new 1!=NULL; new 1=new 1 ->next) // new 1 initialize Ist record { printf("n %d %s %d", new 1 ->eno, new 1 ->ename, new 1 ->sal); } //Viewing particular records printf("n Enter the employee number to be displayed"); scanf("%d", &no); for(new 1=start; new 1!=NULL; new 1=new 1 ->next) // new 1 initialize Ist record { if (no==new 1 ->eno) { printf("n %d %s %d", new 1 ->eno, new 1 ->ename, new 1 ->sal); } } // Insert the record at the middle position printf("n ENter the Record No is to be inserted"); scanf("%d", &recno); accept(); for(new 2=start; new 2!=NULL; new 2=new 2 ->next) { if(recno==start->eno) // to search if it is Ist record { new 1 ->next=start; start=new 1; Continued…, }
else if (recno==new 2 ->eno && new 2 ->next !=NULL) { temp->next=new 1; new 1 ->next=new 2; } // to check if it is last record &also to insert that place. else if (recno==new 2 ->eno && new 2 ->next==NULL) { new 2 ->next=new 1; new 1 ->next=NULL; } temp=new 2; } // display all records for(new 2=start; new 2!=NULL; new 2=new 2 ->next) // new 2 initialize Ist record { printf("n %d %s %d", new 2 ->eno, new 2 ->ename, new 2 ->sal); } Continued…,
//Deletion process printf("n Enter the record number to delete"); scanf("%d", &recno); for(new 2=start; new 2!=NULL; new 2=new 2 ->next) { if(recno==start->eno) { start=start->next; } else if (recno==new 2 ->eno && new 2 ->next !=NULL) { temp->next=new 2 ->next; } else if(recno==new 2 ->eno && new 2 ->next==NULL) {temp->next=NULL; } temp=new 2; } //display all the records for(new 2=start; new 2!=NULL; new 2=new 2 ->next) // new 2 initialize Ist record { printf("n %d %s %d", new 2 ->eno, new 2 ->ename, new 2 ->sal); } Continued…,
// Modify process printf("n Enter the employee number to be modified"); scanf("%d", &no); for(new 1=start; new 1!=NULL; new 1=new 1 ->next) //new 1 init. Ist record & search { if (no==new 1 ->eno) { printf("n Enter the Empno, Empname and salary"); scanf("n %d %s %d", &new 1 ->eno, new 1 ->ename, &new 1 ->sal); } } //display all the records for(new 1=start; new 1!=NULL; new 1=new 1 ->next) // new 1 initialize Ist record { printf("n %d %s %d", new 1 ->eno, new 1 ->ename, new 1 ->sal); } //Memory deallocating process new 1=start; Continued…,
while(1){ if(new 1==NULL) break; dptr=new 1; new 1=new 1 ->next; free(dptr); } dptr=NULL; new 1=NULL; temp=NULL; start=NULL; } void accept() { new 1=(struct emp *) malloc(sizeof(struct emp)); printf("n Enter the employee number"); scanf("%d", &new 1 ->eno); printf("n Enter the employee name"); scanf("%s", new 1 ->ename); printf("n Enter the employee salary"); scanf("%d", &new 1 ->sal); }}
- Slides: 15