1 FRANCIS XAVIER ENGINEERING COLLEGE TIRUNELVELI - 627003 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 19CS3611 – DATA STRUCTURES LABORATORY 2 INDEX EX.NO. NAME OF THE EXPERIMENT PAGE NO. MAR K SIGN 1a Singly Linked List 04 1b Doubly Linked List 11 1c Circular Linked List 15 2 Polynomial Arithmetic 20 3a Array Implementation of Stack 24 3b Array implementation of Queue 28 4a Linked List implementation of stack 32 4b Linked list implementation of Queue 34 5 Infix to postfix conversion using stack 36 6 Implementation of binary trees and traversals 39 7 AVL tree 44 8 Heaps using Priority Queues 50 9 Hashing 57 10a Insertion sort 59 10b Binary search 62 11 Evaluation of a postfix expression using a Stack 64 12 Dijikstra’s Algorithm 66 3 Ex No.1a Singly Linked List Aim: To write a C program to implement singly linked list. Algorithm: 1. For insert operation, allocate memory for the new node, add the number given as input by the user and add the node at the end of the list. 2. For the delete operation, delete the node with the value given as input by the user. 3. Display all the values present in the list. 4. Display the size which denotes the total number of elements in the list. Program: #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }*head; void append(int num) { struct node *temp,*right; temp= (struct node *)malloc(siz eof(struct node)); temp - >data=num; right=(struct node *)head; while(right - >next != NULL) right=right - >next; right - >next =temp; right=temp; right - >next=NULL; } void add( int num ) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp - >data=num; 4 if (head== NULL) { head=temp; head - >next=NULL; } else { temp - >next=head; head=temp; } } void addafter(int num, int loc) { int i; struct node *temp,*left,*right; right=head; for(i=1;i<loc;i++) { left=right; right=right - >next; } temp=(struct node *)malloc(sizeof(struct node)); temp - >data=num; left - >next=temp; left=temp; left - >next=right; return; } void insert(int num) { int c=0; struct node *temp; temp=head; if(temp==NULL) { add(num); } else { while(temp!=NULL) { if(temp - >data<num) c++; temp=temp - >next; } if(c==0) 5 add(num); else if(c<count()) addafter(num,++c); else append(num); } } int delete(int num) { struct node *temp, *prev; temp=head; while(temp!=NULL) { if(temp - >data==num) { if(temp==head) { head=temp - >next; free(temp); return 1; } else { prev - >next=temp - >next; free(temp); return 1; } } else { prev=temp; temp= temp - >next; } } return 0; } void display(struct node *r) { r=head; if(r==NULL) { return; } while(r!=NULL) { printf("%d ",r - >data); 6 r=r - >next; } printf(" \ n"); } int count() { struct node *n; int c=0; n=head; while(n!=NULL) { n=n - >next; c++; } return c; } int main() { int i,num; struct node *n; head=NULL; while(1) { printf(" \ nList Operations \ n"); printf("=============== \ n"); printf("1.Insert \ n"); printf("2.Display \ n"); printf("3.Size \ n"); printf("4.Delete \ n"); printf("5.Exit \ n"); printf("Enter your choice : "); if(scanf("%d",&i)<=0){ printf("Ente r only an Integer \ n"); exit(0); } else { switch(i) { case 1: printf("Enter the number to insert : "); scanf("%d",&num); insert(num); break; case 2: if(head==NULL) { printf("List is Empty \ n"); } else 7 { printf("Element(s) in the list are : "); } display(n); break; case 3: printf("Size of the list is %d \ n",count()); break; case 4: if(head==NULL) printf("List is Empty \ n"); else{ printf("Enter the number to delete : "); scanf("%d",&num); if(delete(num)) printf("%d deleted successfully \ n",num); else printf("%d not found in the list \ n",num); } break; case 5: return 0; default: printf("Invalid option \ n"); } } } return 0; } 8 Output: 9 Result: Thus the C program for implementing singly linked list is done and the output is verified successfully. 10 Ex No 1b Doubly Linked List Aim: To write a C program to implement doubly linked list. Algorithm: 1. For insert operation, allocate memory for the node by the adding the value given as input by the user in the position specified. 2. For deletion, get the value as input from the user and delete the corresponding node in the list. 3. Search an element from the list and display the position of the node. 4. Display all the elements in the list. Program: #include<stdio.h> #include<stdlib.h> struct Node; typedef struct Node * PtrToNode; typedef PtrToNode List; typedef PtrToNode Position; struct Node { int e; Position previous; Position next; }; void Insert(int x, List l, Position p) { Position TmpCell; TmpCell = (struct Node*) malloc(sizeof(struct Node)); if(TmpCell == NULL) printf("Memory out of space \ n"); else { TmpCell - >e = x; TmpCell - >previous = p; TmpCell - >next = p - >next; 11 p - >next = TmpCell; } } int isLast(Position p) { return (p - >next == NULL); } Position Find(int x, List l) { Position p = l - >next; while(p != NULL && p - >e != x) p = p - >next; return p; } void Delete(int x, List l) { Position p, p1, p2; p = Find(x, l); if(p != NULL) { } else } p1 = p - > previous; p2 = p - > next; p1 - > next = p - > next; if(p2 != NULL) // if the node is not the last node p2 - > previous = p - > previous; printf("Element does not exist!!! \ n"); void Display(List l) { Position p = l - >next; printf("The list element are :: "); while(p != NULL) { printf("%d - > ", p - >e); p = p - >next; } } void main() { int x, pos, ch, i; List l, l1; List p = l; l = (struct Node *) malloc(sizeof(struct Node)); l - >previous = NULL; 12 l - >next = NULL; printf("DOUBLY LINKED LIST IMPLEMENTATION OF LIST ADT \ n \ n"); do { :: "); printf(" \ n \ n1. INSERT \ t 2. DELETE \ t 3. FIND \ t 4. PRINT \ t 5. QUIT \ n \ nEnter the choice scanf("%d", &ch); switch(ch) { case 1: p = l; printf("Enter the element to be inserted :: "); scanf("%d",&x); printf("Enter the position of the element :: "); scanf("%d",&pos); for(i = 1; i < pos; i++) { p = p - >next; } Insert(x,l,p); break; case 2: p = l; printf("Enter the element to be deleted :: "); scanf("%d",&x); Delete(x,p); break; case 3: p = l; printf("Enter the element to be searched :: "); scanf("%d",&x); p = Find(x,p); if(p == NULL) printf("Element does not exist!!! \ n"); else printf("Element exist!!! \ n"); break; case 4: Display(l); break; } } while(ch<5); } 13 Output: 14 Result: Thus a C program for implementing doubly linked list is done and the output is verified successfully. Ex No 1c Circular Linked List Aim: To write a C program to implement circular linked list. Algorithm: 1. Get the input to add records from the user. 2. Afte r completing the insert operation, make the next pointer of the last node point to the header node. 3. Delete operation is done by getting input from the user and deleting the node with the corresponding value. 4. Display the results. Program: #include<stdio.h> #include<alloc.h> #include<conio.h> struct node { int data; struct node *next; }; struct node *head=NULL; struct node *tail=NULL; void main() { void addrecord(); void deleterecord(); void disrecord(); int ch; clrscr(); do { printf(" \ n 1. To add records \ n"); printf(" \ n 2. To delete a records \ n"); printf(" \ n 3. To view the records \ n"); printf(" \ n 4. To exit \ n"); printf(" \ n Enter your choice \ n"); scanf("%d",&ch); fflush(stdin); switch(ch) 15 { case 1:a ddrecord(); break; case 2:deleterecord(); break; case 3: disrecord(); break; case 4:exit(0); } } while (ch!=4); } void addrecord() { int new_data; char ans='y'; struct node *ptr,*prev,*temp; clrscr(); while (ans=='y') { temp=(struct node*)malloc(sizeof(struct node)); printf(" \ n Enter the new element: \ n"); scanf("%d",&new_data); fflush(stdin); temp - >data=new_data; temp - >next=NULL; if (head==NULL) { } else { } head=tail=temp; temp - >next=head; tail - > next=temp; tail=temp; printf(" \ n Would you like to enter another data(y \ \ n): \ n"); ans = getchar(); fflush(stdin); } } void deleterecord() { struct node *ptr,*prev,*delnode; int elt; printf(" \ n Enter the enrollment number to be delet ed \ n"); scanf("%d",&elt); 16 fflush(stdin); if (head==NULL) { printf(" \ n No elements in the list \ n"); return; } else { if (head - >data==elt) { delnode=head; if (head==tail) head=tail=NULL; else { head=head - >next; tail - >next=head; } } else if (tail - >data==elt) { for(ptr=head;(ptr!=tail);prev=ptr,ptr=ptr - >next); delnode=tail; tail=prev; tail - >next=head; } else { for(prev=ptr=head;(ptr - >data!=elt)&&(ptr!=tail); prev=ptr,ptr=ptr - >next); if(ptr - >data==elt) { delnode=ptr; prev - >next=ptr - >next; printf("yes..."); } else { printf("Given element not found in the list"); getch(); return; } } } free(delnode); } 17 void disrecord() { struct node *ptr,*prev=NULL; if (head==NULL) { printf(" \ n No records to view \ n"); return; } printf(" \ n The elements in the circular list are \ n"); for (ptr=head;prev!=tail;prev=ptr,ptr=ptr - >next) printf(" \ n \ n %d",ptr - >data); printf(" NULL \ n \ n "); getch(); } 18 Output: 19 Result: Thus a C program for circular linked list is done and the output is verified successfully. Ex.No.2 Polynomial Arithmetic Aim: To write a C program to perform polynomial arithmetic. Algorithm: 1. Get the values of exponent and coefficient as input from the user for the two polynomials. 2. Add the coefficients in which the exponents match. 3. Display the results. 20 Program: #include <conio.h> #include <stdio.h> #include <malloc.h> s truct link { int coef; int expo; struct link *next; }; typedef struct link node; node * getnode() { node *tmp; tmp =(node *) malloc( sizeof(node) ); printf(" \ n Enter Coefficient : "); fflush(stdin); scanf("%d",&tmp - >coef); printf(" \ n Enter Exponent : "); fflush(stdin); scanf("%d",&tmp - >expo); tmp - >next = NULL; return tmp; } node * create_poly (node *p ) { char ch; node *temp,*newnode; while( 1 ) { printf (" \ n Do U Want polynomial node (y/n): "); ch = getche(); if(ch == 'n') break; newnode = getnode(); if( p == NULL ) p= newnode; else { temp = p; while(temp - >next != NULL ) temp = temp - >next; temp - >next = newnode; } } return p;