Вы находитесь на странице: 1из 131

S.

NO

Name of Program

Date

Teachers Signature

PROGRAM-1:
// // // // // // // main.c DSLAB1 Created by Vipul Verma on 25/08/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include <stdio.h> typedef struct node { int data; struct node *next; }node; node *create(node *); void display(node *); node *insert(node *p,int pos); void search(node *,int n); node *inserte(node *p,int n); node *delete(node *p,int pos); node *deletee(node *p,int n); node* alldel(node *p); int main(int argc, const char * argv[]) { node *head=NULL; int choice,n,pos,elem; printf("1.Create a list\n2.Insert element before/after a particular position\n3.Insert element before/after a particular number4.Delete element at a particular position\n5.Delete element\n6.Search for a number\n7.Delete entire list\n8.Display list\n9.Exit"); printf("\nEnter your choice:"); scanf("%d",&choice); while(choice!=9) { switch(choice) { case 1: printf("\nEnter the elements you want to enter:"); scanf("%d",&n); while(n--) { head=create(head); } break; case 2: printf("\nEnter the postion:"); scanf("%d",&pos); head=insert(head,pos); break; case 3: printf("\nEnter the elem:"); scanf("%d",&elem);

head=inserte(head,elem); break; case 4: printf("\nEnter the postion:"); scanf("%d",&pos); head=delete(head,pos); break; case 5: printf("\nEnter the elem:"); scanf("%d",&elem); head=deletee(head,elem); break; case 6: printf("\nEnter the elem:"); scanf("%d",&elem); search(head,elem); break; case 7: head=alldel(head); break; case 8: display(head); break; case 9: exit(0); default: printf("Wrong choice entered please try again"); } printf("1.Create a list\n2.Insert element before/after a particular position\n3.Insert element before/after a particular number4.Delete element at a particular position\n5.Delete element\n6.Search for a number\n7.Delete entire list\n8.Display list\n9.Exit"); printf("\nEnter your choice:"); scanf("%d",&choice); } return 0; } node *create(node *p) { node *temp,*r; int dat; if(p==NULL) { temp=(node *)malloc(sizeof(node)); scanf("%d",&dat); temp->data=dat; temp->next=NULL; p=temp; } else{ temp=p; while(temp->next!=NULL) {

temp=temp->next; } r=(node *)malloc(sizeof(node)); scanf("%d",&dat); r->data=dat; r->next=NULL; temp->next=r; } return p; } void display(node *p) { printf("\nList is:"); while(p!=NULL) { printf(" %d ",p->data); p=p->next; } printf("\n"); } node *insert(node *p,int pos) { node *temp=p; int dat,choice; printf("\n1.Insertion before postion\n2.Insertion after position(by default)"); printf("\nEnter ur choice:"); scanf("%d",&choice); if(choice==2) { for(int i=0;i<pos;i++) { temp=temp->next; if(temp==NULL) { printf("\nThere are less elements in this list than the desired pos"); return p; } } node *r=(node *)malloc(sizeof(node)); scanf("%d",&dat); r->data=dat; r->next=temp->next; temp->next=r; } else{ for(int i=0;i<pos-1;i++) { temp=temp->next; if(temp==NULL) { printf("\nThere are less elements in this list than the desired pos"); return p; } } node *r=(node *)malloc(sizeof(node));

scanf("%d",&dat); r->data=dat; r->next=temp->next; temp->next=r; } return p; } node *inserte(node *p,int n) { node *temp=p,*q; int choice,dat; node *r=(node *)malloc(sizeof(node)); printf("\nEnter the element you want to enter:"); scanf("%d",&dat); r->data=dat; printf("\n1.Before element\n2.After element:"); scanf("%d",&choice); if(choice==1) { while(temp!=NULL && temp->data!=n) { q=temp; temp=temp->next; } if(temp!=NULL) { if(temp==p) { r->next=temp; p=r; return p; } else { r->next=temp; q->next=r; return p; } } } else { while(temp!=NULL && temp->data!=n) { temp=temp->next; } if(temp!=NULL) { r->next=temp->next; temp->next=r; return p; } } return p;

} node *delete(node *p,int pos) { node *temp,*q; int n=0,choice; temp=p; printf("\n1.Delete before postion\n2.Delete after position"); scanf("%d",&choice); if(choice==2) { for(int i=0;i<pos;i++,n++) { q=temp; temp=temp->next; if(temp==NULL) { printf("\nLess element in list than the desired position"); return p; } } } else{ for(int i=0;i<pos-1;i++,n++) { q=temp; temp=temp->next; if(temp==NULL) { printf("\nLess element in list than the desired position"); return p; } } } q->next=temp->next; free(temp); return p; } node *deletee(node *p,int n) { node *temp=p,*q; int choice; printf("\n1.Deletion before element\n2.Deletion after element:"); scanf("%d",&choice); if(choice==1) { if(temp==NULL) { printf("\nEmpty list"); return p; } else { while(temp!=NULL && temp->next->data!=n) { q=temp;

temp=temp->next; } if(temp==p) { printf("\nNo element present before it"); return p; } else { temp->next=temp->next->next; free(temp->next); return p; } } } else { if(temp==NULL) { printf("\nEmpty list"); return p; } else { while(temp!=NULL && temp->data!=n) { temp=temp->next; } if(temp==NULL) { printf("\nDeletion not possible"); return p; } else { temp->next=temp->next->next; free(temp->next); return p; } } } return p; } void search(node *p,int n) { int count=1; while(p!=NULL) { if(p->data==n) { printf("\n%d found at %d",n,count); } p=p->next; count++;

} if(p==NULL) { printf("\nElement not found\n"); } } node* alldel(node *p) { node *temp=p,*next; while(temp!=NULL) { next=temp->next; free(temp); temp=next; } p=NULL; return p; } 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:1 Enter the elements you want to enter:5 55 44 22 77 88 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:8 List is: 55 44 22 77 88 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:2 Enter the postion:2

1.Insertion before postion 2.Insertion after position(by default) Enter ur choice:1 66 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:8 List is: 55 44 66 22 77 88 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:2 Enter the postion:2 1.Insertion before postion 2.Insertion after position(by default) Enter ur choice:2 100 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:8 List is: 55 44 66 100 22 77 88 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit

Enter your choice:3 Enter the elem:66 Enter the element you want to enter:88 1.Before element 2.After element:1 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:8 List is: 55 44 88 66 100 22 77 88 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:3 Enter the elem:66 Enter the element you want to enter:99 1.Before element 2.After element:2 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:8 List is: 55 44 88 66 99 100 22 77 88 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number

7.Delete entire list 8.Display list 9.Exit Enter your choice:5 Enter the elem:66 1.Deletion before element 2.Deletion after element:1 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:8 List is: 55 44 88 99 100 22 77 88 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:8 List is: 55 44 88 99 100 22 77 88 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:5 Enter the elem:44

1.Deletion before element 2.Deletion after element:2 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:8 List is: 55 44 99 100 22 77 88 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:6 Enter the elem:99 99 found at 3 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:1 Enter the elements you want to enter:5 54321 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:7 1.Create a list 2.Insert element before/after a particular position

3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:8 List is: 1.Create a list 2.Insert element before/after a particular position 3.Insert element before/after a particular number4.Delete element at a particular position 5.Delete element 6.Search for a number 7.Delete entire list 8.Display list 9.Exit Enter your choice:9

PROGRAM-2
#include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node *next; struct Node *prev; }node;

void insertatend(node *pointer,int data) { while(pointer->next != NULL) pointer = pointer->next; pointer->next = (node *)malloc(sizeof(node)); pointer = pointer->next;

pointer->prev=pointer; pointer->data = data; pointer->next = NULL; } void insertbelem(node *pointer,int data,int a) { if(pointer) { while(pointer->next != NULL && pointer->next->data != a) pointer = pointer->next; if(pointer->data == NULL) printf("Number cannot be inserted"); else { node *temp; temp = (node *)malloc(sizeof(node)); temp->data = data; temp->prev = pointer; temp->next = pointer->next; pointer->next = temp; pointer = pointer->next; pointer->prev = temp; } } } void insertaelem(node *pointer,int data,int a) { if(pointer) { pointer = pointer->next; while(pointer != NULL && pointer->data != a) pointer = pointer->next; if(pointer->next == NULL) printf("Number cannot be inserted"); else { node *temp; temp = (node *)malloc(sizeof(node)); temp->data = data; temp->prev = pointer; temp->next = pointer->next; pointer->next = temp; pointer = pointer->next; pointer->prev = temp; } } } void insertbpos(node *pointer,int data,int pos) { int count=1; while(count != pos) { pointer = pointer->next;

count++; } node *temp; temp = (node *)malloc(sizeof(node)); temp->data = data; temp->prev = pointer; temp->next = pointer->next; pointer->next = temp; pointer = pointer->next; pointer->prev = temp; } void insertapos(node *pointer,int data,int pos) { int count=1; while(count != pos + 1) { pointer = pointer->next; count++; } node *temp; temp = (node *)malloc(sizeof(node)); temp->data = data; temp->prev = pointer; temp->next = pointer->next; pointer->next = temp; pointer = pointer->next; pointer->prev = temp; } void find(node *pointer,int data) { while(pointer->next != NULL && (pointer->next)->data != data) pointer = pointer->next; if(pointer->next == NULL) { printf("\nElement not found"); } else if((pointer->next)->data == data) { printf("\nElement found"); } } void printandcount(node *pointer) { int count=0; printf("\nThe list is : "); pointer = pointer->next; while(pointer != NULL) { printf("%d ",pointer->data); pointer = pointer->next; count++; } printf("\nTotal elements are : %d",count);

} void delete(node *pointer,int data) { while(pointer->next != NULL && (pointer->next)->data != data) pointer = pointer->next; if(pointer->next == NULL) { printf("\nElement not found"); } else { node *temp; temp = pointer->next; pointer->next = temp->next; temp->prev=pointer; free(temp); printf("\nElement Deleted"); } } void deletebelem(node *pointer,int data) { while(pointer->next != NULL && pointer->next->next->data != data) pointer = pointer->next; if(pointer->next == NULL) { printf("\nElement not found"); } else { node *temp; temp = pointer->next; pointer->next = temp->next; temp->prev=pointer; free(temp); printf("\nElement Deleted"); } } void deleteaelem(node *pointer,int data) { while(pointer->next != NULL && pointer->data != data) pointer = pointer->next; if(pointer->next == NULL) { printf("\nElement not found"); } else { node *temp; temp = pointer->next; pointer->next = temp->next; temp->prev=pointer;

free(temp); printf("\nElement Deleted"); } }

void deletebpos(node *pointer,int pos) { int count = 1; while(count != pos-1 && pointer->next != NULL) { pointer = pointer->next; count++; } if(pointer->next == NULL) { printf("\nElement not found"); } else { node *temp; temp = pointer->next; pointer->next = temp->next; temp->prev=pointer; free(temp); printf("\nElement Deleted"); } } void deleteapos(node *pointer,int pos) { int count = 1; while(count != pos + 1 && pointer->next != NULL) { pointer = pointer->next; count++; } if(pointer->next == NULL) { printf("\nElement not found"); } else if(count == pos + 1) { node *temp; temp = pointer->next; pointer->next = temp->next; temp->prev=pointer; free(temp); printf("\nElement Deleted"); } else printf("\nCannot be Deleted"); }

void search(node *pointer,int data) { while(pointer->next != NULL && (pointer->next)->data != data) pointer = pointer->next; if(pointer->next == NULL) { printf("\nElement not found"); } else if((pointer->next)->data == data) { printf("\nElement found"); } } int deletelist(node *pointer) { while(pointer->next) { node *temp; temp = pointer->next; free(pointer); pointer = temp; } printf("List Deleted"); return NULL; } int main(void) { node *start,*temp; start = (node *)malloc(sizeof(node)); temp = start; temp->prev=NULL; temp->next = NULL; int ch,data,pos,a; insertatend(start,1); insertatend(start,2); insertatend(start,3); insertatend(start,4); insertatend(start,5); insertatend(start,6); printandcount(start); printf("\n1.Insert"); printf("\n2.Delete"); printf("\n3.Print & Count"); printf("\n4.Find"); printf("\n5.Delete Entire List"); printf("\n6.Exit"); label: printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch)

{ case 1: printf("\nEnter the element to be inserted :"); scanf("%d",&data); printf("\n1.Insert before an element"); printf("\n2.Insert after an element"); printf("\n3.Insert before the position"); printf("\n4.Insert after the position"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the element before which the element is to be added : "); scanf("%d",&a); insertbelem(start,data,a); break; case 2: printf("\nEnter the element after which the element is to be added : "); scanf("%d",&a); insertaelem(start,data,a); break; case 3: printf("\nEnter the position before which the element is to be added : "); scanf("%d",&pos); insertbpos(start,data,pos); break; case 4: printf("\nEnter the position after which the element is to be added : "); scanf("%d",&pos); insertapos(start,data,pos); break; } break; case 2: printf("\n1.Delete before an element"); printf("\n2.Delete after an element"); printf("\n3.Delete before the position"); printf("\n4.Delete after the position"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the element before which the element is to be deleted : "); scanf("%d",&a); deletebelem(start,a); break; case 2: printf("\nEnter the element after which the element is to be deleted : "); scanf("%d",&a); deleteaelem(start,a); break; case 3: printf("\nEnter the position before which the element is to be deleted : "); scanf("%d",&pos); deletebpos(start,pos); break;

case 4: printf("\nEnter the position after which the element is to be deleted : "); scanf("%d",&pos); deleteapos(start,pos); break; } break; case 3: printandcount(start); break; case 4: printf("\nEnter the element to be find :"); scanf("%d",&data); search(start,data); break; case 5: start = deletelist(start); break; case 6: exit(0); break; } goto label; return 0; } The list is : 1 2 3 4 5 6 Total elements are : 6 1.Insert 2.Delete 3.Print & Count 4.Find 5.Delete Entire List 6.Exit Enter your choice:1 Enter the element to be inserted :55 1.Insert before an element 2.Insert after an element 3.Insert before the position 4.Insert after the position Enter your choice:1 Enter the element before which the element is to be added : 2 Enter your choice:3 The list is : 1 55 2 3 4 5 6 Total elements are : 7 Enter your choice:1 Enter the element to be inserted :66 1.Insert before an element 2.Insert after an element 3.Insert before the position 4.Insert after the position Enter your choice:2

