Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
60 views

Singly Linked List

The document describes an implementation of an expression tree for a postfix expression. It defines a node structure for a binary tree with left, right child pointers and data. It includes functions to create the tree by parsing the postfix expression, and to perform inorder, preorder and postorder traversals of the tree. Key nodes are pushed onto a stack as they are encountered in the expression and popped off to connect nodes based on operator precedence.

Uploaded by

Manukumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Singly Linked List

The document describes an implementation of an expression tree for a postfix expression. It defines a node structure for a binary tree with left, right child pointers and data. It includes functions to create the tree by parsing the postfix expression, and to perform inorder, preorder and postorder traversals of the tree. Key nodes are pushed onto a stack as they are encountered in the expression and popped off to connect nodes based on operator precedence.

Uploaded by

Manukumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

SINGLY LINKED LIST

#include<stdio.h> #include<conio.h> #include<alloc.h> #define NULL 0 struct info { int data; struct info *next; }; struct info *head,*temp,*disp; void additem(); void delitem(); void display(); int size(); void search(); void main() { int choice; clrscr(); while(1) { printf("\n1.Add records"); printf("\n2.Delete records"); printf("\n3.Display records"); printf("\n4.Count no. of items in the list"); printf("\n5.Searching an item in the list"); printf("\n6.Exit"); printf("\nEnter your choice:"); scanf("%d",&choice); fflush(stdin); switch(choice) { case 1: additem(); break; case 2: delitem(); break; case 3: display(); break; case 4: printf("\nThe size of the list is %d",size()); break; case 5: search(); break; case 6: exit(0); 1

} } } void additem() { struct info *add; char proceed='y'; while(toupper(proceed)=='Y') { add=(struct info*)malloc(sizeof(struct info)); printf("Enter data:"); scanf("%d",&add->data); fflush(stdin); if(head==NULL) { head=add; add->next=NULL; temp=add; } else { temp->next=add; add->next=NULL; temp=add; } printf("\nWant to proceed y/n"); proceed=getchar(); fflush(stdin); } } void delitem() { struct info *curr,*prev; int tdata; if(head==NULL) { printf("\nNo records to delete"); return; } printf("\nEnter the data to delete"); scanf("%d",&tdata); fflush(stdin); prev=curr=head; while((curr!=NULL)&&(curr->data!=tdata)) { prev=curr; curr=curr->next; } if(curr==NULL) { 2

printf("\nData not found"); return; } if(curr==head) head=head->next; else { /*for inbetween element deletion*/ prev->next=curr->next; /*for the last element deletion*/ if(curr->next==NULL) temp=prev; } free(curr); } void display() { if(head==NULL) { printf("\nNo data to display"); return; } for(disp=head;disp!=NULL;disp=disp->next) { printf("Data->%d",disp->data); } } int size() { int count=0; if(head==NULL) return count; for(disp=head;disp!=NULL;disp=disp->next) count++; return count; } void search() { int titem,found=0; if(head==NULL) { printf("\nNo data in the list"); return; } printf("\Enter the no. to search:"); scanf("%d",&titem); for(disp=head;disp!=NULL&&found==0;disp=disp->next) { if(disp->data==titem) found=1; 3

} if(found==0) printf("\nSearch no. is not present in the list"); else printf("\nSearch no. is present in the list"); return; }
DOUBLY LINKED LIST

#include<stdio.h> #include<conio.h> #include<alloc.h> #define NULL 0 struct info { int data; struct info *next; struct info *prev; }; struct info *head,*temp,*disp; void additem(); void delitem(); void display(); int size(); void search(); void main() { int choice; clrscr(); while(1) { printf("\n1.Add records"); printf("\n2.Delete records"); printf("\n3.Display records"); printf("\n4.Count no. of items in the list"); printf("\n5.Searching an item in the list"); printf("\n6.Exit"); printf("\nEnter your choice:"); scanf("%d",&choice); fflush(stdin); switch(choice) { case 1: additem(); break; case 2: delitem(); break; case 3: display(); break; 4

