FRANCIS XAVIER ENGINEERING COLLEGE TIRUNELVELI-627003 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING 19CS3611 – DATA STRUCTURES LABORATORY 1 INDEX EX.NO. NAME OF THE EXPERIMENT PAGE MAR SIGN NO. K 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 2 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(sizeof(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; 3 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) 4 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); 5 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("Enter 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 6 { 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; } 7 Output: 8 Result: Thus the C program for implementing singly linked list is done and the output is verified successfully. 9 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; 10 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) { 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; } else 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; 11 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); } 12 Output: 13 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. After 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) 14 { case 1:addrecord(); 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) { head=tail=temp; temp->next=head; } else { 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 deleted \n"); scanf("%d",&elt); 15 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); } 16 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(); } 17 Output: 18 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. 19 Program: #include <conio.h> #include <stdio.h> #include <malloc.h> struct 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; 20 } void display (node *p) { node *t = p; while (t != NULL) { printf("+ %d", t -> coef); printf("X^ %d", t -> expo); t = t -> next; } } void add_poly(node *p1,node *p2) { node *newnode; while(1) { if( p1 == NULL || p2 == NULL ) break; if(p1->expo == p2->expo ) { printf("+ %d X ^%d",p1->coef+p2->coef,p1->expo); p1 = p1->next; p2 = p2->next; } else { if(p1->expo > p2->expo) { printf("+ %dX ^%d",p1->coef,p1->expo); p1 = p1->next; } else { printf(" + %dX ^%d",p2->coef,p2->expo); p2 = p2->next; } } } while(p1 != NULL ) { printf("+ %d X ^%d",p1->coef,p1->expo); p1 = p1->next; } while(p2 != NULL ) { printf("+ %d X ^%d",p2->coef,p2->expo); p2 = p2->next; 21 } } void main() { node *poly1 = NULL ,*poly2 = NULL,*poly3=NULL; clrscr(); printf("\nEnter First Polynomial..(in ascending-order of exponent)"); poly1 = create_poly (poly1); printf("\nEnter Second Polynomial..(in ascending-order of exponent)"); poly2 = create_poly (poly2); clrscr(); printf("\n Enter Polynomial 1: "); display (poly1); printf("\n Enter Polynomial 2: "); display (poly2); printf( "\n Resultant Polynomial : "); add_poly(poly1, poly2); display (poly3); getch(); } Output: Result: Thus a C Program for polynomial arithmetic is done and the output is verified successfully. 22 Ex.No 3a Array Implementation of Stack Aim: To write a C program to perform array implementation of stack. Algorithm: 1. Initialize the top pointer to be -1. 2. To perform push operation, get the input from the user and add the value to the stack by incrementing the value of top. 3. To perform the pop operation, the value is deleted by decrementing the top pointer. 4. Display the values in the stack. Program: #include <stdio.h> #include <conio.h> #define max 5 static int stack[max]; int top = -1; void push(int x) { stack[++top] = x; } int pop() { return (stack[top--]); } void view() { int i; if (top < 0) printf("\n Stack Empty \n"); else { printf("\n Top-->"); for(i=top; i>=0; i--) { printf("%4d", stack[i]); 23 } printf("\n"); } } void main() { int ch=0, val; clrscr(); while(ch != 4) { printf("\n STACK OPERATION \n"); printf("1.PUSH "); printf("2.POP "); printf("3.VIEW "); printf("4.QUIT \n"); printf("Enter Choice : "); scanf("%d", &ch); switch(ch) { case 1: if(top < max-1) { printf("\nEnter Stack element : "); scanf("%d", &val); push(val); } else printf("\n Stack Overflow \n"); break; case 2: if(top < 0) printf("\n Stack Underflow \n"); else { val = pop(); printf("\n Popped element is %d\n", val); } break; case 3: view(); break; case 4: exit(0); default: printf("\n Invalid Choice \n"); } 24 } } Output: 25 26 Result: Thus a C program for array implementation of stack is done and the output is verified successfully. Ex.No.3b Array implementation of Queue Aim: To write a C program for array implementation of queue. Algorithm: 1. Initialize the values of rear and front to be -1. 2. Insert operation is done by incrementing the value of rear and placing the value in the corresponding position. 3. Delete operation is done by incrementing the front pointer and deleting the value at that position. 4. Display the values present in the queue. Program: #include <stdio.h> #include <conio.h> #define max 5 static int queue[max]; int front = -1; int rear= -1; void insert(int x) { queue[++rear] = x; if (front == -1) front = 0; }int rem() { int val; val = queue[front]; if (front==rear && rear==max-1) front = rear = -1; else front ++; return (val); } void view() { int i; if (front == -1) printf("\n Queue Empty \n"); 27 else { printf("\n Front-->"); for(i=front; i<=rear; i++) printf("%4d", queue[i]); printf(" <--Rear\n"); } } void main() { int d,ch= 0,val; clrscr(); while(ch != 4) { printf("\n QUEUE OPERATION \n"); printf("1.INSERT "); printf("2.DELETE "); printf("3.VIEW "); printf("4.QUIT\n"); printf("Enter Choice : "); scanf("%d", &ch); switch(ch) { case 1: if(rear < max-1) { printf("\n Enter element to be inserted : "); scanf("%d", &val); insert(val); } else printf("\n Queue Full \n"); break; case 2: if(front == -1) printf("\n Queue Empty \n"); else { d=rem(); printf("\n Element deleted : %d \n",d); } break; case 3: view(); break; case 4: exit(0); default: printf("\n Invalid Choice \n"); 28 } } } Output: 29 30 Result: Thus a C program for array implementation of queue is done and the output is verified successfully. Ex.No.4a Linked List implementation of stack Aim: To write a C program to implement stack using linked list. Algorithm: 1. Initially create the header node and assign its next pointer to null. 2. For push operation, allocate memory for creating a temporary node and add the node to the next of header node. 3. For pop operation, delete the node next to the header and free the memory space. 4. Display the values present in the stack. Program: #include <stdio.h> #include <conio.h> #include <process.h> #include <alloc.h> struct node { int label; struct node *next; }; main() { int ch = 0; int k; struct node *h, *temp, *head; /* Head node construction */ head = (struct node*) malloc(sizeof(struct node)); head->next = NULL; while(1) { printf("\n Stack using Linked List \n"); printf("1->Push "); printf("2->Pop "); printf("3->View "); printf("4->Exit \n"); printf("Enter your choice : "); scanf("%d", &ch); switch(ch) { case 1: /* Create a new node */ temp=(struct node *)(malloc(sizeof(struct node))); printf("Enter label for new node : "); scanf("%d", &temp->label); 31 h = head; temp->next = h->next; h->next = temp; break; case 2: /* Delink the first node */ h = head->next; head->next = h->next; printf("Node %s deleted\n", h->label); free(h); break; case 3: printf("\n HEAD -> "); h = head; /* Loop till last node */ while(h->next != NULL) { h = h->next; printf("%d -> ",h->label); } printf("NULL \n"); break; case 4: exit(0); } } } Output: Result: 32 Thus a C program for implementing stack using linked list is done and the output is verified successfully. Ex.no.4b Linked list implementation of Queue Aim: To write a C program for implementing queue using linked list. Algorithm: 1. Initially create the header node and assign its next pointer to null. 2. For insert operation, allocate memory for the new node and insert the node next to the header. 3. For delete operation, delete the last node in the list and free the memory space.\ 4. Display the values available in the queue. Program: #include <stdio.h> #include <conio.h> #include <process.h> #include <alloc.h> struct node { int label; struct node *next; }; main() { int ch=0; int k; struct node *h, *temp, *head; /* Head node construction */ head = (struct node*) malloc(sizeof(struct node)); 33 head->next = NULL; while(1) { printf("\n Queue using Linked List \n"); printf("1->Insert "); printf("2->Delete "); printf("3->View "); printf("4->Exit \n"); printf("Enter your choice : "); scanf("%d", &ch); switch(ch) { case 1: /* Create a new node */ temp=(struct node *)(malloc(sizeof(struct node))); printf("Enter label for new node : "); scanf("%d", &temp->label); /* Reorganize the links */ h = head; while (h->next != NULL) h = h->next; h->next = temp; temp->next = NULL; break; case 2: /* Delink the first node */ h = head->next; head->next = h->next; printf("Node deleted \n"); free(h); break; case 3: printf("\n\nHEAD -> "); h=head; while (h->next!=NULL) { h = h->next; printf("%d -> ",h->label); } printf("NULL \n"); break; case 4: exit(0); } } } 34 Output: Result: Thus a C program for linked list implementation of stack is done and the output is verified successfully. Ex.No 5 Infix to postfix conversion using stack 35 Aim: To write a C program to convert infix to postfix expression using stack. Algorithm: 1. Scan the Infix Expression from left to right. 2. If the scanned character is an operand, append it with final Infix to Postfix string. 3. Else, a) If the precedence order of the scanned(incoming) operator is greater than the precedence order of the operator in the stack (or the stack is empty or the stack contains a ‘(‘ or ‘[‘ or ‘{‘), push it on stack. 4. b)Else, Pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing that Push the scanned operator to the stack. 5. 4. If the scanned character is an ‘(‘ or ‘[‘ or ‘{‘, push it to the stack. 6. If the scanned character is an ‘)’or ‘]’ or ‘}’, pop the stack and and output it until a ‘(‘ or ‘[‘ or ‘{‘ respectively is encountered, and discard both the parenthesis. 7. Repeat steps 2-6 until infix expression is scanned. 8. Print the output 9. Pop and output from the stack until it is not empty. Program: #include <stdio.h> #include <conio.h> #include <ctype.h> #define SIZE 50 char s[SIZE]; int top=-1; push(char elem) { s[++top]=elem; } char pop() { return(s[top--]); } int pr(char elem) { switch(elem) { case '#': return 0; case '(': return 1; case '+': case '-': return 2; case '*': case '/': return 3; } 36 } void main() { char infx[50],pofx[50],ch,elem; int i=0,k=0; clrscr(); printf("\n\nRead the Infix Expression ? "); scanf("%s",infx); push('#'); while( (ch=infx[i++]) != '\0') { if( ch == '(') push(ch); else if(isalnum(ch)) pofx[k++]=ch; else if( ch == ')') { while( s[top] != '(') pofx[k++]=pop(); elem=pop(); } else { while( pr(s[top]) >= pr(ch) ) pofx[k++]=pop(); push(ch); } } while( s[top] != '#') pofx[k++]=pop(); pofx[k]='\0'; printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n",infx,pofx); getch(); } Output: 37 Result: Thus a C program for converting infix to postfix is done using stack and the output is verified successfully. Ex.No.6 Implementation of binary trees and traversals Aim: To write a C program to implement binary trees and traversals. Algorithm: 1. Get the input from the user and create a binary tree such that the root is greater than the left child and less than the right child. 2. For inorder traversal a) Traverse the left subtree, i.e., call Inorder(left-subtree) b) Visit the root. c) Traverse the right subtree, i.e., call Inorder(right-subtree) 3. For preorder traversal a) Visit the root. b) Traverse the left subtree, i.e., call Preorder(left-subtree) c) Traverse the right subtree, i.e., call Preorder(right-subtree) 4. For postorder traversal a) Traverse the left subtree, i.e., call Postorder(left-subtree) b) Traverse the right subtree, i.e., call Postorder(right-subtree) 38 c) Visit the root. 5. Display the results. Program: #include<stdio.h> #include<conio.h> struct node { int data; struct node *right,*left; } *root,*p,*q; struct node *make(int y) { struct node *newnode; newnode=(struct node *)malloc(sizeof(struct node)); newnode->data=y; newnode->right=newnode->left=NULL; return(newnode); } void left(struct node *r,int x) { if(r->left!=NULL) printf("\n invalid!"); else r->left=make(x); } void right(struct node *r,int x) { if(r->right!=NULL) printf("\n invalid!"); else r->right=make(x); } void inorder(struct node *r) { if(r!=NULL) { inorder(r->left); printf("\t %d",r->data); inorder(r->right); }} void preorder(struct node *r) { if(r!=NULL) { printf("\t%d",r->data); 39 preorder(r->left); preorder(r->right); } } void postorder(struct node *r) { if(r!=NULL) { postorder(r->left); postorder(r->right); printf("\t%d",r->data); } } void main() { int no; int choice; char ch; clrscr(); printf("\n enter the root"); scanf("%d",&no); root=make(no); p=root; do { printf("Do you want to continue(y/n)"); scanf("%s",&ch); printf("\n enter another number:"); scanf("%d",&no); if(no==-1) break; p=root; q=root; while(no!=p->data && q!=NULL) { p=q; if(no<p->data) q=p->left; else q=p->right; } if(no<p->data) { printf("\n leftbranch of %d is%d",p->data,no); left(p,no); } 40
Enter the password to open this PDF file:
-
-
-
-
-
-
-
-
-
-
-
-