Enter the element after which the element is to be added : 2 Enter your choice:3 The list is : 1 55 2 66 3 4 5 6 Total elements are : 8 Enter your choice:1 Enter the element to be inserted :77 1.Insert before an element 2.Insert after an element 3.Insert before the position 4.Insert after the position Enter your choice:3 Enter the position before which the element is to be added : 3 Enter your choice:3 The list is : 1 55 77 2 66 3 4 5 6 Total elements are : 9 Enter your choice:1 Enter the element to be inserted :88 1.Insert before an element 2.Insert after an element 3.Insert before the position 4.Insert after the position Enter your choice:4 Enter the position after which the element is to be added : 3 Enter your choice:3 The list is : 1 55 77 88 2 66 3 4 5 6 Total elements are : 10 Enter your choice:2 1.Delete before an element 2.Delete after an element 3.Delete before the position 4.Delete after the position Enter your choice:1 Enter the element before which the element is to be deleted : 77 Element Deleted Enter your choice:3 The list is : 1 77 88 2 66 3 4 5 6 Total elements are : 9 Enter your choice:2

1.Delete before an element 2.Delete after an element 3.Delete before the position 4.Delete after the position Enter your choice:2 Enter the element after which the element is to be deleted : 55 Element not found Enter your choice:3 The list is : 1 77 88 2 66 3 4 5 6 Total elements are : 9 Enter your choice:2 1.Delete before an element 2.Delete after an element 3.Delete before the position 4.Delete after the position Enter your choice:3 Enter the position before which the element is to be deleted : 3 Element Deleted Enter your choice:3 The list is : 1 88 2 66 3 4 5 6 Total elements are : 8 Enter your choice:2 1.Delete before an element 2.Delete after an element 3.Delete before the position 4.Delete after the position Enter your choice:4 Enter the position after which the element is to be deleted : 4 Element Deleted Enter your choice:3 The list is : 1 88 2 66 4 5 6 Total elements are : 7 Enter your choice:4 Enter the element to be find :88 Element found Enter your choice:5 List Deleted Enter your choice:3

PROGRAM-2 Continued
#include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node *next; }node; void insertatend(node *pointer,int data) { node *temp,*current; current = pointer; temp = (node *)malloc(sizeof(node)); temp->data = data; temp->next = pointer; while(current->next != pointer) current = current->next; current->next = temp; }

void insertbelem(node *pointer,int data,int a) { if(pointer) { while(pointer->next != NULL && pointer->next->data != a) pointer = pointer->next; if(pointer->data == NULL) printf("Number cannot be inserted"); else { node *temp; temp=(node *)malloc(sizeof(node)); temp->next=pointer->next; temp->data=data; pointer->next=temp; } } } void insertaelem(node *pointer,int data,int a) {

if(pointer) { pointer = pointer->next; while(pointer != NULL && pointer->data != a) pointer = pointer->next; if(pointer->next == NULL) printf("Number cannot be inserted"); else { node *temp; temp=(node *)malloc(sizeof(node)); temp->next=pointer->next; temp->data=data; pointer->next=temp; } } } void insertbpos(node *pointer,int data,int pos) { int count=1; while(count != pos) { pointer = pointer->next; count++; } node *temp; temp=(node *)malloc(sizeof(node)); temp->next=pointer->next; temp->data=data; pointer->next=temp; } void insertapos(node *pointer,int data,int pos) { int count=1; while(count != pos + 1) { pointer = pointer->next; count++; } node *temp; temp=(node *)malloc(sizeof(node)); temp->next=pointer->next; temp->data=data; pointer->next=temp; }

void search(node *pointer,int data) { node *start = pointer; while(pointer->next != start && (pointer->next)->data != data) pointer = pointer->next; if(pointer->next == start)

{ printf("\nElement not Found"); } else if((pointer->next)->data == data) { printf("\nElement Found"); } } void deletebelem(node *pointer,int data) { node *start = pointer; while(pointer->next != start && pointer->next->next->data != data) pointer = pointer->next; if(pointer->next == start) { printf("\nElement not found"); } else { node *temp; temp = pointer->next; pointer->next = temp->next; free(temp); printf("\nElement deleted"); } } void deleteaelem(node *pointer,int data) { node *start = pointer; while(pointer->next != start && pointer->data != data) pointer = pointer->next; if(pointer->next == start) { printf("\nElement not found"); } else { node *temp; temp = pointer->next; pointer->next = temp->next; free(temp); printf("\nElement deleted"); } }

void deletebpos(node *pointer,int pos) { int count = 1; node *start = pointer; while(count != pos-1 && pointer->next != start) {

pointer = pointer->next; count++; } if(pointer->next == start) { printf("\nElement not found"); } else { node *temp; temp = pointer->next; pointer->next = temp->next; free(temp); printf("\nElement deleted"); } } void deleteapos(node *pointer,int pos) { int count = 1; node *start = pointer; while(count != pos + 1 && pointer->next != start) { pointer = pointer->next; count++; } if(pointer->next == start) { printf("\nElement not found"); } else { node *temp; temp = pointer->next; pointer->next = temp->next; free(temp); printf("\nElement deleted"); } } void printandcount(node *start,node *pointer) { int count=0; printf("\nThe list is : "); while(pointer != start) { printf("%d ",pointer->data); pointer = pointer->next; count++; } printf("\nTotal elements are : %d",count); } node *deletelist(node *pointer) {

node *start = pointer; while(pointer->next != start) { node *temp; temp = pointer->next; free(pointer); pointer = temp; } printf("List Deleted"); return start; } int main(void) { node *start,*temp; start = (node *)malloc(sizeof(node)); temp = start; temp->next = start; int ch,data,pos,a; insertatend(start,1); insertatend(start,2); insertatend(start,3); insertatend(start,4); insertatend(start,5); insertatend(start,6); printandcount(start,start->next); printf("\n1.Insert"); printf("\n2.Delete"); printf("\n3.Print & Count"); printf("\n4.Find"); printf("\n5.Delete Entire List"); printf("\n6.Exit"); label: printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the element to be inserted :"); scanf("%d",&data); printf("\n1.Insert before an element"); printf("\n2.Insert after an element"); printf("\n3.Insert before the position"); printf("\n4.Insert after the position"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the element before which the element is to be added : "); scanf("%d",&a); insertbelem(start,data,a); break;

case 2: printf("\nEnter the element after which the element is to be added : "); scanf("%d",&a); insertaelem(start,data,a); break; case 3: printf("\nEnter the position before which the element is to be added : "); scanf("%d",&pos); insertbpos(start,data,pos); break; case 4: printf("\nEnter the position after which the element is to be added : "); scanf("%d",&pos); insertapos(start,data,pos); break; } break; case 2: printf("\n1.Delete before an element"); printf("\n2.Delete after an element"); printf("\n3.Delete before the position"); printf("\n4.Delete after the position"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the element before which the element is to be deleted : "); scanf("%d",&a); deletebelem(start,a); break; case 2: printf("\nEnter the element after which the element is to be deleted : "); scanf("%d",&a); deleteaelem(start,a); break; case 3: printf("\nEnter the position before which the element is to be deleted : "); scanf("%d",&pos); deletebpos(start,pos); break; case 4: printf("\nEnter the position after which the element is to be deleted : "); scanf("%d",&pos); deleteapos(start,pos); break; } break; case 3: printandcount(start,start->next); break; case 4: printf("\nEnter the element to be find :"); scanf("%d",&data); search(start,data); break; case 5: start = deletelist(start); break; case 6: exit(0);

break; } goto label; return 0; } The list is : 1 2 3 4 5 6 Total elements are : 6 1.Insert 2.Delete 3.Print & Count 4.Find 5.Delete Entire List 6.Exit Enter your choice:1 Enter the element to be inserted :22 1.Insert before an element 2.Insert after an element 3.Insert before the position 4.Insert after the position Enter your choice:1 Enter the element before which the element is to be added : 2 Enter your choice:3 The list is : 1 22 2 3 4 5 6 Total elements are : 7 Enter your choice:1 Enter the element to be inserted :33 1.Insert before an element 2.Insert after an element 3.Insert before the position 4.Insert after the position Enter your choice:2 Enter the element after which the element is to be added : 2 Enter your choice:3 The list is : 1 22 2 33 3 4 5 6 Total elements are : 8 Enter your choice:1 Enter the element to be inserted :44 1.Insert before an element 2.Insert after an element 3.Insert before the position 4.Insert after the position

Enter your choice:3 Enter the position before which the element is to be added : 3 Enter your choice:3 The list is : 1 22 44 2 33 3 4 5 6 Total elements are : 9 Enter your choice:1 Enter the element to be inserted :55 1.Insert before an element 2.Insert after an element 3.Insert before the position 4.Insert after the position Enter your choice:4 Enter the position after which the element is to be added : 4 Enter your choice:3 The list is : 1 22 44 2 55 33 3 4 5 6 Total elements are : 10 Enter your choice:2 1.Delete before an element 2.Delete after an element 3.Delete before the position 4.Delete after the position Enter your choice:1 Enter the element before which the element is to be deleted : 2 Element deleted Enter your choice:3 The list is : 1 22 2 55 33 3 4 5 6 Total elements are : 9 Enter your choice:2 1.Delete before an element 2.Delete after an element 3.Delete before the position 4.Delete after the position Enter your choice:2 Enter the element after which the element is to be deleted : 3 Element deleted Enter your choice:3 The list is : 1 22 2 55 33 3 5 6 Total elements are : 8 Enter your choice:2

1.Delete before an element 2.Delete after an element 3.Delete before the position 4.Delete after the position Enter your choice:3 Enter the position before which the element is to be deleted : 3 Element deleted Enter your choice:3 The list is : 1 2 55 33 3 5 6 Total elements are : 7 Enter your choice:2 1.Delete before an element 2.Delete after an element 3.Delete before the position 4.Delete after the position Enter your choice:4 Enter the position after which the element is to be deleted : 3 Element deleted Enter your choice:3 The list is : 1 2 55 3 5 6 Total elements are : 6 Enter your choice:4 Enter the element to be find :55 Element Found Enter your choice:5 List Deleted

PROGRAM-3 // // // // // // //

main.c DSLAB3 Created by Vipul Verma on 25/08/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include <stdio.h>

#define MAX 15 int insert(int *,int,int,int); int delete(int *,int,int); void search(int *,int,int); void reverse(int *,int); void display(int *,int); int n; int main(int argc, const char * argv[]) { int choice,a[MAX],i,pos,elem; printf("\nCreate array\n"); printf("Enter the number of elements you want to enter:\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nEnter element %d:",i+1); scanf("%d",&a[i]); } printf("\nMENU \n1.Insert\n2.Delete\n3.Search\n4.Reverse\n5.Display\n6.Exit\n"); printf("\nEnter your choice:"); scanf("%d",&choice); while(choice!=6) { switch(choice) { case 1: printf("\nEnter the position and element:"); scanf("%d %d",&pos,&elem); n=insert(a,pos,elem,n); break; case 2: printf("\nEnter the position:"); scanf("%d",&pos); n=delete(a,pos,n); break; case 3: printf("\nEnter the element:"); scanf("%d",&elem); search(a,elem,n); break; case 4: reverse(a,n); break; case 5: display(a,n); break; case 6: exit(0); default: printf("\nWrongh choice entered please try again"); break; } printf("\nMENU \n1.Insert\n2.Delete\n3.Search\n4.Reverse\n5.Display\n6.Exit\n");

printf("\nEnter your choice:"); scanf("%d",&choice); } return 0; } int insert(int *a,int pos,int elem,int n) { int i; for(i=n;i>=pos;i--) { a[i]=a[i-1]; } a[i]=elem; n++; return n; } int delete(int *a,int pos,int n) { int i; for(i=pos;i<n;i++) { a[i-1]=a[i]; } n--; return n; } void search(int *a,int elem,int n) { int i; for(i=0;i<n;i++) { if(a[i]==elem) { printf("\nThe element %d found at %d:",elem,i+1); } } } void display(int *a,int n) { int i; for(i=0;i<n;i++) { printf("\nElement %d is %d",i+1,a[i]); } printf("\n"); } void reverse(int *a,int n) { int i,temp; for(i=0;i<(n/2);i++) { temp=a[i]; a[i]=a[n-1-i]; a[n-1-i]=temp;

} }

Create array Enter the number of elements you want to enter: 5 Enter element 1:6 Enter element 2:5 Enter element 3:4 Enter element 4:3 Enter element 5:2 MENU 1.Insert 2.Delete 3.Search 4.Reverse 5.Display 6.Exit Enter your choice:1 Enter the position and element:6 3 MENU 1.Insert 2.Delete 3.Search 4.Reverse 5.Display 6.Exit Enter your choice:5 Element 1 is 6 Element 2 is 5 Element 3 is 4 Element 4 is 3 Element 5 is 2 Element 6 is 3 MENU 1.Insert 2.Delete 3.Search 4.Reverse 5.Display 6.Exit

Enter your choice:2 Enter the position:3 MENU 1.Insert 2.Delete 3.Search 4.Reverse 5.Display 6.Exit Enter your choice:5 Element 1 is 6 Element 2 is 5 Element 3 is 3 Element 4 is 2 Element 5 is 3 MENU 1.Insert 2.Delete 3.Search 4.Reverse 5.Display 6.Exit Enter your choice:3 Enter the element:3 The element 3 found at 3: The element 3 found at 5: MENU 1.Insert 2.Delete 3.Search 4.Reverse 5.Display 6.Exit Enter your choice:4 MENU 1.Insert 2.Delete 3.Search 4.Reverse 5.Display 6.Exit Enter your choice:5

Element 1 is 3 Element 2 is 2 Element 3 is 3 Element 4 is 5 Element 5 is 6 MENU 1.Insert 2.Delete 3.Search 4.Reverse 5.Display 6.Exit Enter your choice:6

PROGRAM-4
#include<stdio.h> #define MAX 10 struct term { int coeff; int exp; }; struct poly { struct term t[10]; int totalterms; }; void initpoly(struct poly*); void polycreate(struct poly*,int c,int e); struct poly mulpoly(struct poly,struct poly); struct poly subpoly(struct poly,struct poly); struct poly addpoly(struct poly,struct poly); void display(struct poly); int main()