case 4: printf("\nThe size of the list is %d",size()); break; case 5: search(); break; case 6: exit(0); } } } void additem() { struct info *add; char proceed='y'; while(toupper(proceed)=='Y') { add=(struct info*)malloc(sizeof(struct info)); printf("Enter data:"); scanf("%d",&add->data); fflush(stdin); if(head==NULL) { head=add; add->next=NULL; add->prev=NULL; temp=add; } else { temp->next=add; add->prev=temp; add->next=NULL; temp=add; } printf("\nWant to proceed y/n"); proceed=getchar(); fflush(stdin); } } void delitem() { int x; struct info *p;; if(head==NULL) { printf("\nNo items in the list"); return; } printf("\nEnter the data to delete"); 5

scanf("%d",&x); p=(struct info *)malloc(sizeof(struct info)); p=head->next; if(head->data==x) { head=head->next; return; } while(p) { if(p->data==x) { p->prev->next=p->next; if(p->next!=NULL) p->next->prev=p->prev; else temp=p->prev; return; } else { p=p->next; } } printf("\nInvalid input"); } void display() { if(head==NULL) { printf("\nNo data to display"); return; } printf("\nFrom forward direction\n"); for(disp=head;disp!=NULL;disp=disp->next) { printf("Data->%d",disp->data); } printf("\nFrom backward direction\n"); for(disp=temp;disp!=NULL;disp=disp->prev) { printf("Data->%d",disp->data); } } int size() { int count=0; if(head==NULL) return count; for(disp=head;disp!=NULL;disp=disp->next) 6

count++; return count; } void search() { int titem,found=0; if(head==NULL) { printf("\nNo data in the list"); return; } printf("\Enter the no. to search:"); scanf("%d",&titem); for(disp=head;disp!=NULL&&found==0;disp=disp->next) { if(disp->data==titem) found=1; } if(found==0) printf("\nSearch no. is not present in the list"); else printf("\nSearch no. is present in the list"); return; }
POLYNOMIAL ADDITION