{ struct poly p1,p2,p3; int n1,n2,c,e; initpoly(&p1); initpoly(&p2); initpoly(&p3); int ch; printf("\n1.Create\n2.Addition\n3.Subtraction\n4.Multiplication\n5.Exit\n"); printf("\nPlease enter ur choice:"); scanf("%d",&ch); while(ch!=5) { switch(ch) { case 1: printf("\nEnter number of terms to be entered in polynomial 1 and 2:"); scanf("%d%d",&n1,&n2); while(n1--) { printf("\nEnter coefficient and element:"); scanf("%d%d",&c,&e); polycreate(&p1,c,e); } while(n2--) { printf("\nEnter coefficient and element:"); scanf("%d%d",&c,&e); polycreate(&p2,c,e); } break; case 2: p3=addpoly(p1,p2); display(p3); break; case 3: p3=subpoly(p1,p2); display(p3); break; case 4: p3=mulpoly(p1,p2); display(p3); break; case 5: exit(0); default: printf("\nPlease try again"); } printf("\n1.Create\n2.Addition\n3.Subtraction\n4.Multiplication\n5.Exit\n"); printf("\nPlease enter ur choice:"); scanf("%d",&ch); } return 0; } /* Initializes elements of struct poly */ void initpoly(struct poly *p)

{ int i; p->totalterms=0; for(i=0;i<MAX;i++) { p->t[i].coeff=0; p->t[i].exp=0; } } /* Add the term of polynomial to the array t */ void polycreate(struct poly *p,int c,int e) { p->t[p->totalterms].coeff=c; p->t[p->totalterms].exp=e; (p->totalterms)++; } /* DISPLAY the polynomial equation */ void display(struct poly p) { int flag=0,i; for(i=0;i<p.totalterms;i++) { if(p.t[i].exp != 0) printf("%d x^%d + ",p.t[i].coeff,p.t[i].exp); else { printf("%d",p.t[i].coeff); flag=1; } } if(!flag) printf("\b\b"); }

/* ADD two polynomials p1 and p2 */ struct poly addpoly(struct poly p1,struct poly p2) { int i,j,c; struct poly p3; initpoly(&p3); if(p1.totalterms>p2.totalterms) c=p1.totalterms; else c=p2.totalterms; for(i=0,j=0;i<=c;p3.totalterms++) { if(p1.t[i].coeff==0 && p2.t[j].coeff==0) break; if(p1.t[i].exp>=p2.t[j].exp) { if(p1.t[i].exp==p2.t[j].exp) {

p3.t[p3.totalterms].coeff=p1.t[i].coeff + p2.t[j].coeff; p3.t[p3.totalterms].exp=p1.t[i].exp; i++; j++; } else { p3.t[p3.totalterms].coeff=p1.t[i].coeff; p3.t[p3.totalterms].exp=p1.t[i].exp; i++; } } else { p3.t[p3.totalterms].coeff=p2.t[j].coeff; p3.t[p3.totalterms].exp=p2.t[j].exp; j++; } } return p3; } /* Multiplies two polynomials p1 and p2 */ struct poly mulpoly(struct poly p1,struct poly p2) { int coeff,exp; struct poly temp,p3; initpoly(&temp); initpoly(&p3); if(p1.totalterms != 0 && p2.totalterms != 0) { int i; for(i=0;i<p1.totalterms;i++) { int j; struct poly p; initpoly(&p); for(j=0;j<p2.totalterms;j++) { coeff=p1.t[i].coeff * p2.t[j].coeff; exp= p1.t[i].exp +p2.t[j].exp; polycreate(&p,coeff,exp); } if(i !=0) { p3=addpoly(temp,p); temp=p3; } else temp=p; } } return p3; } struct poly subpoly(struct poly p1,struct poly p2)

{ int i,j,c; struct poly p3; initpoly(&p3); if(p1.totalterms>p2.totalterms) c=p1.totalterms; else c=p2.totalterms; for(i=0,j=0;i<=c;p3.totalterms++) { if(p1.t[i].coeff==0 && p2.t[j].coeff==0) break; if(p1.t[i].exp>=p2.t[j].exp) { if(p1.t[i].exp==p2.t[j].exp) { p3.t[p3.totalterms].coeff=p1.t[i].coeff - p2.t[j].coeff; p3.t[p3.totalterms].exp=p1.t[i].exp; i++; j++; } else { p3.t[p3.totalterms].coeff=p1.t[i].coeff; p3.t[p3.totalterms].exp=p1.t[i].exp; i++; } } else { p3.t[p3.totalterms].coeff=-p2.t[j].coeff; p3.t[p3.totalterms].exp=p2.t[j].exp; j++; } } return p3; }

1.Create 2.Addition 3.Subtraction 4.Multiplication 5.Exit Please enter ur choice:1 Enter number of terms to be entered in polynomial 1 and 2:2 2 Enter coefficient and element:3 4 Enter coefficient and element:2 2 Enter coefficient and element:2 2

Enter coefficient and element:1 1 1.Create 2.Addition 3.Subtraction 4.Multiplication 5.Exit Please enter ur choice:2 3 x^4 + 4 x^2 + 1 x^1 1.Create 2.Addition 3.Subtraction 4.Multiplication 5.Exit Please enter ur choice:3 3 x^4 + 0 x^2 + -1 x^1 1.Create 2.Addition 3.Subtraction 4.Multiplication 5.Exit Please enter ur choice:4 6 x^6 + 3 x^5 + 4 x^4 + 2 x^3 1.Create 2.Addition 3.Subtraction 4.Multiplication 5.Exit Please enter ur choice:5 LINKED LIST REPRESENTATION #include<stdio.h> #include<stdlib.h> struct node { float coef; int expo; struct node *link; }; struct node *create(struct node *); struct node *insert_s(struct node *,float,int); struct node *insert(struct node *,float,int); void display(struct node *ptr); void poly_add(struct node *,struct node *); void poly_sub(struct node *,struct node *); void poly_mult(struct node *,struct node *); int main( )

{ int ch; struct node *start1=NULL,*start2=NULL; printf("Enter polynomial 1 :\n"); start1=create(start1); printf("Enter polynomial 2 :\n"); start2=create(start2); printf("Polynomial 1 is : "); display(start1); printf("Polynomial 2 is : "); display(start2); printf("\n1.Addition of polynomial\n2.Subtaction of polynomial\n3.Mutiplication of polynomial\n4.Exit"); scanf("%d",&ch); while(ch!=4) { switch(ch) { case 1: poly_add(start1, start2); break; case 2: poly_sub(start1,start2); break; case 3: poly_mult(start1, start2); break; case 4: exit(0); default: printf("\nPlease try again"); } printf("\n1.Addition of polynomial\n2.Subtaction of polynomial\n3.Mutiplication of polynomial\n4.Exit"); scanf("%d",&ch); } return 0; }/*End of main()*/ struct node *create(struct node *start) { int i,n,ex; float co; printf("Enter the number of terms : "); scanf("%d",&n); for(i=1;i<=n;i++) { printf("Enter coeficient for term %d : ",i); scanf("%f",&co); printf("Enter exponent for term %d : ",i); scanf("%d",&ex); start=insert_s(start,co,ex);

} return start; }/*End of create()*/ struct node *insert_s(struct node *start,float co,int ex) { struct node *ptr,*tmp; tmp=(struct node *)malloc(sizeof(struct node)); tmp->coef=co; tmp->expo=ex; /*list empty or exp greater than first one */ if(start==NULL || ex > start->expo) { tmp->link=start; start=tmp; } else { ptr=start; while(ptr->link!=NULL && ptr->link->expo >= ex) ptr=ptr->link; tmp->link=ptr->link; ptr->link=tmp; } return start; }/*End of insert()*/ struct node *insert(struct node *start,float co,int ex) { struct node *ptr,*tmp; tmp=(struct node *)malloc(sizeof(struct node)); tmp->coef=co; tmp->expo=ex; /*If list is empty*/ if(start==NULL) { tmp->link=start; start=tmp; } else /*Insert at the end of the list*/ { ptr=start; while(ptr->link!=NULL) ptr=ptr->link; tmp->link=ptr->link; ptr->link=tmp; } return start; }/*End of insert()*/ void display(struct node *ptr) { if(ptr==NULL) { printf("Zero polynomial\n"); return;

} while(ptr!=NULL) { printf("(%.1fx^%d)", ptr->coef,ptr->expo); ptr=ptr->link; if(ptr!=NULL) printf(" + "); else printf("\n"); } }/*End of display()*/ void poly_add(struct node *p1,struct node *p2) { struct node *start3; start3=NULL; while(p1!=NULL && p2!=NULL) { if(p1->expo > p2->expo) { start3=insert(start3,p1->coef,p1->expo); p1=p1->link; } else if(p2->expo > p1->expo) { start3=insert(start3,p2->coef,p2->expo); p2=p2->link; } else if(p1->expo==p2->expo) { start3=insert(start3,p1->coef+p2->coef,p1->expo); p1=p1->link; p2=p2->link; } } /*if poly2 has finished and elements left in poly1*/ while(p1!=NULL) { start3=insert(start3,p1->coef,p1->expo); p1=p1->link; } /*if poly1 has finished and elements left in poly2*/ while(p2!=NULL) { start3=insert(start3,p2->coef,p2->expo); p2=p2->link; } printf("Added polynomial is : "); display(start3); }/*End of poly_add() */ void poly_sub(struct node *p1,struct node *p2) { struct node *start4; start4=NULL;

while(p1!=NULL && p2!=NULL) { if(p1->expo > p2->expo) { start4=insert(start4,p1->coef,p1->expo); p1=p1->link; } else if(p2->expo > p1->expo) { start4=insert(start4,-p2->coef,p2->expo); p2=p2->link; } else if(p1->expo==p2->expo) { start4=insert(start4,p1->coef-p2->coef,p1->expo); p1=p1->link; p2=p2->link; } } /*if poly2 has finished and elements left in poly1*/ while(p1!=NULL) { start4=insert(start4,p1->coef,p1->expo); p1=p1->link; } /*if poly1 has finished and elements left in poly2*/ while(p2!=NULL) { start4=insert(start4,-p2->coef,p2->expo); p2=p2->link; } printf("Subtracted polynomial is : "); display(start4); }/*End of poly_add() */ void poly_mult(struct node *p1, struct node *p2) { struct node *start3; struct node *p2_beg = p2; start3=NULL; if(p1==NULL || p2==NULL) { printf("Multiplied polynomial is zero polynomial\n"); return; } while(p1!=NULL) { p2=p2_beg; while(p2!=NULL) { start3=insert_s(start3,p1->coef*p2->coef,p1->expo+p2->expo); p2=p2->link; } p1=p1->link; }

printf("Multiplied polynomial is : "); display(start3); }/*End of poly_mult()*/ Enter polynomial 1 : Enter the number of terms : 2 Enter coeficient for term 1 : 2 Enter exponent for term 1 : 2 Enter coeficient for term 2 : 2 Enter exponent for term 2 : 2 Enter polynomial 2 : Enter the number of terms : 2 Enter coeficient for term 1 : 3 Enter exponent for term 1 : 3 Enter coeficient for term 2 : 2 Enter exponent for term 2 : 2 Polynomial 1 is : (2.0x^2) + (2.0x^2) Polynomial 2 is : (3.0x^3) + (2.0x^2) 1.Addition of polynomial 2.Subtaction of polynomial 3.Mutiplication of polynomial 4.Exit1 Added polynomial is : (3.0x^3) + (4.0x^2) + (2.0x^2) 1.Addition of polynomial 2.Subtaction of polynomial 3.Mutiplication of polynomial 4.Exit2 Subtracted polynomial is : (-3.0x^3) + (0.0x^2) + (2.0x^2) 1.Addition of polynomial 2.Subtaction of polynomial 3.Mutiplication of polynomial 4.Exit3 Multiplied polynomial is : (6.0x^5) + (6.0x^5) + (4.0x^4) + (4.0x^4) 1.Addition of polynomial 2.Subtaction of polynomial 3.Mutiplication of polynomial 4.Exit4

PROGRAM-6
// // // // // // // main.c DSLAB6 Created by Vipul Verma on 31/08/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include <stdio.h> struct stack { int arr[15]; int top; }; void ini(struct stack *); void push(struct stack*,int n); int pop(struct stack *); int main(int argc, const char * argv[]) { struct stack s; int ch,n,elem; ini(&s); printf("\n1.Push\n2.Pop\n3.Exit:"); scanf("%d",&ch); while(ch!=3) { switch(ch) { case 1: printf("\nEnter number of elements you want to enter"); scanf("%d",&n); while(n--) { printf("\nEnter the element:"); scanf("%d",&elem); push(&s,elem); } break; case 2: printf("\nEnter number of elements you want to pop"); scanf("%d",&n);

while(n--) { elem=pop(&s); printf("\nItem popped is:%d\n",elem); } break; case 3: exit(0); default: printf("\nWrong choice entered please try again\n"); } printf("\n1.Push\n2.Pop\n3.Exit:"); scanf("%d",&ch); } return 0; } void ini(struct stack *s) { s->top=-1; } void push(struct stack *s,int n) { if(s->top==14) { printf("\nStack is full\n"); return; } s->top++; s->arr[s->top]=n; } int pop(struct stack *s) { int data; if(s->top==-1) { printf("\nStack is empty"); return 0; } data=s->arr[s->top]; s->top--; return data; }

1.Push 2.Pop 3.Exit:1 Enter number of elements you want to enter5 Enter the element:5 Enter the element:4 Enter the element:3

Enter the element:2 Enter the element:1 1.Push 2.Pop 3.Exit:2 Enter number of elements you want to pop2 Item popped is:1 Item popped is:2 1.Push 2.Pop 3.Exit:2 Enter number of elements you want to pop3 Item popped is:3 Item popped is:4 Item popped is:5 1.Push 2.Pop 3.Exit:2 Enter number of elements you want to pop1 Stack is empty Item popped is:0 1.Push 2.Pop 3.Exit:3 LINKED LIST REPRESENTATION // // // // // // //

main.c DSLAB6.1 Created by Vipul Verma on 31/08/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include <stdio.h> struct stack { int data; struct stack *next;

}; void push(struct stack **,int); int pop(struct stack **); void delstack(struct stack **s); int main(int argc, const char * argv[]) { struct stack *s=NULL; int ch,n,elem; printf("\n1.Push\n2.Pop\n3.Exit:"); scanf("%d",&ch); while(ch!=3) { switch(ch) { case 1: printf("\nEnter number of elements you want to enter"); scanf("%d",&n); while(n--) { printf("\nEnter the element:"); scanf("%d",&elem); push(&s,elem); } break; case 2: printf("\nEnter number of elements you want to pop"); scanf("%d",&n); while(n--) { elem=pop(&s); printf("\nItem popped is:%d\n",elem); } break; case 3: exit(0); default: printf("\nWrong choice entered please try again\n"); } printf("\n1.Push\n2.Pop\n3.Exit:"); scanf("%d",&ch); } delstack(&s); return 0; } void push(struct stack **s,int n) { struct stack *temp; temp=(struct stack *)malloc(sizeof(struct stack)); if(temp==NULL) { printf("\nStack is full\n"); return; } temp->data=n; temp->next=*s;

*s=temp; } int pop(struct stack **s) { struct stack *temp; int item; if(*s==NULL) { printf("\nStack is empty\n"); return NULL; } temp=*s; item=temp->data; *s=(*s)->next; free(temp); return item; } void delstack(struct stack **s) { struct stack *temp; if(*s==NULL) { return; } while(*s!=NULL) { temp=*s; *s=(*s)->next; free(temp); } }
1.Push 2.Pop 3.Exit:1 Enter number of elements you want to enter5 Enter the element:5 Enter the element:4 Enter the element:3 Enter the element:2 Enter the element:6 1.Push 2.Pop 3.Exit:2 Enter number of elements you want to pop2 Item popped is:6 Item popped is:2 1.Push 2.Pop 3.Exit:3

PROGRAM-7
// // // // // // // main.c DSLAB7 Created by Vipul Verma on 01/09/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include<stdio.h> #define MAX 50 int stack[MAX]; char post[MAX]; int top=-1;

void pushstack(int tmp); void calculator(char c); int main() { int i; printf("Insert a postfix notation :: "); gets(post); for(i=0;i<strlen(post);i++) { if(post[i]>='0' && post[i]<='9') { pushstack(i); } if(post[i]=='+' || post[i]=='-' || post[i]=='*' || post[i]=='/' || post[i]=='^') { calculator(post[i]); } } printf("\n\nResult :: %d",stack[top]); return 0; } void pushstack(int tmp) { top++; stack[top]=(int)(post[tmp]-48); } void calculator(char c) { int a,b,ans; a=stack[top]; stack[top]='\0';

top--; b=stack[top]; stack[top]='\0'; top--; switch(c) { case '+': ans=b+a; break; case '-': ans=b-a; break; case '*': ans=b*a; break; case '/': ans=b/a; break; case '^': ans=pow(b,a); break; default: ans=0; } top++; stack[top]=ans; }

Insert a postfix notation :: 23+5*6+

Result :: 31

PROGRAM-8

#include<stdio.h> #include<math.h> struct infix { char stack[50]; char target[50]; char *s,*t; int top; }; void ini(struct infix *); void set(struct infix *,char *); void push(struct infix *,char ); char pop(struct infix *); int prior(char c); void show(struct infix p); void convert(struct infix *p); int main() { struct infix p; char expr[50]; ini(&p); printf("Enter an expression in infix form:"); gets(expr); set(&p,expr); convert(&p); printf("\nThe postfix expression is:\n"); show(p); return 0; } void ini(struct infix *p) { p->top=-1; strcpy(p->target,""); strcpy(p->stack,""); p->t=p->target; p->s=""; } void set(struct infix *p,char *str) { p->s=str; } void push(struct infix *p,char c) { if(p->top==50) { printf("\nStack is full\n"); } else { p->top++; p->stack[p->top]=c; } } char pop(struct infix *p)

{ if(p->top==-1) { printf("\nStack is empty"); return -1; } else { char item=p->stack[p->top]; p->top--; return item; } } int prior(char c) { if(c == '$') return 3; if(c== '*' || c=='/' || c=='%') return 2; else { if(c=='+' || c=='-') return 1; else return 0; } } void show(struct infix p) { printf("%s",p.target); } void convert(struct infix *p) { char opr; while(*(p->s)) { if(*(p->s)== ' ' || *(p->s)=='\t') { p->s++; continue; } if( isdigit(*(p->s)) || isalpha(*(p->s))) { while(isdigit(*(p->s)) || isalpha(*(p->s))) { *(p->t)=*(p->s); p->s++; p->t++; } } if(*(p->s)=='(') { push(p,*(p->s)); p->s++; }

if(*(p->s)=='*' || *(p->s)=='+' || *(p->s)=='-' || *(p->s)=='/' || *(p->s)=='%' || *(p>s)=='$') { if(p->top!=-1) { opr=pop(p); while(prior(opr)>=prior(*(p->s))) { *(p->t)=opr; p->t++; opr=pop(p); } push(p,opr); push(p,*(p->s)); } else push(p,*(p->s)); p->s++; } if(*(p->s)==')') { opr=pop(p); while((opr)!='(') { *(p->t)=opr; p->t++; opr=pop(p); } p->s++; } } while(p->top!=-1) { char opr=pop(p); *(p->t)=opr; p->t++; } *(p->t)='\0'; } Enter an expression in infix form:2+3*(6+9)+(4*9)

The postfix expression is: 2369+*+49*+

Sparse Matrix Programs: Addition of two sparse matrix-:

// // // // // // //

main.c SP-1 Created by Vipul Verma on 05/10/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include <stdio.h> #define MAX1 3 #define MAX2 3 #define MAXSIZE 9 #define BIGNUM 100 struct sparse { int *sp ; int row ; int *result ; }; void initsparse ( struct sparse * ) ; void create_array ( struct sparse * ) ; int count ( struct sparse ) ; void display ( struct sparse ) ; void create_tuple ( struct sparse *, struct sparse ) ; void display_tuple ( struct sparse ) ; void addmat ( struct sparse *, struct sparse, struct sparse ) ; void display_result ( struct sparse ) ; void delsparse ( struct sparse * ) ; void main( ) { struct sparse s[5] ; int i ; for ( i = 0 ; i <= 4 ; i++ ) initsparse ( &s[i] ) ; create_array ( &s[0] ) ; create_tuple ( &s[1], s[0] ) ; display_tuple ( s[1] ) ; create_array ( &s[2] ) ; create_tuple ( &s[3], s[2] ) ; display_tuple ( s[3] ) ; addmat ( &s[4], s[1], s[3] ) ; printf ( "\nResult of addition of two matrices: " ) ; display_result ( s[4] ) ; for ( i = 0 ; i <= 4 ; i++ ) delsparse ( &s[i] ) ; } /* initialises structure elements */ void initsparse ( struct sparse *p ) { p -> sp = NULL ; p -> result = NULL ; } /* dynamically creates the matrix */ void create_array ( struct sparse *p )

{ int n, i ; /* allocate memory */ p -> sp = ( int * ) malloc ( MAX1 * MAX2 * sizeof ( int ) ) ; /* add elements to the array */ for ( i = 0 ; i < MAX1 * MAX2 ; i++ ) { printf ( "Enter element no. %d:", i ) ; scanf ( "%d", &n ) ; * ( p -> sp + i ) = n ; } } /* displays the contents of the matrix */ void display ( struct sparse s ) { int i ; /* traverses the entire matrix */ for ( i = 0 ; i < MAX1 * MAX2 ; i++ ) { /* positions the cursor to the new line for every new row */ if ( i % MAX2 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.sp + i ) ) ; } } /* counts the number of non-zero elements */ int count ( struct sparse s ) { int cnt = 0, i ; for ( i = 0 ; i < MAX1 * MAX2 ; i++ ) { if ( * ( s.sp + i ) != 0 ) cnt++ ; } return cnt ; } /* creates an array that stores information about non-zero elements */ void create_tuple ( struct sparse *p, struct sparse s ) { int r = 0 , c = -1, l = -1, i ; /* get the total number of non-zero elements and add 1 to store total no. of rows, cols, and non-zero values */ p -> row = count ( s ) + 1 ; /* allocate memory */ p -> sp = ( int * ) malloc ( p -> row * 3 * sizeof ( int ) ) ; /* store information about total no. of rows, cols, and non-zero values */ * ( p -> sp + 0 ) = MAX1 ; * ( p -> sp + 1 ) = MAX2 ; * ( p -> sp + 2 ) = p -> row - 1 ; l=2; /* scan the array and store info. about non-zero values in the 3-tuple */ for ( i = 0 ; i < MAX1 * MAX2 ; i++ ) { c++ ;

/* sets the row and column values */ if ( ( ( i % MAX2 ) == 0 ) && ( i != 0 ) ) { r++ ; c=0; } /* checks for non-zero element row, column and non-zero element value is assigned to the matrix */ if ( * ( s.sp + i ) != 0 ) { l++ ; * ( p -> sp + l ) = r ; l++ ; * ( p -> sp + l ) = c ; l++ ; * ( p -> sp + l ) = * ( s.sp + i ) ; } } } /* displays the contents of the matrix */ void display_tuple ( struct sparse s ) { int i, j ; /* traverses the entire matrix */ printf ( "\nElements in a 3-tuple: \n" ) ; j = ( * ( s.sp + 2 ) * 3 ) + 3 ; for ( i = 0 ; i < j ; i++ ) { /* positions the cursor to the new line for every new row */ if ( i % 3 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.sp + i ) ) ; } printf ( "\n" ) ; } /* carries out addition of two matrices */ void addmat ( struct sparse *p, struct sparse s1, struct sparse s2 ) { int i = 1, j = 1, k = 1 ; int elem = 1 ; int max, amax, bmax ; int rowa, rowb, cola, colb, vala, valb ; /* get the total number of non-zero values from both the matrices */ amax = * ( s1.sp + 2 ) ; bmax = * ( s2.sp + 2 ) ; max = amax + bmax ; /* allocate memory for result */ p -> result = ( int * ) malloc ( MAXSIZE * 3 * sizeof ( int ) ) ; while ( elem <= max ) { /* check if i < max. non-zero values in first 3-tuple and get the values */ if ( i <= amax ) { rowa = * ( s1.sp + i * 3 + 0 ) ;

cola = * ( s1.sp + i * 3 + 1 ) ; vala = * ( s1.sp + i * 3 + 2 ) ; } else rowa = cola = BIGNUM ; /* check if j < max. non-zero values in secon 3-tuple and get the values */ if ( j <= bmax ) { rowb = * ( s2.sp + j * 3 + 0 ) ; colb = * ( s2.sp + j * 3 + 1 ) ; valb = * ( s2.sp + j * 3 + 2 ) ; } else rowb = colb = BIGNUM ; /* if row no. of both 3-tuple are same */ if ( rowa == rowb ) { /* if col no. of both 3-tuple are same */ if ( cola == colb ) { /* add tow non-zero values store in result */ * ( p -> result + k * 3 + 0 ) = rowa ; * ( p -> result + k * 3 + 1 ) = cola ; * ( p -> result + k * 3 + 2 ) = vala + valb ; i++ ; j++ ; max-- ; } /* if col no. of first 3-tuple is < col no. of second 3-tuple, then add info. as it is to result */ if ( cola < colb ) { * ( p -> result + k * 3 + 0 ) = rowa ; * ( p -> result + k * 3 + 1 ) = cola ; * ( p -> result + k * 3 + 2 ) = vala ; i++ ; } /* if col no. of first 3-tuple is > col no. of second 3-tuple, then add info. as it is to result */ if ( cola > colb ) { * ( p -> result + k * 3 + 0 ) = rowb ; * ( p -> result + k * 3 + 1 ) = colb ; * ( p -> result + k * 3 + 2 ) = valb ; j++ ; } k++ ; } /* if row no. of first 3-tuple is < row no. of second 3-tuple, then add info. as it is to result */ if ( rowa < rowb ) { * ( p -> result + k * 3 + 0 ) = rowa ; * ( p -> result + k * 3 + 1 ) = cola ;

* ( p -> result + k * 3 + 2 ) = vala ; i++ ; k++ ; } /* if row no. of first 3-tuple is > row no. of second 3-tuple, then add info. as it is to result */ if ( rowa > rowb ) { * ( p -> result + k * 3 + 0 ) = rowb ; * ( p -> result + k * 3 + 1 ) = colb ; * ( p -> result + k * 3 + 2 ) = valb ; j++ ; k++ ; } elem++ ; } /* add info about the total no. of rows, cols, and non-zero values that the resultant array contains to the result */ * ( p -> result + 0 ) = MAX1 ; * ( p -> result + 1 ) = MAX2 ; * ( p -> result + 2 ) = max ; } /* displays the contents of the matrix */ void display_result ( struct sparse s ) { int i ; /* traverses the entire matrix */ for ( i = 0 ; i < ( * ( s.result + 0 + 2 ) + 1 ) * 3 ; i++ ) { /* positions the cursor to the new line for every new row */ if ( i % 3 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.result + i ) ) ; } } /* deallocates memory */ void delsparse ( struct sparse *p ) { if ( p -> sp != NULL ) free ( p -> sp ) ; if ( p -> result != NULL ) free ( p -> result ) ; } Enter element no. 0:1 Enter element no. 1:0 Enter element no. 2:0 0Enter element no. 3:0 Enter element no. 4:2 Enter element no. 5:0 Enter element no. 6:0 Enter element no. 7:2 Enter element no. 8:0 Elements in a 3-tuple:

3 33 0 01 1 12 2 12 Enter element no. 0:2 Enter element no. 1:1 Enter element no. 2:9 Enter element no. 3:0 Enter element no. 4:0 Enter element no. 5:0 Enter element no. 6:0 Enter element no. 7:0 Enter element no. 8:0 Elements in a 3-tuple: 3 0 0 0 3 0 1 2 3 2 1 9

Result of addition of two matrices: 3 35 0 03 0 11 0 29 1 12 2 1 2

Sparse Matrix Multiplication:

#include<stdio.h> #define MAX1 3 #define MAX2 3 #define MAXSIZE 20 #define TRUE 1 #define FALSE 2 struct sparse { int *sp ; int row ; int *result ; }; void initsparse ( struct sparse * ) ; void create_array ( struct sparse * ) ; int count ( struct sparse ) ; void display ( struct sparse ) ; void create_tuple ( struct sparse*, struct sparse ) ;