#include<stdio.h> #include<conio.h> struct polynomial { int coff; int pow; struct polynomial *link; }*ptr,*start1,*node,*start2,*start3,*ptr1,*ptr2; typedef struct polynomial pnl; int temp1,temp2; void main() { void create(void); void prnt(void); void suml(void); void sort(void); clrscr(); printf("Enrter the elements of the first polynomial :"); node = (pnl *) malloc(sizeof (pnl)); start1=node; if (start1==NULL) { printf(" Unable to create memory."); getch(); exit(); 7

} create(); printf("Enter the elements of the second poly :"); node = (pnl *) malloc(sizeof (pnl)); start2=node; if (start2==NULL) { printf("Unable to create memory."); getch(); exit(); } create(); clrscr(); printf("The elements of the poly first are :"); ptr=start1; prnt(); printf("The elements of the poly second are :"); ptr=start2; prnt(); printf("The first sorted list is :"); ptr=start1; sort(); ptr=start1; prnt(); printf("The second sorted list is :"); ptr=start2; sort(); ptr=start2; prnt(); printf("The sum of the two lists are :"); suml(); ptr=start3; prnt(); getch(); } void create() { char ch; while(1) { printf(" Enter the coff and pow :"); scanf("%d%d",&node->coff,&node->pow); if (node->pow==0 ) { ptr=node; node=(pnl *)malloc(sizeof(pnl)); node=NULL; ptr->link=node; break; } 8

printf("Do u want enter more coff ?(y/n)"); fflush(stdin); scanf("%c",&ch); if (ch=='n' ) { ptr=node; node=(pnl *)malloc(sizeof(pnl)); node=NULL; ptr->link=node; break; } ptr=node; node=(pnl *)malloc(sizeof(pnl)); ptr->link=node; } } void prnt() { int i=1; while(ptr!=NULL ) { if(i!=1) printf("+ "); printf(" %dx^%d\n ",ptr->coff,ptr->pow); ptr=ptr->link; i++; } } void sort() { for(;ptr->coff!=NULL;ptr=ptr->link) for(ptr2=ptr->link;ptr2->coff!=NULL;ptr2=ptr2->link) { if(ptr->pow>ptr2->pow) { temp1=ptr->coff; temp2=ptr->pow; ptr->coff=ptr2->coff; ptr->pow=ptr2->pow; ptr2->coff=temp1; ptr2->pow=temp2; } } } void suml() { node=(pnl *)malloc (sizeof(pnl)); start3=node; ptr1=start1; 9

ptr2=start2; while(ptr1!=NULL && ptr2!=NULL) { ptr=node; if (ptr1->pow > ptr2->pow ) { node->coff=ptr2->coff; node->pow=ptr2->pow; ptr2=ptr2->link; //update ptr list B } else if ( ptr1->pow < ptr2->pow ) { node->coff=ptr1->coff; node->pow=ptr1->pow; ptr1=ptr1->link; //update ptr list A } else { node->coff=ptr2->coff+ptr1->coff; node->pow=ptr2->pow; ptr1=ptr1->link; //update ptr list A ptr2=ptr2->link; //update ptr list B } node=(pnl *)malloc (sizeof(pnl)); ptr->link=node; //update ptr list C }//end of while if (ptr1==NULL) //end of list A { while(ptr2!=NULL) { node->coff=ptr2->coff; node->pow=ptr2->pow; ptr2=ptr2->link; //update ptr list B ptr=node; node=(pnl *)malloc (sizeof(pnl)); ptr->link=node; //update ptr list C } } else if (ptr2==NULL) //end of list B { while(ptr1!=NULL) { node->coff=ptr1->coff; node->pow=ptr1->pow; ptr1=ptr1->link; //update ptr list B ptr=node; node=(pnl *)malloc (sizeof(pnl)); ptr->link=node; //update ptr list C } } 10

node=NULL; ptr->link=node; }
CONVERT INFIX TO POSTFIX EXPRESSION

#include <stdio.h> #include <conio.h> #include <string.h> #include <ctype.h> char stack[100]; int top=0; char exp[100]; struct table { char s[2]; int isp; int icp; }pr[7]; int isp(char c) { int i; for(i=0;i<=6;i++) if(pr[i].s[0]==c) return(pr[i].isp); return 0; } int icp(char c) { int i; for(i=0;i<=6;i++) if(pr[i].s[0]==c) return(pr[i].icp); return 0; } void main() { int i; clrscr(); strcpy(pr[0].s,"^"); pr[0].isp=3; pr[0].icp=4; strcpy(pr[1].s,"*"); pr[1].isp=2; pr[1].icp=2; strcpy(pr[2].s,"/"); pr[2].isp=2; pr[2].icp=2; strcpy(pr[3].s,"+"); pr[3].isp=1; pr[3].icp=1; strcpy(pr[4].s,"-"); 11

pr[4].isp=1; pr[4].icp=1; strcpy(pr[5].s,"("); pr[5].isp=0; pr[5].icp=4; strcpy(pr[6].s,"="); pr[6].isp=-1; pr[6].icp=0; clrscr(); stack[top]='='; printf("enter the infix expression"); gets(exp); i=0; printf("the postfix expression is ") while(i<strlen(exp)) { if(isalpha(exp[i])==0) { if(exp[i]==')') { while(stack[top]!='(') { printf("%c",stack[top]); top--; } top--; } else { while(isp(stack[top])>=icp(exp[i])) { printf("%c",stack[top]); top--; } top++; stack[top]=exp[i]; } } else printf("%c",exp[i]); i++; } while(top>0) { printf("%c",stack[top]); top--; } getch(); }

12

Implement an expression tree

#include<stdio.h> #include<conio.h> #include<alloc.h> #include<ctype.h> #define size 20 typedef struct node { char data; struct node *left; struct node *right; }btree; btree *stack[size]; int top; void main() { btree *root; char exp[80]; btree *create(char exp[80]); void inorder(btree *root); void preorder(btree *root); void postorder(btree *root); clrscr(); printf("\nEnter the postfix expression:"); scanf("%s",exp); top=-1; root=create(exp); printf("\nThe tree is created"); printf("\nThe inorder traversal is :\n"); inorder(root); printf("\nThe preorder traversal is :\n"); preorder(root); printf("\nThe postorder traversal is :\n"); postorder(root); getch(); } btree *create(char exp[]) { btree *temp; int pos; char ch; void push(btree *); btree *pop(); pos=0; ch=exp[pos]; while(ch!='\0') { temp=(btree*)malloc(sizeof(btree)); temp->left=temp->right=NULL; temp->data=ch; 13

if(isalpha(ch)) push(temp); else if(ch=='+'||ch=='-'||ch=='*'||ch=='/') { temp->right=pop(); temp->left=pop(); push(temp); } else printf("Invalid character\n"); pos++; ch=exp[pos]; } temp=pop(); return(temp); } void push(btree *node) { if(top+1>=size) printf("\nstack is Full"); top++; stack[top]=node; } btree *pop() { btree *node; if(top==-1) printf("\nStack is empty"); node=stack[top]; top--; return(node); } void inorder(btree *root) { btree *temp; temp=root; if(temp!=NULL) { inorder(temp->left); printf("%c",temp->data); inorder(temp->right); } } void preorder(btree *root) { btree *temp; temp=root; if(temp!=NULL) { printf("%c",temp->data); 14

preorder(temp->left); preorder(temp->right); } } void postorder(btree *root) { btree *temp; temp=root; if(temp!=NULL) { postorder(temp->left); postorder(temp->right); printf("%c",temp->data); } }
Implement priority queue using heap