void display_tuple ( struct sparse ) ; void prodmat ( struct sparse *, struct sparse, struct sparse ) ; void searchina ( int *sp, int ii, int*p, int*flag ) ; void searchinb ( int *sp, int jj, int colofa, int*p, int*flag ) ; void display_result ( struct sparse ) ; void delsparse ( struct sparse * ) ; void main( ) { struct sparse s[5] ; int i ; for ( i = 0 ; i<= 3 ; i++ ) initsparse ( &s[i] ) ; create_array ( &s[0] ) ; create_tuple ( &s[1], s[0] ) ; display_tuple ( s[1] ) ; create_array ( &s[2] ) ; create_tuple ( &s[3], s[2] ) ; display_tuple ( s[3] ) ; prodmat ( &s[4], s[1], s[3] ) ; printf ( "\nResult of multiplication of two matrices: " ) ; display_result ( s[4] ) ; for ( i = 0 ; i<= 3 ; i++ ) delsparse ( &s[i] ) ; } /* initialises elements of structure */ void initsparse ( struct sparse *p ) { p -> sp = NULL ; p -> result = NULL ; } /* dynamically creates the matrix */ void create_array ( struct sparse *p ) { int n, i ; /* allocate memory */ p -> sp = ( int * ) malloc ( MAX1 * MAX2 * sizeof ( int ) ) ; /* add elements to the array */ for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) { printf ( "Enter element no. %d: ", i ) ; scanf ( "%d", &n ) ; * ( p -> sp + i ) = n ; } } /* displays the contents of the matrix */ void display ( struct sparse s ) { int i ; /* traverses the entire matrix */ for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) { /* positions the cursor to the new line for every new row */ if ( i % 3 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.sp + i ) ) ;

} } /* counts the number of non-zero elements */ int count ( struct sparse s ) { int cnt = 0, i ; for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) { if ( * ( s.sp + i ) != 0 ) cnt++ ; } return cnt ; } /* creates an array that stores information about non-zero elements */ void create_tuple ( struct sparse *p, struct sparse s ) { int r = 0 , c = -1, l = -1, i ; /* get the total number of non-zero elements */ p -> row = count ( s ) + 1 ; /* allocate memory */ p -> sp = ( int * ) malloc ( p -> row * 3 * sizeof ( int ) ) ; /* store information about total no. of rows, cols, and non-zero values */ * ( p -> sp + 0 ) = MAX1 ; * ( p -> sp + 1 ) = MAX2 ; * ( p -> sp + 2 ) = p -> row - 1 ; l=2; /* scan the array and store info. about non-zero values in the 3-tuple */ for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) { c++ ; /* sets the row and column values */ if ( ( ( i % 3 ) == 0 ) && ( i != 0 ) ) { r++ ; c=0; } /* checks for non-zero element, row, column and non-zero value is assigned to the matrix */ if ( * ( s.sp + i ) != 0 ) { l++ ; * ( p -> sp + l ) = r ; l++ ; * ( p -> sp + l ) = c ; l++ ; * ( p -> sp + l ) = * ( s.sp + i ) ; } } } /* displays the contents of the matrix */ void display_tuple ( struct sparse s ) { int i, j ; /* traverses the entire matrix */ printf ( "\nElements in a 3-tuple: " ) ;

j = ( * ( s.sp + 2 ) * 3 ) + 3 ; for ( i = 0 ; i< j ; i++ ) { /* positions the cursor to the new line for every new row */ if ( i % 3 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.sp + i ) ) ; } printf ( "\n" ) ; } /* performs multiplication of sparse matrices */ void prodmat ( struct sparse *p, struct sparse a, struct sparse b ) { int sum, k, position, posi, flaga, flagb, i , j ; k=1; p -> result = ( int * ) malloc ( MAXSIZE * 3 * sizeof ( int ) ) ; for ( i = 0 ; i< * ( a.sp + 0 * 3 + 0 ) ; i++ ) { for ( j = 0 ; j< * ( b.sp + 0 * 3 + 1 ) ; j++ ) { /* search if an element present at ith row */ searchina ( a.sp, i, &position, &flaga ) ; if ( flaga == TRUE ) { sum = 0 ; /* run loop till there are element at ith row in first 3-tuple */ while ( * ( a.sp + position * 3 + 0 ) == i ) { /* search if an element present at ith col. in second 3-tuple */ searchinb ( b.sp, j, * ( a.sp + position * 3 + 1 ), &posi, &flagb ) ; /* if found then multiply */ if ( flagb == TRUE ) sum = sum + * ( a.sp + position * 3 + 2 ) * * ( b.sp + posi * 3 + 2 ) ; position = position + 1 ; } /* add result */ if ( sum != 0 ) { * ( p -> result + k * 3 + 0 ) = i ; * ( p -> result + k * 3 + 1 ) = j ; * ( p -> result + k * 3 + 2 ) = sum ; k=k+1; } } } } /* add total no. of rows, cols and non-zero values */ * ( p -> result + 0 * 3 + 0 ) = * ( a.sp + 0 * 3 + 0 ) ; * ( p -> result + 0 * 3 + 1 ) = * ( b.sp + 0 * 3 + 1 ) ; * ( p -> result + 0 * 3 + 2 ) = k - 1 ; } /* searches if an element present at iith row */ void searchina ( int *sp, int ii, int *p, int *flag ) { int j ;

*flag = FALSE ; for ( j = 1 ; j<= * ( sp + 0 * 3 + 2 ) ; j++ ) { if ( * ( sp + j * 3 + 0 ) == ii ) { *p = j ; *flag = TRUE ; return ; } } } /* searches if an element where col. of first 3-tuple is equal to row of second 3-tuple */ void searchinb ( int *sp, int jj, int colofa, int *p, int *flag ) { int j ; *flag = FALSE ; for ( j = 1 ; j<= * ( sp + 0 * 3 + 2 ) ; j++ ) { if ( * ( sp + j * 3 + 1 ) == jj && * ( sp + j * 3 + 0 ) == colofa ) { *p = j ; *flag = TRUE ; return ; } } } /* displays the contents of the matrix */ void display_result ( struct sparse s ) { int i ; /* traverses the entire matrix */ for ( i = 0 ; i< ( * ( s.result + 0 + 2 ) + 1 ) * 3 ; i++ ) { /* positions the cursor to the new line for every new row */ if ( i % 3 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.result + i ) ) ; } } /* deallocates memory */ void delsparse ( struct sparse *s ) { if ( s -> sp != NULL ) free ( s -> sp ) ; if ( s -> result != NULL ) free ( s -> result ) ; } Enter element no. 0: 1 Enter element no. 1: 1 Enter element no. 2: 0 Enter element no. 3: 0 Enter element no. 4: 0

Enter element no. 5: 0 Enter element no. 6: 2 Enter element no. 7: 0 Enter element no. 8: 0 Elements in a 3-tuple: 3 33 0 01 0 11 2 02 Enter element no. 0: 1 Enter element no. 1: 2 Enter element no. 2: 3 Enter element no. 3: 4 Enter element no. 4: 0 Enter element no. 5: 0 Enter element no. 6: 0 Enter element no. 7: 0 Enter element no. 8: 0 Elements in a 3-tuple: 3 34 0 01 0 12 0 23 1 04 Result of multiplication of two matrices: 3 36 0 05 0 12 0 23 2 02 2 14 2 2 6

Simple Queue using array


// // main.c // DSQ-1 // // Created by Vipul Verma on 03/10/13. // Copyright (c) 2013 Vipul Verma. All rights reserved. // #include<stdio.h> #define N 6 int queue[N]={0}; int rear=0,front=0; void insert(void); void del(void); void disp(void); void cre(void); int main() { int user=0; while(user!=4) { printf("\n\n\n\t\t\t THE SIZE OF QUEUE IS %d",N); printf("\n\t 1.INSERT"); printf("\n\t 2.DELETE"); printf("\n\t 3.DISPLAY"); printf("\n\t 4.EXIT"); printf("\n\t 5.CREATE"); scanf("%d",&user); switch(user) { case 1: insert(); break; case 2: del(); break; case 3: disp(); break; case 4: printf("\n\t THANK U"); break;

case 5: cre(); break; } } }

/*********************insert********************/ void insert(void) { int t; if(rear<N) { printf("\n\t ENTER A VALUE IN QUEUE"); scanf("%d",&t); queue[rear]=t; rear++; } else { printf("\n\t Q OVERFLOW!!!!!!!!!!!!!!!"); } } void del(void) { printf("\n\t %d gets deleted.........",queue[front]); queue[front]=0; front++; } void disp(void) { int i; for(i=front;i<rear;i++) { printf("\n\t %d",queue[i]); } } void cre(void) { int t; printf("\n\t ENTER A VALUE IN QUEUE"); scanf("%d",&t); front=0; queue[front]=t; rear=front+1; }

THE SIZE OF QUEUE IS 6 1.INSERT

2.DELETE 3.DISPLAY 4.EXIT 5.CREATE5 ENTER A VALUE IN QUEUE2

THE SIZE OF QUEUE IS 6 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT 5.CREATE1 ENTER A VALUE IN QUEUE6

THE SIZE OF QUEUE IS 6 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT 5.CREATE3 2 6

THE SIZE OF QUEUE IS 6 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT 5.CREATE2 2 gets deleted.........

THE SIZE OF QUEUE IS 6 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT 5.CREATE4 THANK U

Circular Queue using array // // main.c

// DSQ-3 // // Created by Vipul Verma on 05/10/13. // Copyright (c) 2013 Vipul Verma. All rights reserved. // //Circular queue #include<stdio.h> #define max 5 int q[max]; int rear=-1,front=-1; void insert(); void delet(); void display(); void main() { int ch; int choice; do { printf("\n1 -> Insert"); printf("\n2 -> Delete"); printf("\n3 -> Display"); printf("\n0 -> Exit"); printf("\n\nEnter Your Choice -> "); scanf("%d",&choice); switch(choice) { case 1: insert(); break; case 2: delet(); break; case 3: display(); break; case 0: exit(0); default: printf("\n\nInvalid Choice"); } printf("\n\nDo u Want to Continue(1 for yes) -> "); scanf("%d",&ch); }while(ch == 1); } void insert() { int data; if((rear==max-1 && front == 0 ) || rear==front-1)

{ printf("\nSorry! Q is Full"); } else { printf("\nEnter data You want to insert ->"); scanf("%d",&data); if(front==-1) { front++; rear++; } else if(rear==max-1) { rear=0; } else { rear++; } q[rear]=data; printf("\n\nData inserted successfully"); } } void delet() { if(front==-1) { printf("\n\nSorry! Q is Empty"); } else { if(front==rear) { front=rear=-1; } else if(front==max-1) { front=0; } else { front++; } printf("\nElement deleted Successfully"); } } void display() { int i; if(front==-1) { printf("\n\nSorry! Q is Empty"); }

else { printf("\n\n:: Queue Elements are ::\n"); if(front<=rear) { for(i=front;i<=rear;i++) { printf("\n%d",q[i]); } } else { for(i=front;i<max;i++) { printf("\n%d",q[i]); } for(i=0;i<=rear;i++) { printf("\n%d",q[i]); } } } }

1 -> Insert 2 -> Delete 3 -> Display 0 -> Exit Enter Your Choice -> 1 Enter data You want to insert ->1

Data inserted successfully Do u Want to Continue(1 for yes) -> 1 1 -> Insert 2 -> Delete 3 -> Display 0 -> Exit Enter Your Choice -> 1 Enter data You want to insert ->1

Data inserted successfully Do u Want to Continue(1 for yes) -> 1 1 -> Insert 2 -> Delete

3 -> Display 0 -> Exit Enter Your Choice -> 1 Enter data You want to insert ->1

Data inserted successfully Do u Want to Continue(1 for yes) -> 1 1 -> Insert 2 -> Delete 3 -> Display 0 -> Exit Enter Your Choice -> 1 Enter data You want to insert ->1

Data inserted successfully Do u Want to Continue(1 for yes) -> 1 1 -> Insert 2 -> Delete 3 -> Display 0 -> Exit Enter Your Choice -> 1 Enter data You want to insert ->1

Data inserted successfully Do u Want to Continue(1 for yes) -> 1 1 -> Insert 2 -> Delete 3 -> Display 0 -> Exit Enter Your Choice -> 1 Sorry! Q is Full Do u Want to Continue(1 for yes) -> 1 1 -> Insert 2 -> Delete 3 -> Display 0 -> Exit

Enter Your Choice -> 3

:: Queue Elements are :: 1 1 1 1 1 Do u Want to Continue(1 for yes) -> 1 1 -> Insert 2 -> Delete 3 -> Display 0 -> Exit Enter Your Choice -> 2 Element deleted Successfully Do u Want to Continue(1 for yes) -> 1 1 -> Insert 2 -> Delete 3 -> Display 0 -> Exit Enter Your Choice -> 1 Enter data You want to insert ->5

Data inserted successfully Do u Want to Continue(1 for yes) -> 1 1 -> Insert 2 -> Delete 3 -> Display 0 -> Exit Enter Your Choice -> 3

:: Queue Elements are :: 1 1 1 1 5

Dequeue in C

// // // // // // //

main.c DSQ-4 Created by Vipul Verma on 05/10/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#define MAX 100 #include<stdio.h> void insert(int); int delStart(); int delEnd(); int queue[MAX]; int rear =0, front =0; void display(); void insertEnd(int); void insertStart(int); int main() { int ch; int choice, c, token; printf("Enter your choice for which deque operation you want to perform operation \n"); do { printf("\n1.Input-restricted deque \n"); printf("2.output-restricted deque \n"); printf("\nEnter your choice for the operation : "); scanf("%d",&c); switch(c) { case 1: printf("\nDo operation in Input-Restricted c deque\n"); printf("1.Insert"); printf("\n2.Delete from end"); printf("\n3.Delete from begning"); printf("\n4.show or display"); do { printf("\nEnter your choice for the operation in c deque: "); scanf("%d",&choice); switch(choice) { case 1: insertEnd(token); display(); break; case 2: token=delEnd(); printf("\nThe token deleted is %d",token); display();

break; case 3: token=delStart(); printf("\nThe token deleted is %d",token); display(); break; case 4: display(); break; default:printf("Wrong choice"); break; } printf("\nDo you want to continue(y/n) to do operation in input-restricted c deque: "); scanf("%d",&ch); } while(ch==1); break; case 2 : printf("\nDo operation in Output-Restricted c deque\n"); printf("1.Insert at the End"); printf("\n2.Insert at the begning"); printf("\n3.Delete the element"); printf("\n4.show or display"); do { printf("\nEnter your choice for the operation: "); scanf("%d",&choice); switch(choice) { case 1: insertEnd(token); display(); break; case 2: insertStart(token); display(); break; case 3: token=delStart(); printf("\nThe token deleted is %d",token); display(); break; case 4: display(); break; default:printf("Wrong choice"); break; } printf("\nDo you want to continue(y/n):"); scanf("%d",&ch); } while(ch==1); break ; } printf("\nDo you want to continue(y/n):"); scanf("%d",&ch); } while(ch==1);

} void display() { int i; printf("\nThe queue elements are:"); for(i=rear;i<front;i++) { printf("%d ",queue[i]); } } void insertEnd(int token) { int a; if(front==MAX/2) { printf("\nQueue full\nyou cant enter more elements at the end of c queue"); return; } do { printf("\nEnter the token to be inserted:"); scanf("%d",&token); queue[front]=token; front=front+1; printf("do you want to continue insertion Y/N"); scanf("%d",&a); } while(a==1); } void insertStart(int token) { int a; if(front==MAX/2) { printf("\nQueue full\nyou cant enter more elements at the start of queue"); return; } do { printf("\nEnter the token to be inserted:"); scanf("%d",&token); rear=rear-1; queue[rear]=token; printf("do you want to continue insertion Y/N"); scanf("%d",&a); } while(a==1); } int delEnd() { int t; if(front==rear)

{ printf("\nQueue empty"); return 0; } front=front-1; t=queue[front+1]; return t; } int delStart() { int t; if(front==rear) { printf("\nQueue empty"); return 0; } rear=rear+1; t=queue[rear-1]; return t; }

Enter your choice for which deque operation you want to perform operation 1.Input-restricted deque 2.output-restricted deque Enter your choice for the operation : 1 Do operation in Input-Restricted c deque 1.Insert 2.Delete from end 3.Delete from begning 4.show or display Enter your choice for the operation in c deque: 1 Enter the token to be inserted:5 do you want to continue insertion Y/N1 Enter the token to be inserted:6 do you want to continue insertion Y/N2 The queue elements are:5 6 Do you want to continue(y/n) to do operation in input-restricted c deque: 1 Enter your choice for the operation in c deque: 2 The token deleted is 0 The queue elements are:5 Do you want to continue(y/n) to do operation in input-restricted c deque: 1 Enter your choice for the operation in c deque: 3 The token deleted is 5

The queue elements are: Do you want to continue(y/n) to do operation in input-restricted c deque: 1 Enter your choice for the operation in c deque: 4 The queue elements are: Do you want to continue(y/n) to do operation in input-restricted c deque: 2 Do you want to continue(y/n):1 1.Input-restricted deque 2.output-restricted deque Enter your choice for the operation : 2 Do operation in Output-Restricted c deque 1.Insert at the End 2.Insert at the begning 3.Delete the element 4.show or display Enter your choice for the operation: 1 Enter the token to be inserted:5 do you want to continue insertion Y/N1 Enter the token to be inserted:2 do you want to continue insertion Y/N1 Enter the token to be inserted:9 do you want to continue insertion Y/N1 Enter the token to be inserted:6 do you want to continue insertion Y/N2 The queue elements are:5 2 9 6 Do you want to continue(y/n):1 Enter your choice for the operation: 2 Enter the token to be inserted:1 do you want to continue insertion Y/N2 The queue elements are:1 5 2 9 6 Do you want to continue(y/n):1 Enter your choice for the operation: 3 The token deleted is 1 The queue elements are:5 2 9 6 Do you want to continue(y/n):1 Enter your choice for the operation: 4 The queue elements are:5 2 9 6 Do you want to continue(y/n):2

Do you want to continue(y/n):2

Priority Queue
#include <stdio.h> #include <stdlib.h> #define MAX 5 void insert_by_priority(int); void delete_by_priority(int); void create(); void check(int); void display_pqueue(); int pri_que[MAX]; int front, rear; void main() { int n, ch; printf("\n1 - Insert an element into queue"); printf("\n2 - Delete an element from queue"); printf("\n3 - Display queue elements"); printf("\n4 - Exit"); create(); while (1) { printf("\nEnter your choice : "); scanf("%d", &ch); switch (ch) { case 1: printf("\nEnter value to be inserted : "); scanf("%d",&n); insert_by_priority(n); break; case 2: printf("\nEnter value to delete : "); scanf("%d",&n); delete_by_priority(n); break; case 3: display_pqueue(); break; case 4: exit(0); default:

printf("\nChoice is incorrect, Enter a correct choice"); } } } /* Function to create an empty priority queue */ void create() { front = rear = -1; } /* Function to insert value into priority queue */ void insert_by_priority(int data) { if (rear >= MAX - 1) { printf("\nQueue overflow no more elements can be inserted"); return; } if ((front == -1) && (rear == -1)) { front++; rear++; pri_que[rear] = data; return; } else check(data); rear++; } /* Function to check priority and place element */ void check(int data) { int i,j; for (i = 0; i <= rear; i++) { if (data >= pri_que[i]) { for (j = rear + 1; j > i; j--) { pri_que[j] = pri_que[j - 1]; } pri_que[i] = data; return; } } pri_que[i] = data; } /* Function to delete an element from queue */ void delete_by_priority(int data) { int i;

if ((front==-1) && (rear==-1)) { printf("\nQueue is empty no elements to delete"); return; } for (i = 0; i <= rear; i++) { if (data == pri_que[i]) { for (; i < rear; i++) { pri_que[i] = pri_que[i + 1]; } pri_que[i] = -99; rear--; if (rear == -1) front = -1; return; } } printf("\n%d not found in queue to delete", data); } /* Function to display queue elements */ void display_pqueue() { if ((front == -1) && (rear == -1)) { printf("\nQueue is empty"); return; } for (; front <= rear; front++) { printf(" %d ", pri_que[front]); } front = 0; }
1 - Insert an element into 2 - Delete an element from 3 - Display queue elements 4 Exit Enter your choice : 1 Enter value to be inserted Enter your choice : 1 Enter value to be inserted Enter your choice : 1 queue queue

: 20 : 45

Enter Enter Enter Enter Enter 89 56 Enter Enter Enter Enter

value to be inserted your choice : 3 89 your choice : 1 value to be inserted your choice : 3 45 20 your choice : 2 value to delete : 45 your choice : 3 89 your choice : 4

: 89 45 20 : 56

56

20

Simple Queue as linked list


// // // // // // // main.c DSQ-6 Created by Vipul Verma on 05/10/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include<stdio.h> struct node { int info; struct node *next; } *front, *rear; void enqueue(int elt); int dequeue(); void display(); void main() { int ch, elt; rear = NULL; front = NULL; while (1) { printf("************ Menu ***************"); printf("\nEnter:\n1->Insert\n2->Delete\n3->Display\n4->Exit\n"); printf("Enter your choice :: "); scanf("%d", &ch); switch (ch) { case 1: printf("Enter The Element Value\n"); scanf("%d", &elt); enqueue(elt); break; case 2: elt = dequeue(); printf("The deleted element = %d\n", elt); break;

case 3: display(); break; default: printf("~~~Exit~~~"); exit(0); break; } } } void enqueue(int elt) { struct node *p; p = (struct node*)malloc(sizeof(struct node)); p->info = elt; p->next = NULL; if (rear == NULL || front == NULL) front = p; else rear->next = p; rear = p; } int dequeue() { struct node *p; int elt; if (front == NULL || rear == NULL) { printf("\nUnder Flow"); exit(0); } else { p = front; elt = p->info; front = front->next; free(p); } return (elt); } void display() { struct node *t; t = front; while (front == NULL || rear == NULL) { printf("\nQueue is empty"); exit(0); } while (t != NULL) { printf("->%d", t->info); t = t->next; } }

************ Menu *************** Enter: 1->Insert 2->Delete 3->Display 4->Exit Enter your choice :: 1 Enter The Element Value 5 ************ Menu *************** Enter: 1->Insert 2->Delete 3->Display 4->Exit Enter your choice :: 1 Enter The Element Value 6 ************ Menu *************** Enter: 1->Insert 2->Delete 3->Display 4->Exit Enter your choice :: 3 ->5->6************ Menu *************** Enter: 1->Insert 2->Delete 3->Display 4->Exit Enter your choice :: 2 The deleted element = 5 ************ Menu *************** Enter: 1->Insert 2->Delete 3->Display 4->Exit Enter your choice :: 3 ->6************ Menu *************** Enter: 1->Insert 2->Delete 3->Display 4->Exit Enter your choice :: 4 ~~~Exit~~~ Circular Queue as linked list // // main.c // DSQ-7

// // Created by Vipul Verma on 05/10/13. // Copyright (c) 2013 Vipul Verma. All rights reserved. // #include<stdio.h> #include<stdlib.h> struct Node { int Data; struct Node* next; }*rear, *front; void delQueue() { struct Node *temp, *var=rear; if(var==rear) { rear = rear->next; free(var); } else printf("\nQueue Empty"); } void push(int value) { struct Node *temp; temp=(struct Node *)malloc(sizeof(struct Node)); temp->Data=value; if (front == NULL) { front=temp; front->next=NULL; rear=front; } else { front->next=temp; front=temp; front->next=rear; } } void display() { struct Node *var=rear; if(var!=NULL) { printf("\nElements are as: "); while(var!=front) { printf("\t%d",var->Data); var=var->next; } if(var==front)

{ printf("\t%d",var->Data); } printf("\n"); } else printf("\nQueue is Empty"); } int main(int argc, char *argv[]) { int i=0; front=NULL; printf(" \n1. Push to Queue"); printf(" \n2. Pop from Queue"); printf(" \n3. Display Data of Queue"); printf(" \n4. Exit\n"); while(1) { printf(" \nChoose Option: "); scanf("%d",&i); switch(i) { case 1: { int value; printf("\nEnter a valueber to push into Queue: "); scanf("%d",&value); push(value); display(); break; } case 2: { delQueue(); display(); break; } case 3: { display(); break; } case 4: { exit(0); } default: { printf("\nwrong choice for operation"); } } } }

1. Push to Queue 2. Pop from Queue 3. Display Data of Queue 4. Exit Choose Option: 1 Enter a valueber to push into Queue: 5 Elements are as: Choose Option: 1 Enter a valueber to push into Queue: 6 Elements are as: Choose Option: 1 Enter a valueber to push into Queue: 8 Elements are as: Choose Option: 2 Elements are as: Choose Option: 4 6 8 5 6 8 5 6 5

Priority Queue as linked list:


// // // // // // // main.c DSQ-8 Created by Vipul Verma on 05/10/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

//priority queue #include<stdio.h> struct q { int info,prio; struct q *link; }*front=NULL,*rear=NULL,*temp,*neww,*prev; void insert(); void delet(); void display();

void main() { int ch; int choice; do { printf("\n1 -> Insert"); printf("\n2 -> Delete"); printf("\n3 -> Display"); printf("\n0 -> Exit"); printf("\n\nEnter Your Choice -> "); scanf("%d",&choice); switch(choice) { case 1: insert(); break; case 2: delet(); break; case 3: display(); break; case 0: exit(0); default: printf("\n\nInvalid Choice"); } printf("\n\nDo u Want to Continue -> "); scanf("%d",&ch); }while(ch == 1); } void insert() { int data,p; printf("\nEnter Q Element -> "); scanf("%d",&data); printf("\nEnter Priority -> "); scanf("%d",&p); neww=(struct q *)malloc(sizeof(struct q)); neww->info=data; neww->prio=p; neww->link=NULL; if(front==NULL) { front=rear=neww; } else { temp=front; while(temp->prio <= p && temp!=NULL) {

prev=temp; temp=temp->link; } if(prev==rear) { rear->link=neww; rear=neww; } else if(temp==front) { neww->link=front; front=neww; } else { neww->link=temp; prev->link=neww; } printf("\nElement Inserted Successfully"); } } void delet() { if(front==NULL) { printf("\n\nSorry! Q is Empty"); } else { temp=front; front=front->link; free(temp); printf("\nElemnt Deleted Successfully"); } } void display() { if(front==NULL) { printf("\n\nSorry! Priority Q is Empty"); } else { temp=front; printf("\n\nInfo\tPrio\n"); while(temp!=NULL) { printf("\n%d\t%d",temp->info,temp->prio); temp=temp->link;

} } }

Dequeue Using linked list


// // // // // // // main.c DSQ-9 Created by Vipul Verma on 05/10/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include <stdio.h>