#include<stdio.h> #include<conio.h> #include<stdlib.h> typedef int ElementType; struct HeapStruct; typedef struct HeapStruct *PriorityQueue; PriorityQueue Initialize(int MaxElements); void Insert(ElementType X, PriorityQueue H); ElementType DeleteMin(PriorityQueue H); ElementType FindMin(PriorityQueue H); int IsEmpty(PriorityQueue H); int IsFull(PriorityQueue H); void display(PriorityQueue H); int MinPQSize ; int MinData struct HeapStruct { int Capacity; int Size; ElementType *Elements; }; PriorityQueue Initialize(int MaxElements) { PriorityQueue H; if (MaxElements < MinPQSize) printf("Priority queue size is too small"); H =(struct HeapStruct *) malloc(sizeof ( struct HeapStruct)); if (H == NULL) ("Out of space!!!"); H->Elements =(int *) malloc((MaxElements + 1)* sizeof ( ElementType)); if (H->Elements == NULL) printf("Out of space!!!"); H->Capacity = MaxElements; H->Size = 0; H->Elements[ 0 ] = MinData; return H; } 15

void Insert(ElementType X, PriorityQueue H) { int i; if (IsFull(H)) { printf("Priority queue is full"); return; } for (i = ++H->Size; H->Elements[ i / 2 ] > X; i /= 2) H->Elements[ i ] = H->Elements[ i / 2 ]; H->Elements[ i ] = X; } ElementType DeleteMin(PriorityQueue H) { int i, Child; ElementType MinElement, LastElement; if (IsEmpty(H)) { printf("Priority queue is empty"); return H->Elements[ 0 ]; } MinElement = H->Elements[ 1 ]; LastElement = H->Elements[ H->Size-- ]; for (i = 1; i * 2 <= H->Size; i = Child) { Child = i * 2; if (Child != H->Size && H->Elements[ Child + 1 ] < H->Elements[ Child ]) Child++; if (LastElement > H->Elements[ Child ]) H->Elements[ i ] = H->Elements[ Child ]; else break; } H->Elements[ i ] = LastElement; return MinElement; } ElementType FindMin(PriorityQueue H) { if (!IsEmpty(H)) return H->Elements[ 1 ]; printf("Priority Queue is Empty"); return H->Elements[ 0 ]; } int IsEmpty(PriorityQueue H) { return H->Size == 0; } int IsFull(PriorityQueue H) { return H->Size == H->Capacity; } void display(PriorityQueue H) { int i=1;printf("the elements in the priority queue are\t"); while(i<=H->Size) { printf("%d\t",H->Elements[i]); i++; 16

} } #include<stdio.h> #include <process.h> #define MaxSize (1000) main() { PriorityQueue H; int b,a,j,n; H = Initialize(MaxSize); while(1) { printf("\nenter your choice.......1.insert 2.delete min 3.findmin 4.display 5.exit\n"); scanf("%d",&b); switch(b) { case 1: printf("enter the no."); scanf("%d",&j); Insert(j, H); break; case 4: display(H); break; case 2: n=DeleteMin(H); printf("the deleted element is %d\n",n); break; case 3: a=FindMin(H); printf("the minimum element="); printf("%d\n",a); break; case 5: exit(0);break; }}} hashing techniques #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a[125],key,size,i,h; clrscr(); printf("\n Enter the array size:"); scanf("%d",&size); printf("\n Enter the array element:"); for(i=0;i<size;i++) { 17

scanf("%d",&a[i]); } printf("Enter the key value"); scanf("%d",&key); h=key%size; while(h!=i) i++; printf("The element is %d",a[i]); getch(); }

18

You might also like