struct node { int data ; struct node *link ; }; struct dqueue { struct node *front ; struct node *rear ; }; void initdqueue ( struct dqueue * ) ; void addqatend ( struct dqueue *, int item ) ; void addqatbeg ( struct dqueue *, int item ) ; int delqatbeg ( struct dqueue * ) ; int delqatend ( struct dqueue * ) ; void display ( struct dqueue ) ; int count ( struct dqueue ) ; void deldqueue ( struct dqueue * ) ; void main( ) { struct dqueue dq ; int i, n ; initdqueue ( &dq ) ; addqatend ( &dq, 11 ) ; addqatbeg ( &dq, 10 ) ; addqatend ( &dq, 12 ) ; addqatbeg ( &dq, 9 ) ; addqatend ( &dq, 13 ) ; addqatbeg ( &dq, 8 ) ; addqatend ( &dq, 14 ) ; addqatbeg ( &dq, 7 ) ;

display ( dq ) ; n = count ( dq ) ; printf ( "\nTotal elements: %d", n ) ; i = delqatbeg ( &dq ) ; printf ( "\nItem extracted = %d", i ) ; i = delqatbeg ( &dq ) ; printf ( "\nItem extracted = %d", i ) ; i = delqatbeg ( &dq ) ; printf ( "\nItem extracted = %d", i ) ; i = delqatend ( &dq ) ; printf ( "\nItem extracted = %d", i ) ; display ( dq ) ; n = count ( dq ) ; printf ( "\nElements Left: %d", n ) ; deldqueue ( &dq ) ; } /* initializes elements of structure */ void initdqueue ( struct dqueue *p ) { p -> front = p -> rear = NULL ; } /* adds item at the end of dqueue */ void addqatend ( struct dqueue *p, int item ) { struct node *temp ; temp = ( struct node * ) malloc ( sizeof ( struct node ) ); temp -> data = item ; temp -> link = NULL ; if ( p -> front == NULL ) p -> front = temp ; else p -> rear -> link = temp ; p -> rear = temp ; } /* adds item at begining of dqueue */ void addqatbeg ( struct dqueue *p, int item ) { struct node *temp ; int *q ; temp = ( struct node * ) malloc ( sizeof ( struct node ) ); temp -> data = item ; temp -> link = NULL ;

if ( p -> front == NULL ) p -> front = p -> rear = temp ; else { temp -> link = p -> front ; p -> front = temp ; } } /* deletes item from begining of dqueue */ int delqatbeg ( struct dqueue *p ) { struct node *temp = p -> front ; int item ; if ( temp == NULL ) { printf ( "\nQueue is empty." ) ; return 0 ; } else { temp = p -> front ; item = temp -> data ; p -> front = temp -> link ; free ( temp ) ; if ( temp == NULL ) p -> rear = NULL ; return ( item ) ; } } /* deletes item from end of dqueue */ int delqatend ( struct dqueue *p ) { struct node *temp , *rleft, *q ; int item ; temp = p -> front ; if ( p -> rear == NULL ) { printf ( "\nQueue is empty." ) ; return 0 ; } else { while ( temp != p -> rear ) { rleft = temp ; temp = temp -> link ; } q = p -> rear ; item = q -> data ; free ( q ) ; p -> rear = rleft ;

p -> rear -> link = NULL ; if ( p -> rear == NULL ) p -> front = NULL ; return ( item ) ; } } /* displays the queue */ void display ( struct dqueue dq ) { struct node *temp = dq.front ; printf ( "\nfront -> " ) ; while ( temp != NULL ) { if ( temp -> link == NULL ) { printf ( "\t%d", temp -> data ) ; printf ( " <- rear" ) ; } else printf ( "\t%d", temp -> data ) ; temp = temp -> link ; } printf ( "\n" ) ; } /* counts the number of items in dqueue */ int count ( struct dqueue dq ) { int c = 0 ; struct node *temp = dq.front ; while ( temp != NULL ) { temp = temp -> link ; c++ ; } return c ; } /* deletes the queue */ void deldqueue ( struct dqueue *p ) { struct node *temp ; if ( p -> front == NULL ) return ; while ( p -> front != NULL ) { temp = p -> front ; p -> front = p -> front -> link ; free ( temp ) ;

} }

front ->

10

11

12

13

14 <- rear

Total elements: 8 Item extracted = 7 Item extracted = 8 Item extracted = 9 Item extracted = 14 front -> 10 11 Elements Left: 4

12

13 <- rear

1.Binary Search trees


#include <stdio.h> typedef struct node { struct node *left; struct node *right; int data; }node; node *stack[50]; node *stack2[50]; int top = -1, top2 = -1; node *insert(node*, int); void inorder(node*); void postorder(node*); void preorder(node*); node *search(node*, int); node *del(node*, int); node *pop(); int main(void) { node *r = NULL; int choice, item; node *x, *searched; do { printf("\nInorder : "); inorder(r); printf("\nPostorder : "); postorder(r); printf("\nPreorder : "); preorder(r); printf("\n\t===MENU===\n\t1. Insert\n\t2. Delete\n\t3. Search\n\t4. Exit\n");

printf("Enter choice : "); scanf("%d", &choice); switch(choice) { case 1: printf("Enter the item : "); scanf("%d", &item); r = insert(r, item); break; case 2: printf("Enter the node to delete : "); scanf("%d", &item); r = del(r, item); break; case 3: printf("Enter the node to search : "); scanf("%d", &item); r = search(r, item); break; case 4: break; } }while(choice != 4); } node *insert(node *root, int item) { node *prev = root; node *temp = root; if(root == NULL) { root = malloc(sizeof(node)); root->data = item; root->left = root->right = NULL; } else { while(temp != NULL) { if(item > temp->data) { prev = temp; temp = temp->right; } else { prev = temp; temp = temp->left; } } temp = malloc(sizeof(node)); temp->data = item; temp->left = temp->right = NULL; if(item >= prev->data)

prev->right = temp; else prev->left = temp; } return root; } void inorder(node *root) { node *temp = root; while((top >= 0) || (temp != NULL)) { if(temp != NULL) { stack[++top] = temp; temp = temp->left; } else { temp = stack[top--]; printf("%d\t", temp->data); temp = temp->right; } } } node *pop() { if(top == -1) printf("Cannot pop.\n"); else return stack[top--]; } void postorder(node *root) { node *temp; if(root != NULL) stack[++top] = root; while(top >= 0) { temp = pop(); stack2[++top2] = temp; if(temp->left != NULL) stack[++top] = temp->left; if(temp->right != NULL) stack[++top] = temp->right; } while(top2 != -1) { temp = stack2[top2--]; printf("%d\t", temp->data); } }

void preorder(node *root) { node *temp; if(root != NULL) stack[++top] = root; while(top >= 0) { temp = pop(); printf("%d\t", temp->data); if(temp->right != NULL) stack[++top] = temp->right; if(temp->left != NULL) stack[++top] = temp->left; } } node *del(node *root, int item) { int flag = 0; node *temp = root, *suc, *subtree; node *prev = NULL; while((flag == 0) && temp != NULL) { if(item < temp->data) { prev = temp; temp = temp->left; } else if(item > temp->data) { prev = temp; temp = temp->right; } else flag = 1; } if(flag == 0) printf("Item not found\n"); else { if((temp->left) != NULL && (temp->right) != NULL) { suc = temp->right; prev = temp; while((suc->left) != NULL) { prev = suc; suc = suc->left; } temp->data = suc->data; temp = suc; } subtree = temp->left; if(subtree == 0) subtree = temp->right;

if(prev == 0) root = subtree; else if(prev->left == temp) prev->left = subtree; else prev->right = subtree; free(temp); } return root; } node *search(node *root, int item) { int flag = 0; node *temp = root; node *prev = NULL; while((flag == 0) && temp != NULL) { if(item < temp->data) { prev = temp; temp = temp->left; } else if(item > temp->data) { prev = temp; temp = temp->right; } else { flag = 1; printf("Node found\n"); if(prev != NULL) printf("Its parent is %d\n", prev->data); else printf("It is the root\n"); } } if(flag == 0) printf("Item not found\n"); return root; } Inorder : Postorder : Preorder : ===MENU=== 1. Insert 2. Delete 3. Search 4. Exit Enter choice : 1 Enter the item : 25

Inorder : 25 Postorder : 25 Preorder : 25 ===MENU=== 1. Insert 2. Delete 3. Search 4. Exit Enter choice : 1 Enter the item : 10 Inorder : 10 25 Postorder : 10 25 Preorder : 25 10 ===MENU=== 1. Insert 2. Delete 3. Search 4. Exit Enter choice : 1 Enter the item : 30 Inorder : 10 25 30 Postorder : 10 30 25 Preorder : 25 10 30 ===MENU=== 1. Insert 2. Delete 3. Search 4. Exit Enter choice : 2 Enter the node to delete : 10 Inorder : 25 30 Postorder : 30 25 Preorder : 25 30 ===MENU=== 1. Insert 2. Delete 3. Search 4. Exit Enter choice : 3 Enter the node to search : 30 Node found Its parent is 25 Inorder : 25 30 Postorder : 30 25 Preorder : 25 30 ===MENU=== 1. Insert 2. Delete 3. Search 4. Exit

Enter choice : 4

Recursive BST
// // main.c // TREES-1 // // Created by Vipul Verma on 05/10/13. // Copyright (c) 2013 Vipul Verma. All rights reserved. // #include<stdio.h> #include<stdlib.h> typedef struct treeNode { int data; struct treeNode *left; struct treeNode *right; }treeNode; treeNode* FindMin(treeNode *node) { if(node==NULL) { /* There is no element in the tree */ return NULL; } if(node->left) /* Go to the left sub tree to find the min element */ return FindMin(node->left); else return node; } treeNode* FindMax(treeNode *node) { if(node==NULL) { /* There is no element in the tree */ return NULL; } if(node->right) /* Go to the left sub tree to find the min element */ return FindMax(node->right); else return node; } treeNode * Insert(treeNode *node,int data) { if(node==NULL) { treeNode *temp; temp = (treeNode *)malloc(sizeof(treeNode));

temp -> data = data; temp -> left = temp -> right = NULL; return temp; } if(data >(node->data)) { node->right = Insert(node->right,data); } else if(data < (node->data)) { node->left = Insert(node->left,data); } /* Else there is nothing to do as the data is already in the tree. */ return node; } treeNode * Delete(treeNode *node, int data) { treeNode *temp; if(node==NULL) { printf("Element Not Found"); } else if(data < node->data) { node->left = Delete(node->left, data); } else if(data > node->data) { node->right = Delete(node->right, data); } else { /* Now We can delete this node and replace with either minimum element in the right sub tree or maximum element in the left subtree */ if(node->right && node->left) { /* Here we will replace with minimum element in the right sub tree */ temp = FindMin(node->right); node -> data = temp->data; /* As we replaced it with some other node, we have to delete that node */ node -> right = Delete(node->right,temp->data); } else { /* If there is only one or zero children then we can directly remove it from the tree and connect its parent to its child */ temp = node; if(node->left == NULL) node = node->right; else if(node->right == NULL) node = node->left; free(temp); /* temp is longer required */

} } return node; } treeNode * Find(treeNode *node, int data) { if(node==NULL) { /* Element is not found */ return NULL; } if(data > node->data) { /* Search in the right sub tree. */ return Find(node->right,data); } else if(data < node->data) { /* Search in the left sub tree. */ return Find(node->left,data); } else { /* Element Found */ return node; } } void PrintInorder(treeNode *node) { if(node==NULL) { return; } PrintInorder(node->left); printf("%d ",node->data); PrintInorder(node->right); } void PrintPreorder(treeNode *node) { if(node==NULL) { return; } printf("%d ",node->data); PrintPreorder(node->left); PrintPreorder(node->right); } void PrintPostorder(treeNode *node) { if(node==NULL)

{ return; } PrintPostorder(node->left); PrintPostorder(node->right); printf("%d ",node->data); } int main() { treeNode *p=NULL,*find=NULL; int n,num; int choice; printf("\n1.Insert\n2.Inorder\n3.Preorder\n4.Postorder\n5.Search\n6.Delete\n7.Exit:"); scanf("%d",&choice); while(choice!=7) { switch(choice) { case 1: printf("\nEnter the number of values to be entered:"); scanf("%d",&n); while(n--) { printf("\nEnter the value:"); scanf("%d",&num); p=Insert(p,num); } break; case 2: PrintInorder(p); break; case 3: PrintPreorder(p); break; case 4: PrintPostorder(p); break; case 5: printf("\nEnter the number to be searched:"); scanf("%d",&num); find=Find(p,num); if(find!=NULL) { printf("Number found"); } else printf("Number not found"); break; case 6: printf("\nEnter the number to be Deleted:"); scanf("%d",&num); p=Delete(p,num); break;

case 7: exit(0); break; } printf("\n1.Insert\n2.Inorder\n3.Preorder\n4.Postorder\n5.Search\n6.Delete\n7.Exit:"); scanf("%d",&choice); } return 0; } 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:1 Enter the number of values to be entered:5 Enter the value:5 Enter the value:1 Enter the value:6 Enter the value:2 Enter the value:7 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:2 12567 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:3 51267 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete

7.Exit:4 21765 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:5 Enter the number to be searched:7 Number found 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:5 Enter the number to be searched:11 Number not found 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:6 Enter the number to be Deleted:2 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:2 1567 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:7

AVL TREES RECURSIVE

// // // // // // //

main.c AVL Created by Vipul Verma on 06/10/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include<stdio.h> #include<stdlib.h> // An AVL tree node struct node { int key; struct node *left; struct node *right; int height; }; // A utility function to get maximum of two integers int max(int a, int b); // A utility function to get height of the tree int height(struct node *N) { if (N == NULL) return 0; return N->height; } // A utility function to get maximum of two integers int max(int a, int b) { return (a > b)? a : b; } /* Helper function that allocates a new node with the given key and NULL left and right pointers. */ struct node* newNode(int key) { struct node* node = (struct node*) malloc(sizeof(struct node)); node->key = key; node->left = NULL; node->right = NULL; node->height = 0; // new node is initially added at leaf return(node); } // A utility function to right rotate subtree rooted with y // See the diagram given above. struct node *rightRotate(struct node *y) { struct node *x = y->left; struct node *T2 = x->right;

// Perform rotation x->right = y; y->left = T2; // Update heights y->height = max(height(y->left), height(y->right))+1; x->height = max(height(x->left), height(x->right))+1; // Return new root return x; } // A utility function to left rotate subtree rooted with x // See the diagram given above. struct node *leftRotate(struct node *x) { struct node *y = x->right; struct node *T2 = y->left; // Perform rotation y->left = x; x->right = T2; // Update heights x->height = max(height(x->left), height(x->right))+1; y->height = max(height(y->left), height(y->right))+1; // Return new root return y; } // Get Balance factor of node N int getBalance(struct node *N) { if (N == NULL) return 0; return height(N->left) - height(N->right); } struct node* insert(struct node* node, int key) { /* 1. Perform the normal BST rotation */ if (node == NULL) return(newNode(key)); if (key < node->key) node->left = insert(node->left, key); else node->right = insert(node->right, key); /* 2. Update height of this ancestor node */ node->height = max(height(node->left), height(node->right)) + 1; /* 3. Get the balance factor of this ancestor node to check whether this node became unbalanced */

int balance = getBalance(node); // If this node becomes unbalanced, then there are 4 cases // Left Left Case if (balance > 1 && key < node->left->key) return rightRotate(node); // Right Right Case if (balance < -1 && key > node->right->key) return leftRotate(node); // Left Right Case if (balance > 1 && key > node->left->key) { node->left = leftRotate(node->left); return rightRotate(node); } // Right Left Case if (balance < -1 && key < node->right->key) { node->right = rightRotate(node->right); return leftRotate(node); } /* return the (unchanged) node pointer */ return node; } struct node * Find( struct node *node, int data) { if(node==NULL) { /* Element is not found */ return NULL; } if(data > node->key) { /* Search in the right sub tree. */ return Find(node->right,data); } else if(data < node->key) { /* Search in the left sub tree. */ return Find(node->left,data); } else { /* Element Found */ return node; } } /* Given a non-empty binary search tree, return the node with minimum key value found in that tree. Note that the entire tree does not need to be searched. */

struct node * minValueNode(struct node* node) { struct node* current = node; /* loop down to find the leftmost leaf */ while (current->left != NULL) current = current->left; return current; } struct node* deleteNode(struct node* root, int key) { // STEP 1: PERFORM STANDARD BST DELETE if (root == NULL) return root; // If the key to be deleted is smaller than the root's key, // then it lies in left subtree if ( key < root->key ) root->left = deleteNode(root->left, key); // If the key to be deleted is greater than the root's key, // then it lies in right subtree else if( key > root->key ) root->right = deleteNode(root->right, key); // if key is same as root's key, then This is the node // to be deleted else { // node with only one child or no child if( (root->left == NULL) || (root->right == NULL) ) { struct node *temp = root->left ? root->left : root->right; // No child case if(temp == NULL) { temp = root; root = NULL; } else // One child case *root = *temp; // Copy the contents of the non-empty child free(temp); } else { // node with two children: Get the inorder successor (smallest // in the right subtree) struct node* temp = minValueNode(root->right); // Copy the inorder successor's data to this node

root->key = temp->key; // Delete the inorder successor root->right = deleteNode(root->right, temp->key); } } // If the tree had only one node then return if (root == NULL) return root; // STEP 2: UPDATE HEIGHT OF THE CURRENT NODE root->height = max(height(root->left), height(root->right)) + 1; // STEP 3: GET THE BALANCE FACTOR OF THIS NODE (to check whether // this node became unbalanced) int balance = getBalance(root); // If this node becomes unbalanced, then there are 4 cases // Left Left Case if (balance > 1 && getBalance(root->left) >= 0) return rightRotate(root); // Left Right Case if (balance > 1 && getBalance(root->left) < 0) { root->left = leftRotate(root->left); return rightRotate(root); } // Right Right Case if (balance < -1 && getBalance(root->right) <= 0) return leftRotate(root); // Right Left Case if (balance < -1 && getBalance(root->right) > 0) { root->right = rightRotate(root->right); return leftRotate(root); } return root; } void preorder(struct node *root) { if(root != NULL) { printf(" %d ", root->key); preorder(root->left); preorder(root->right); } } void inorder(struct node *root) { if(root==NULL)

return; else { inorder(root->left); printf(" %d ",root->key); inorder(root->right); } } void postorder(struct node *root) { if(root==NULL) return; else { postorder(root->left); postorder(root->right); printf(" %d ",root->key); } } int main() { struct node *p=NULL,*find; int n,num; int choice; printf("\n1.Insert\n2.Inorder\n3.Preorder\n4.Postorder\n5.Search\n6.Delete\n7.Exit:"); scanf("%d",&choice); while(choice!=7) { switch(choice) { case 1: printf("\nEnter the number of values to be entered:"); scanf("%d",&n); while(n--) { printf("\nEnter the value:"); scanf("%d",&num); p=insert(p,num); } break; case 2: inorder(p); break; case 3: preorder(p); break; case 4: postorder(p); break; case 5: printf("\nEnter the number to be searched:"); scanf("%d",&num); find=Find(p,num);

if(find!=NULL) { printf("Number found"); } else printf("Number not found"); break; case 6: printf("\nEnter the number to be Deleted:"); scanf("%d",&num); p=deleteNode(p,num); break; case 7: exit(0); break; } printf("\n1.Insert\n2.Inorder\n3.Preorder\n4.Postorder\n5.Search\n6.Delete\n7.Exit:"); scanf("%d",&choice); } return 0; } 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:1 Enter the number of values to be entered:5 Enter the value:6 Enter the value:5 Enter the value:4 Enter the value:3 Enter the value:2 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:2 2 3 4 5 6 1.Insert 2.Inorder

3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:3 5 4 3 2 6 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:4 2 3 4 6 5 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:5 Enter the number to be searched:3 Number found 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:6 Enter the number to be Deleted:6 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:2 2 3 4 5 1.Insert 2.Inorder 3.Preorder 4.Postorder 5.Search 6.Delete 7.Exit:7

Threaded Tress Recursive


#include <stdio.h>

#include <stdlib.h> enum marker { CHILD, THREAD }; struct tbstNode { int data; struct tbstNode *link[2]; int marker[2]; }; struct tbstNode *root = NULL; struct tbstNode * createNode (int data) { struct tbstNode *newNode; newNode = (struct tbstNode *)malloc(sizeof (struct tbstNode)); newNode->data = data; newNode->link[0] = newNode->link[1] = NULL; newNode->marker[0] = newNode->marker[1] = THREAD; return newNode; } void insertion(int data) { struct tbstNode *parent, *newNode; int path; if (!root) { root = createNode(data); return; } parent = root; /* find the location to insert the new node */ while (1) { if (data == parent->data) { printf("Duplicates Not Allowed\n"); return; } path = (data > parent->data) ? 1 : 0; if (parent->marker[path] == THREAD) break; else parent = parent->link[path]; } /* * newnode's left points to predecessor and * right to successor */ newNode = createNode(data); newNode->link[path] = parent->link[path]; parent->marker[path] = CHILD; newNode->link[!path] = parent; parent->link[path] = newNode;

return; } void delete(int data) { struct tbstNode *current, *parent, *temp; int path; parent = root; current = root; /* search the node to delete */ while (1) { if (data == current->data) break; path = (data > current->data) ? 1 : 0; if (current->marker[path] == THREAD) { printf("Given data is not available!!\n"); return; } parent = current; current = current->link[path]; } if (current->marker[1] == THREAD) { if (current->marker[0] == CHILD) { /* node with single child */ temp = current->link[0]; while (temp->marker[1] == CHILD) { temp = temp->link[1]; } temp->link[1] = current->link[1]; if (current == root) { root = current->link[0]; } else { parent->link[path] = current->link[0]; } } else { /* deleting leaf node */ if (current == root) { root = NULL; } else { parent->link[path] = current->link[path]; parent->marker[path] = THREAD; } } } else { temp = current->link[1]; /* * node with two child - whose right child has * no left child */ if (temp->marker[0] == THREAD) {

temp->link[0] = current->link[0]; temp->marker[0] = current->marker[0]; if (temp->marker[0] == CHILD) { struct tbstNode *x = temp->link[0]; while (x->marker[1] == CHILD) { x = x->link[1]; } x->link[1] = temp; } if (current == root) { root = temp; } else { printf("path: %d data:%d\n", path, parent->data); parent->link[path] = temp; } } else { /* node with two child */ struct tbstNode *child; while (1) { child = temp->link[0]; if (child->marker[0] == THREAD) break; temp = child; } if (child->marker[1] == CHILD) temp->link[0] = child->link[1]; else { temp->link[0] = child; temp->marker[0] = THREAD; } child->link[0] = current->link[0]; /* update the links */ if (current->marker[0] == CHILD) { struct tbstNode *x = current->link[0]; while(x->marker[1] == CHILD) x = x->link[1]; x->link[1] = child; child->marker[0] = CHILD; } child->link[1] = current->link[1]; child->marker[1] = CHILD; if (current == root) root = child; else parent->link[path] = child; } } /* deallocation */ free(current); return;

} void traversal() { struct tbstNode *myNode; if (!root) { printf("Threaded Binary Tree Not Exists!!\n"); return; } myNode = root; while (1) { while(myNode->marker[0] == CHILD) { myNode = myNode->link[0]; } printf("%d ", myNode->data); myNode = myNode->link[1]; if (myNode) { printf("%d ", myNode->data); myNode = myNode->link[1]; } if (!myNode) break; } printf("\n"); return; } void search(int data) { struct tbstNode *myNode; int path; if (!root) { printf("Tree Not Available!!\n"); return; } myNode = root; while (1) { if (myNode->data == data) { printf("Given data present in TBST!!\n"); return; } path = (data > myNode->data) ? 1 : 0; if (myNode->marker[path] == THREAD) break; else myNode = myNode->link[path]; } printf("Given data is not present in TBST!!\n"); return; } int main () { int data, ch;

while (1) { printf("1. Insertion\t2. Deletion\n"); printf("3. Searching\t4. Traversal\n"); printf("5. Exit\nEnter your choice:"); scanf("%d", &ch); switch (ch) { case 1: printf("Enter your input data:"); scanf("%d", &data); insertion(data); break; case 2: printf("Enter your input data:"); scanf("%d", &data); delete(data); break; case 3: printf("Enter your input data:"); scanf("%d", &data); search(data); break; case 4: traversal(); break; case 5: exit(0); default: printf("You have entered wrong option!!\n"); break; } printf("\n"); } }

1. Insertion 2. Deletion 3. Searching 4. Traversal 5. Exit Enter your choice:1 Enter your input data:5 1. Insertion 2. Deletion 3. Searching 4. Traversal 5. Exit Enter your choice:1 Enter your input data:6 1. Insertion 2. Deletion 3. Searching 4. Traversal 5. Exit Enter your choice:4 56 1. Insertion 2. Deletion

3. Searching 4. Traversal 5. Exit Enter your choice:2 Enter your input data:1 Given data is not available!! 1. Insertion 2. Deletion 3. Searching 4. Traversal 5. Exit Enter your choice:2 Enter your input data:5 1. Insertion 2. Deletion 3. Searching 4. Traversal 5. Exit Enter your choice:4 6 1. Insertion 2. Deletion 3. Searching 4. Traversal 5. Exit Enter your choice:3 Enter your input data:6 Given data present in TBST!! 1. Insertion 2. Deletion 3. Searching 4. Traversal 5. Exit Enter your choice:5

// Linear Search
// // // // // // // main.c search Created by Vipul Verma on 06/09/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include <stdio.h> int main(int argc, const char * argv[]) { int a[20],n,i; printf("Enter the number of elements you want to enter:"); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter elements %d:",i); scanf("%d",&a[i]); } for(i=0;i<n;i++)

{ printf("\nElement %d is:%d",i,a[i]); } printf("\n"); int elem,flag=0; printf("Enter the element you want to search:"); scanf("%d",&elem); for(i=0;i<n;i++) { if(elem==a[i]) { printf("%d found at position:%d",a[i],i); flag=1; break; } } if(flag==0) { printf("\nElement not found"); } return 0; }

// Binary search

// // // // // // //

main.c binarysearch Created by Vipul Verma on 06/09/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include<stdio.h> int main(){ int a[10],i,n,m,c=0,l,u,mid; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements in ascending order: "); for(i=0;i<n;i++){ scanf("%d",&a[i]); } printf("Enter the number to be search: "); scanf("%d",&m); l=0,u=n-1; while(l<=u){ mid=(l+u)/2; if(m==a[mid]){ c=1; break; } else if(m<a[mid]){ u=mid-1; } else l=mid+1; } if(c==0) printf("The number is not found."); else printf("The number is found."); return 0; }

// Selection sort
// // // // // // // main.c selsort Created by Vipul Verma on 06/09/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include<stdio.h> int main(){ int s,i,j,temp,a[20]; printf("Enter total elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]);

for(i=0;i<s;i++){ for(j=i+1;j<s;j++){ if(a[i]>a[j]){ temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("After sorting is: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; }

// Bubble sort
// // // // // // // main.c bubblesort Created by Vipul Verma on 06/09/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include<stdio.h> int main(){

int s,temp,i,j,a[20]; printf("Enter total numbers of elements: "); scanf("%d",&s); printf("Enter %d elements: ",s); for(i=0;i<s;i++) scanf("%d",&a[i]); //Bubble sorting algorithm for(i=s-2;i>=0;i--){ for(j=0;j<=i;j++){ if(a[j]>a[j+1]){ temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("After sorting: "); for(i=0;i<s;i++) printf(" %d",a[i]); return 0; }

// Insertion sort
//

// // // // // //

main.c Inssort Created by Vipul Verma on 06/09/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include<stdio.h> int main() { int A[20], N, Temp, i, j; printf("\n\n\t ENTER THE NUMBER OF TERMS...: "); scanf("%d", &N); printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:"); for(i=0; i<N; i++) { scanf("\n\t\t%d", &A[i]); } for(i=1; i<N; i++) { Temp = A[i]; j = i-1; while(Temp<A[j] && j>=0) { A[j+1] = A[j]; j = j-1; } A[j+1] = Temp; } printf("\n\tTHE ASCENDING ORDER LIST IS...:\n"); for(i=0; i<N; i++) printf("\n\t\t\t%d", A[i]); return 0; }

//Shell sort
// // // // // // // main.c shell Created by Vipul Verma on 18/10/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include<stdio.h> int main() { int arr[30]; int i,j,k,tmp,num; printf("Enter total no. of elements : "); scanf("%d", &num); for(k=0; k<num; k++) { printf("\nEnter %d number : ",k+1); scanf("%d",&arr[k]); } for(i=num/2; i>0; i=i/2) { for(j=i; j<num; j++) { for(k=j-i; k>=0; k=k-i) { if(arr[k+i]>=arr[k]) break;

else { tmp=arr[k]; arr[k]=arr[k+i]; arr[k+i]=tmp; } } } } printf("\t**** Shell Sorting ****\n"); for(k=0; k<num; k++) printf("%d\t",arr[k]); return 0; }

// HEAP SORT
// // // // // // // main.c heap Created by Vipul Verma on 18/10/13. Copyright (c) 2013 Vipul Verma. All rights reserved.

#include<stdio.h> void manage(int *, int); void heapsort(int *, int, int); int main() { int arr[20];

int i,j,size,tmp; printf("\n\t------- Heap sorting method -------\n\n"); printf("Enter the number of elements to sort : "); scanf("%d",&size); for(i=1; i<=size; i++) { printf("Enter %d element : ",i); scanf("%d",&arr[i]); manage(arr,i); } j=size; for(i=1; i<=j; i++) { tmp=arr[1]; arr[1]=arr[size]; arr[size]=tmp; size--; heapsort(arr,1,size); } printf("\n\t------- Heap sorted elements -------\n\n"); size=j; for(i=1; i<=size; i++) printf("%d ",arr[i]); return 0; }

void manage(int *arr, int i) { int tmp; tmp=arr[i]; while((i>1)&&(arr[i/2]<tmp)) { arr[i]=arr[i/2]; i=i/2; } arr[i]=tmp; }

void heapsort(int *arr, int i, int size) { int tmp,j; tmp=arr[i]; j=i*2; while(j<=size) { if((j<size)&&(arr[j]<arr[j+1])) j++; if(arr[j]<arr[j/2]) break; arr[j/2]=arr[j]; j=j*2; } arr[j/2]=tmp;

Вам также может понравиться