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

CONTE C ENTS

1. 1 Writ teaProgr ramusing gArraysp performin ngInsertio on, Dele etion,Searching,So ortingandTravers singOper rations. WAP Ptoperfo ormBubb bleSort. WAP Ptoperfo ormSelec ctionSort t. WAP Ptoperfo ormInsertionSort. WAP Ptoperfo ormMerg geSort. WAP Ptoperfo ormLinea arSearch. . WAP Ptoperfo ormBinar rySearch. WAP Ptoimple ementSta acksusing gArrays. WAP Ptoimple ementQu ueuesusingArrays s. WAP Ptoimple ementSin nglyLinke edLists. WAP Ptoimple ementDo oublyLink kedLists. WAP Ptoperfo ormAddit tion,Subt tractiona andMulti iplication on2 2TwoDim mensionalArray. WAP Ptoimple ementSta acksusing gLinkedLists. WAP Ptoimple ementQu ueuesusingLinked dLists.

2. 2 3. 3 4. 4 5. 5 6. 6 7. 7 8. 8 9. 9 10. 1 11. 1 12. 1

13. 1 14. 1

// Write a Prog / e gram usi ing Arra rays per rforming g Insertio I on, Dele etion, Searchin S ng, Sor rting an nd Traversi T ing Oper rations. .
#i include <s stdio.h> nt ize of arra ay in n; //si in BSearch nt h(int *a,in ITEM) nt { int beg g=0,end=n-1 1; int mid d; while( beg<=end ) { mid=(b beg+end)/2; ; if(a[m mid]==ITEM) ) return mid; n else if( a[mid] < ITEM ) i beg=mi id+1; else end=mi id-1; } return -1; } vo oid SSort( (int *a) { int i,j j,temp; for(i=0 i<n-1; i++) 0; i { for(j= =i+1; j<n; j++) { if(a[ [i]>a[j]) { temp p=a[i]; a[i]=a[j]; a[j]=temp; } } } } vo oid DEL(in *a, int ITEM) nt { int i; ; int po os=BSearch( (a, ITEM); if(pos s!=-1) {


{ for(i= =pos; i<n-1 i++) 1; a[i]=a a[i+1]; n--; f("\n Item Deleted. "); printf } else printf f("\n Item not found "); d. } vo oid INS(in *a, int ITEM) nt { int po os,i; //Find ding the pr roper posi ition for S Sorted Arr ray if(ITE EM>a[n-1]) pos=n; ; else if(ITEM<a[0 i 0]) pos=0; ; else { fo or(i=0; i<n n-1; i++) if f(a[i]<=ITE && a[i+ EM +1]>=ITEM) { pos=i+1; p break; b } } //Shif fting of el lements to Insert th element o he t. for(i= =n; i>pos; i--) a[i]=a a[i-1]; a[pos]=ITEM; n++; f("\n Item Inserted. "); printf } vo oid Displa ay(int *a) { int i; i for(i i=0; i<n; i++) i print tf(" %d",a[ [i]); } in main() nt { 100],ch,pos s,ITEM,i; int a[1 ("\n Enter the numbe of Eleme er ents: "); printf( scanf(" "%d",&n); ("\n Enter the Eleme ents: "); printf( for(i=0 i<n; i++ 0; +) scanf(" "%d",&a[i]) ); do


{ f("\n _____ __________ ___________ __________ ______ "); printf printf f("\n MENU: :\n"); printf f("\n 1. In nsert an element. ") e ); printf f("\n 2. De elete an element. ") e ); printf f("\n 3. Se earch for an element "); t. printf f("\n 4. Di isplay ele ements. "); ; printf f("\n 0. Ex xit. "); printf f("\n Your Choice: "); " scanf( ("%d",&ch); ; switch h(ch) { case 1: //INSER RT print tf("\n Ente the Ite to Inser er em rt: "); scanf f("%d",&ITE EM); t(a); SSort INS(a a,ITEM); Displ lay(a); k; break case 2: //DELET TE print tf("\n Ente the Ite to Delet er em te: "); scanf f("%d",&ITE EM); SSort t(a); DEL(a a,ITEM); Displ lay(a); k; break case 3: //SEARC CH print tf("\n Ente the Ite to Searc er em ch: "); scanf f("%d",&ITE EM); t(a); SSort pos=B BSearch(a,I ITEM); if(po os==-1) print tf("\n Item not Foun m nd. "); else tf("\n Item found at %d positi m t ion. ",pos s+1); print k; break case 4: //DISPL LAY tf("\n The Elements are: "); print SSort t(a); Displ lay(a); k; break case 0:


print tf("\n Exit ting... "); break k; } le(ch!=0); }whil } OU UTPUT:

Enter the number of Elements: 5 E Enter the Elements: 10 2 8 4 6 E __________ _ ___________ __________ __________ MENU: M 1. Insert an element 1 t. 2. Delete an element 2 t. 3. Search for an ele 3 ement. 4. Display elements. 4 y . 0. Exit. 0 Your Choic Y ce: 4 The Elemen T nts are: 2 4 6 8 10 0 __________ _ ___________ __________ __________ MENU: M 1. Insert an element 1 t. 2. Delete an element 2 t. 3. Search for an ele 3 ement. 4. Display elements. 4 y . 0. Exit. 0 Your Choic Y ce: 1 Enter the Item to In E nsert: 7 Item Inser I rted. 2 4 6 7 8 10 __________ _ ___________ __________ __________ MENU: M 1. Insert an element 1 t. 2. Delete an element 2 t. 3. Search for an ele 3 ement. 4. Display elements. 4 y . 0. Exit. 0 Your Choic Y ce: 3 Enter the Item to Se E earch: 8 Item found at 5 posi I d ition.

__________ _ ___________ __________ __________ MENU: M 1. Insert an element 1 t. 2. Delete an element 2 t. 3. Search for an ele 3 ement. 4. Display elements. 4 y . 0. Exit. 0 Your Choic Y ce: 2 Enter the Item to De E elete: 4 Item Delet I ted. 2 6 7 8 10 __________ _ ___________ __________ __________ MENU: M 1. Insert an element 1 t. 2. Delete an element 2 t. 3. Search for an ele 3 ement. 4. Display elements. 4 y . 0. Exit. 0 Your Choic Y ce: 0 Exiting... E .

// Write A Prog / e gram to perform a bub rm bble sort. r


#i include<st tdio.h> vo oid Bsort( (int *a,int n) t { in i,j,tem nt mp; fo or(i=n-1; i>=0; i--) ) fo or(j=0; j< <i; j++) if f(a[j]>a[j j+1]) { te emp=a[j+1]; a[ [j+1]=a[j]; a[ [j]=temp; } } ma ain() { in a[50],n nt n,i; pr rintf("Ent ter size of your arr f ray: "); sc canf("%d",&n); pr rintf("Ent ter element of your array: ") ts r ); fo or(i=0; i< <n; i++) sc canf("%d",&a[i]); Bs sort(a,n); ; rintf("\nE Elements: \n"); \ pr fo or(i=0; i< <n; i++) pr rintf("%d ",a[i]);

}
OUTPUT: O En nter size of your ar rray: 5 En nter eleme ents of you array: 2 8 4 6 5 ur El lements: 2 4 5 6 8

//Write A Progr / ram to perform a sele p m ection sort.


#i include<st tdio.h vo oid Ssort( (int *a,int n) t { in i,j,tem nt mp; fo or(i=0; i< <n-1; i++) fo or(j=i+1; j<n; j++) if f(a[i]>a[j j]) { te emp=a[i];; ; a[ [i]=a[j]; a[ [j]=temp; } } ma ain() { in a[50],n nt n,i; pr rintf("Ent ter size of your arr f ray: "); sc canf("%d",&n); pr rintf("Ent ter element of your array: ") ts r ); fo or(i=0; i< <n; i++) sc canf("%d",&a[i]); Ss sort(a,n); ; rintf("\nE Elements: \n"); \ pr fo or(i=0; i< <n; i++) pr rintf("%d ",a[i]); } OU UTPUT: En nter size of your ar rray: 5 En nter eleme ents of you array: 1 9 3 7 6 ur El lements: 1 3 6 7 9

//Write A Progr / ram to perform an Ins p m sert sort. r


#i include<st tdio.h> vo oid Isort( (int *a,int n) t { in i,j,tem nt mp; a[ [0]=-99; fo or(i=2; i< <n+1; i++) { te emp=a[i]; or(j=i-1; temp < a[j j]; j--) fo { [j+1]=a[j]; a[ } a[ [j+1]=temp p; } } ma ain() { in a[50],n nt n,i; pr rintf("Ent ter size of your arr f ray: "); sc canf("%d",&n); pr rintf("Ent ter element of your array: ") ts r ); fo or(i=1; i< <n+1; i++) sc canf("%d",&a[i]); Is sort(a,n); ; rintf("\nE Elements: \n"); \ pr fo or(i=1; i< <n+1; i++) pr rintf("%d ",a[i]); } OU UTPUT: En nter size of your ar rray: 5 En nter eleme ents of you array: 2 8 4 6 5 ur El lements: 2 4 5 6 8

// Write a prog / e gram to perform Merge Sort. rm e


#i include<st tdio.h> vo oid MergeS Sort(int *a int m, int *b, in n, int *c) a, nt { int i= =0,j=0,k=0; ; ( <n) while( i<m && j< { if( a[i] <= b[j] ) { c[k++ +]=a[i++]; } else { c[k++ +]=b[j++]; } } if( i< ) <m { wh hile( i<m ) c[ [k++]=a[i++ +]; } else if( j<n ) i { while(j<n) w c[k++]=a[j+ c ++]; } }

in main() nt { 50],b[50],c c[100]; int a[5 int i,m m,n; ("\n Enter the Size of Array A "); A: printf( scanf(" "%d",&m); ("\n Enter the Eleme ents (In As scending O Order) : ") ); printf( for(i=0 i<m; i++ 0; +) scanf(" "%d",&a[i]) ); ("\n Enter the Size of Array B "); B: printf( scanf(" "%d",&n); ("\n Enter the Eleme ents (In As scending O Order) : ") ); printf( for(i=0 i<n; i++ 0; +) scanf(" "%d",&b[i]) ); MergeSo ort(a,m,b,n n,c);


printf( ("\n The Me erged & So orted Array C: "); y for(i=0 i<(m+n); i++) 0; ; printf( %d",c[i] (" ]); }

OU UTPUT:

Enter the Size of Ar E rray A: 6 Enter the Elements (In Ascend E ( ding Order) : 0 2 4 6 8 10 ) Enter the Size of Ar E rray B: 5 Enter the Elements (In Ascend E ( ding Order) : 1 3 5 7 9 ) The Merged & Sorted Array C: T d 0 1 2 3 4 5 6 7 8 9 10

//Write a progr / ram to perform Linear Search. p m r h


#i include<st tdio.h> in Lsearch nt h(int *a, int n, int item) i t { nt in i; fo or(i=0; i< <n; i++) { if f(item==a[ [i]) re eturn i; } re eturn -1; } ma ain() { in a[50],n nt n,i; in item,po nt os; pr rintf("Ent ter size of your arr f ray: "); sc canf("%d",&n); pr rintf("Ent ter element of your array: ") ts r ); fo or(i=0; i< <n; i++) sc canf("%d",&a[i]); pr rintf("Ent ter item to search: "); o sc canf("%d",&item); po os=Lsearch h(a,n,item) ); if f(pos==-1) pr rintf(" NO FOUND. "); OT " el lse pr rintf(" FO OUND AT %d" (pos+1)); ", } OU UTPUT: En nter size of your ar rray: 5 En nter eleme ents of you array: 0 1 3 4 6 ur En nter item to search: 4 : FOUND AT 4 F

//Write A Progr / ram to perform Binary Search. p m y h


#i include<st tdio.h> in Bsearch nt h(int *a, int n, int item) i t { in beg,end nt d,mid; eg=0; be en nd=n-1; wh hile(beg<= =end) { id=(beg+en nd)/2; mi if f(a[mid]== =item) re eturn mid; ; el lse if(a[m mid]>item) en nd=mid-1; el lse if(a[m mid]<item) be eg=mid+1; } re eturn -1; } ma ain() { in a[50],n nt n,i; in item,po nt os; pr rintf("Ent ter size of your arr f ray: "); sc canf("%d",&n); pr rintf("Ent ter element of your array: ") ts r ); fo or(i=0; i< <n; i++) sc canf("%d",&a[i]); pr rintf("Ent ter item to search: "); o sc canf("%d",&item); po os=Bsearch h(a,n,item) ); if f(pos==-1) pr rintf(" NO FOUND. "); OT " el lse pr rintf(" FO OUND AT %d" (pos+1)); ", } OU UTPUT: En nter size of your ar rray: 6 En nter eleme ents of you array: 2 4 6 8 10 13 ur 0 En nter item to search: 10 : FOUND AT 5 F

//Write A Progr / ram to implemen Stac i nt cks usin Arrays. ng y


#i include<st tdio.h> vo oid PUSH(i int *a, int n, int *top, int I t * ITEM) { if(*to op==-1) //P Pushing Fi irst Elemen nt { 0; *top=0 a[*top p]=ITEM; } else if(*top==(n i n-1)) //Pu ushing into Filled a o array { printf f(" OVERFLO OW. DELETE SOME ELEM E MENTS. "); } else //Normal co / onditions { *top=* *top+1; a[*top p]=ITEM; } } vo oid POP(in *a, int *top) nt { if(*to op==-1) // Deleting from { printf f(" UNDERFL LOW. INSER SOME ELE RT EMENTS. "); } else { *t top=*top-1; ; pr rintf("\n POPPED... \n"); P } } vo oid Displa ay(int *a, int top) { int i= =0; if (to op!=-1) for(i= =top; i>=0; i--) ; { print tf("\n %d ",a[i]); } else printf f(" NO ELEM MENTS. \n" "); printf f("\n"); }

in main() nt { in a[5]; nt


in n=5; // nt /max size in top=-1; nt ; in ch=1,IT nt TEM; wh hile(ch!=4 4) { pri intf("\n pri intf("\n pri intf("\n pri intf("\n pri intf("\n pri intf("\n

ME ENU: "); 1. PUSH. "); . 2. POP. "); . 3. DISPLAY. "); . 4. EXIT. "); . Yo our Choice "); e:

sca anf("%d",&c ch); swi itch(ch) { se NSERT cas 1: //IN prin ntf("\n ENT TER ITEM: "); scan nf("%d",&IT TEM); PUSH H(a,n,&top, ITEM); , brea ak; cas 2: //DE se ELETE POP( (a,&top); brea ak; cas 3: //DI se ISPLAY Disp play(a,top) ); brea ak; cas 4: prin se ntf(" Exiti ing... "); } } return 0; ; }

MENU: M 1. PUSH. 1 2. POP. 2 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 1 ENTER ITEM 56 E M: MENU: M 1. PUSH. 1 2. POP. 2 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 1 ENTER ITEM 89 E M: MENU: M 1. PUSH. 1 2. POP. 2 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 3 89 8 56 5 MENU: M 1. PUSH. 1 2. POP. 2 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 2 POPPED... P MENU: M 1. PUSH. 1 2. POP. 2 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 3 56 5 MENU: M 1. PUSH. 1 2. POP. 2 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 4 Exiting... E .

// Write a Prog / e gram to impleme ment Que eues usi ing Arra ays
include <s stdio.h> #i vo oid INS(in *a, int n, int *b nt beg, int *e end, int I ITEM) { if( *be eg==-1 ) // /FIRST ELE EMENT { *be eg=0; *en nd=0; a[* *end]=ITEM; ; } else if *beg==*e f( end+1 || ( *beg==0 & *end==( && (n-1) ) ) / //OVERFLOW W { rintf("\n OVERFLOW. DELETE SOM ELEMENT O ME TS: "); pr } else S s { //Normal Situations *en nd=*end+1; a[* *end]=ITEM; ; } } vo oid DEL(in *a, int *beg, int *end) nt t { if( *b beg==-1 ) { rintf("\n UNDERFLOW, ADD SOME ELEMENTS: "); U pr } else if( *beg==* i *end ) //L LAST ELEMEN NT { *beg=-1; * *end=-1; * printf("\n Element Deleted. \n p D n"); } else { *b beg=*beg+1; ; pr rintf("\n Element De E eleted.\n") ); } } vo oid Displa ay(int *a, int beg, int end) { printf f("\n"); int i= =0; if (be eg!=-1) for(i= =beg; i<=en nd; i++) { print tf(" %d ",a[i]); } else printf f("\n NO EL LEMENTS. \n"); \ f("\n"); printf


} in main() nt { in a[5]; nt in n=5; // nt /max size nt ; in beg=-1; in end=-1; nt ; in ch=1,IT nt TEM; wh hile(ch!=4 4) { pri intf("\n pri intf("\n pri intf("\n pri intf("\n pri intf("\n pri intf("\n

ME ENU: "); 1. INSERT. "); . 2. DELETE. "); . 3. DISPLAY. "); . 4. EXIT. "); . Yo our Choice "); e:

sca anf("%d",&c ch); swi itch(ch) { se NSERT cas 1: //IN prin ntf("\n ENT TER ITEM: "); scan nf("%d",&IT TEM); INS( (a,n,&beg, &end, ITE EM); brea ak; se ELETE cas 2: //DE DEL( (a,&beg, &e end); brea ak; se ISPLAY cas 3: //DI Disp play(a,beg, ,end); brea ak; se ntf("\n Exi iting... " "); cas 4: prin } } re eturn 0; }

MENU: M 1. INSERT. 1 . 2. DELETE. 2 . 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 1 ENTER ITEM 24 E M: MENU: M 1. INSERT. 1 . 2. DELETE. 2 . 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 1 ENTER ITEM 56 E M: MENU: M 1. INSERT. 1 . 2. DELETE. 2 . 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 3 24 2 56

MENU: M 1. INSERT. 1 . 2. DELETE. 2 . 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 2 Element De E eleted. MENU: M 1. INSERT. 1 . 2. DELETE. 2 . 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 3 56 5 MENU: M 1. INSERT. 1 . 2. DELETE. 2 . 3. DISPLAY 3 Y. 4. EXIT. 4 Your Choic Y ce: 4 Exiting... E .

//Write a Progr / ram to implemen Sing i nt gly Link ked List ts.
#i include<st tdio.h> in count; // Total number of nodes in S nt n Singly Lin nked List. st truct node e { int info; stru uct node * next; }* *start,*pt tr; in INS(int ITEM, int pos) nt t t { struct node * ne t ewnode; newnod de= (struct node *) malloc ( s t sizeof(str ruct node) ) ; newnod de->info=IT TEM; newnod de->next=NU ULL; // ERR ROR HANDLIN #1 - No Memory Le NG o eft (Overf flow) if( ne ewnode == NULL ) N { print tf(" OVERFL LOW: No Me emory Left to create newnode. "); e retur 0; rn } // ERR ROR HANDLIN #2 - Po NG osition Ove Limits er if( po os<=0 || po os>count+1 ) 1 { print tf(" Positi ion entere is over limits. " ed "); retur 0; rn } // Con ndition 1: First nod of LL de if( st tart==NULL ) { start t=newnode; count t=1; } // Con ndition 2: Adding no ode to firs positio of LL st on else if( pos==1) i ) { newno ode->next=s start; start t=newnode; count t++; } // Con ndition 3: Adding no ode to any other pos sition of L LL else { int i; i ptr=s start; for(i i=2; i<=pos s-1; i++)


ptr=p ptr->next; newno ode->next = ptr->nex xt; ptr-> >next = new wnode; count t++; } n cessful return 1; //succ } in DEL(int pos) nt t { struct node *temp p; //ERROR HANDLING #1 - Unde R erflow if(star == NULL) rt ) { f(" UNDERFL LOW: No No ode left to delete. "); o printf return 0; n } //ERROR HANDLING #2 - Posi R ition Over Limits if( pos s<=0 || pos s>count ) { printf f(" Positio entered is over l on d limits. "); return 0; n }

// Cond dition 1: Deleting First Node OR Last R D F Remaining Node if( pos s==1 ) { temp=s start; start= =start->nex xt; free(t temp); count--; } // Cond dition 2: Deleting any other n D a node else { int i; ; ptr=st tart; for(i= =2; i<=pos-1; i++) ptr=pt tr->next; temp=p ptr->next; ptr->n next=temp-> >next; free(t temp); count--; } return 1; }

in TRAVERS nt SE() { rt; ptr=star // ERROR HANDLING R if(start t==NULL) { printf( Linked List Empty No Nodes to displ (" L y. s lay. "); return 0; } // TRAVE ERSING int i; "\n ["); printf(" for(i=1; i<=count; i++, ptr ; ; r=ptr->next t) { (" tr->info); printf( - %d",pt } printf(" - ]"); " return 1; 1 }

in main() nt { em,pos=1; int ite int ch,success; start=N NULL; count=0 0; do { ("\n\n MENU U:"); printf( printf( ("\n 1. Ins sert a nod de. "); printf( ("\n 2. Del lete a nod de. "); printf( ("\n 3. Dis splay the Singly Lin nked List. "); printf( ("\n 4. EXI IT. \n You Choice: "); ur scanf(" "%d",&ch); switch( (ch) { case 1: //INSERT : ("\n ENTER ITEM: "); printf( scanf(" "%d", &item m); if(star rt!=NULL) { f("\n ENTER POSITION "); R N: printf scanf( ("%d", &pos s); } s=INS(item, ,pos); success break;

case 2: //DELETE : if(star rt!=NULL) { f("\n ENTER POSITION "); R N: printf scanf( ("%d", &pos s); } s=DEL(pos); ; success break; case 3: //DISPLAY : Y printf( ("\n The No odes: \n"); success s=TRAVERSE( (); break; case 4: : printf( ("\n Exitin ng.. "); break; default printf(" INVALID CHOICE. ") t: " ); } } while(c ch!=4); //e end of do while loop p return 0; }


OUTPUT: O MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display the Singl Linked List. 3 y ly 4. EXIT. 4 Your Choic Y ce: 1 ENTER ITEM 34 E M: MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display the Singl Linked List. 3 y ly 4. EXIT. 4 Your Choic Y ce: 1 ENTER ITEM 56 E M: ENTER POSI E ITION: 2 MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display the Singl Linked List. 3 y ly 4. EXIT. 4 Your Choic Y ce: 1 ENTER ITEM 67 E M: ENTER POSI E ITION: 3 MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display the Singl Linked List. 3 y ly 4. EXIT. 4 Your Choic Y ce: 3 The Nodes: T : [ - 34 - 56 - 67 - ] 5 MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display the Singl Linked List. 3 y ly 4. EXIT. 4 Your Choic Y ce: 1 ENTER ITEM 12 E M: ENTER POSI E ITION: 1

MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display the Singl Linked List. 3 y ly 4. EXIT. 4 Your Choic Y ce: 3 The Nodes: T : [ - 12 - 34 - 56 - 67 - ] 3 6 MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display the Singl Linked List. 3 y ly 4. EXIT. 4 Your Choic Y ce: 1 ENTER ITEM 0 E M: ENTER POSI E ITION: 6 Position entered is over limi P e its. MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display the Singl Linked List. 3 y ly 4. EXIT. 4 Your Choic Y ce: 2 ENTER POSI E ITION: 3

MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display the Singl Linked List. 3 y ly 4. EXIT. 4 Your Choic Y ce: 3 The Nodes: T : [ - 12 - 34 - 67 - ] 3 MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display the Singl Linked List. 3 y ly 4. EXIT. 4 Your Choic Y ce: 4 Exiting.. E

//Write a Progr / ram to implemen Doub i nt bly Link ked List ts.
#i include <s stdlib.h> in count; nt st truct node e { int info; stru uct node* next; n stru uct node* prev; p }* *start,*pt tr; in INS( in ITEM, in pos) nt nt nt { struct node * new wnode; newnode (struct node*) ma e= alloc ( siz zeof(struc node) ); ct ; newnode e->info=ITE EM; newnode e->next=NUL LL; newnode e->prev=NUL LL; // ERRO HANDLING #1 - Ove OR G erflow? if(newn node==NULL) ) { f("\n No Me emory Left to create a node. "); t e printf return 0; n } //ERROR HANDLING #2 - Posi R ition Overl limits if( pos s<=0 || pos s>count+1 ) { f("\n Posit tion Enter red Over li imits. "); printf return 0; n } dition 1: Entering the first n E t node of LL L // Cond if(star rt==NULL) { start= =newnode; count= =1; } // Cond dition 2: Entering node to the first po E n e osition else if f(pos==1) { newnod de->next=st tart; start->prev=newn node; start= =newnode; count+ ++; } // Cond dition 3: Enterting a node to last posi E ition else if f(pos==coun nt+1) { int i; ; ptr=st tart; for(i= =2; i<=pos-1; i++)


ptr=pt tr->next; newnod de->next=pt tr->next; newnod de->prev=pt tr; ptr->n next=newnod de; count+ ++; } // Cond dition 4: Entering a node to a E any other position else { int i; ; ptr=st tart; for(i= =2; i<=pos-1; i++) ptr=pt tr->next; newnod de->next=pt tr->next; newnod de->prev=pt tr; (ptr-> >next)->pre ev=newnode // Note Line. e; ptr->n next=newnod de; count+ ++; } return 1; r } in DEL(int pos) nt t { struct node *temp p; // ERRO HANDLING #1 - Und OR G derflow if(star rt==NULL) { f("\n There are no Nodes to de e N elete. "); printf return 0; n } // ERRO HANDLING #2 - Pos OR G sition Over rlimits if( pos s<=0 || pos s>count ) { f("\n Posit tion Enter red Over li imits. "); printf return 0; n } // Cond dition 1: Deleting Node at 1st position OR Last N D N t n Node in LL. L if(pos= ==1) { temp=s start; start= =start->nex xt; free(t temp); count--; } // Cond dition 2: Deleting node at las positio D n st on else if f(pos==coun nt) { tr=start; ptr->next!=NULL; ptr p r=ptr->nex xt); for(pt (ptr-> >prev)->nex xt=NULL;


printf f("\n Delet ting Node with value = %d" , ptr->info) e ); free(p ptr); count--; } // Cond dition 3; Deleting node at any other po D n y osition else { ; int i; ptr=st tart; for(i= =2; i<=pos; i++) ; ptr=pt tr->next; >prev)->nex xt=ptr->ne ext; (ptr-> (ptr-> >next)->pre ev=ptr->pr rev; printf f("\n Delet ting Node with value = %d",pt e tr->info); free(p ptr); count--; } return 1; } in F_Trave nt erse() { if(star rt==NULL) { f("\n There are no elements in this Lin e e n nked List." "); printf return 0; n } printf( ("\n ["); for(ptr r=start; pt tr!=NULL; ptr=ptr->n next) printf( - %d",pt (" tr->info); printf( - ]\n"); (" ; return 1; } in R_Trave nt erse() { if(star rt==NULL) { f("\n There are no elements in this Lin e e n nked List." "); printf return 0; n } for(ptr r=start; pt tr->next!= =NULL; ptr= =ptr->next t); ("\n ["); printf( for(; ptr!=NULL; ptr=ptr-> p >prev) printf( - %d",pt (" tr->info); printf( - ]\n"); (" ; return 1; } in Search( nt (int ITEM) {


int i; for(i=1 1,ptr=start ptr!=NU t; ULL; ptr=pt tr->next,i i++) if(ptr->info==ITE EM) return i; return -1; } in main() nt { int ch,success; int ITE EM; int pos s=1; start=N NULL; count=0 0; do { printf( ("\n ______ __________ ___________ __________ ___________ ________ "); printf( ("\n MENU: "); printf( ("\n\n 1. Insert a Node. "); I N printf( ("\n 2. Del lete a Nod de. "); printf( ("\n 3. For rward Trav versing the Doubly L e LL. "); printf( ("\n 4. Rev verse Trav versing the Doubly L e LL. "); printf( ("\n 5. Sea arch for a node. "); ; printf( ("\n 0. EXI IT. "); printf( ("\n\n Your Choice: "); r scanf(" "%d", &ch); ; switch( (ch) { : T case 1: //INSERT ("\n Enter the Item to Insert "); r t: printf( scanf(" "%d",&ITEM M); if(star rt!=NULL) { ("\n Enter the posit r tion: "); printf( scanf(" "%d", &pos s); } success s=INS(ITEM pos); M, break; case 2: //DELETE : E if(star rt!=NULL) { ("\n Enter the posit r tion: "); printf( scanf(" "%d", &pos s); } success s=DEL(pos); break; case 3: //Forwar Traversi : rd ing printf( ("\n The Elements: " E ");


success s=F_Traver rse(); break; case 4: //Revers Traversi : se ing printf( ("\n The Elements: \ E \n"); success s=R_Traver rse(); break; : hing case 5: //Search ("\n Enter the Item to Search for: "); r h printf( scanf(" "%d",&ITEM M); pos=Sea arch(ITEM); if(pos! !=-1) printf( ("\n The Item is fou I und at %d Position",pos); , else ("\n Item not found. "); . printf( break; case 0: : printf( ("\n Exiti ing... "); } }while( (ch); return 1; n }

OUTPUT: O
__ __________ ___________ __________ ___________ __________ ___ MENU: M 1. 1 2. 2 3. 3 4. 4 5. 5 0. 0 Insert a Node. Delete a Node. Forward Traversin the Dou d ng ubly LL. Reverse Traversin the Dou e ng ubly LL. Search for a node e. EXIT.

Your Choic Y ce: 1 Enter the Item to In E nsert: 23 __________ _ ___________ __________ ___________ __________ ____ MENU: M 1. 1 2. 2 3. 3 4. 4 5. 5 0. 0 Insert a Node. Delete a Node. Forward Traversin the Dou d ng ubly LL. Reverse Traversin the Dou e ng ubly LL. Search for a node e. EXIT.

Your Choic Y ce: 1 Enter the Item to In E nsert: 12 Enter the position: 1 E __________ _ ___________ __________ ___________ __________ ____ MENU: M 1. 1 2. 2 3. 3 4. 4 5. 5 0. 0 Insert a Node. Delete a Node. Forward Traversin the Dou d ng ubly LL. Reverse Traversin the Dou e ng ubly LL. Search for a node e. EXIT.

Your Choic Y ce: 1 Enter the Item to In E nsert: 90 Enter the position: 3 E

__ __________ ___________ __________ ___________ __________ ___ MENU: M 1. 1 2. 2 3. 3 4. 4 5. 5 0. 0 Insert a Node. Delete a Node. Forward Traversin the Dou d ng ubly LL. Reverse Traversin the Dou e ng ubly LL. Search for a node e. EXIT.

Your Choic Y ce: 3 The Elemen T nts: [ - 12 - 23 - 90 - ] 2 __________ _ ___________ __________ ___________ __________ ____ MENU: M 1. 1 2. 2 3. 3 4. 4 5. 5 0. 0 Insert a Node. Delete a Node. Forward Traversin the Dou d ng ubly LL. Reverse Traversin the Dou e ng ubly LL. Search for a node e. EXIT.

Your Choic Y ce: 4 The Elemen T nts: [ - 90 - 23 - 12 - ] 2 __________ _ ___________ __________ ___________ __________ ____ MENU: M 1. 1 2. 2 3. 3 4. 4 5. 5 0. 0 Insert a Node. Delete a Node. Forward Traversin the Dou d ng ubly LL. Reverse Traversin the Dou e ng ubly LL. Search for a node e. EXIT.

Your Choic Y ce: 5 Enter the Item to Se E earch for: 12 The Item is found at 1 Positi T i t ion

__ __________ ___________ __________ ___________ __________ ___ MENU: M 1. 1 2. 2 3. 3 4. 4 5. 5 0. 0 Insert a Node. Delete a Node. Forward Traversin the Dou d ng ubly LL. Reverse Traversin the Dou e ng ubly LL. Search for a node e. EXIT.

Your Choic Y ce: 2 Enter the position: 1 E __________ _ ___________ __________ ___________ __________ ____ MENU: M 1. 1 2. 2 3. 3 4. 4 5. 5 0. 0 Insert a Node. Delete a Node. Forward Traversin the Dou d ng ubly LL. Reverse Traversin the Dou e ng ubly LL. Search for a node e. EXIT.

Your Choic Y ce: 3 The Elemen T nts: [ - 23 - 90 - ] 9 __________ _ ___________ __________ ___________ __________ ____ MENU: M 1. 1 2. 2 3. 3 4. 4 5. 5 0. 0 Insert a Node. Delete a Node. Forward Traversin the Dou d ng ubly LL. Reverse Traversin the Dou e ng ubly LL. Search for a node e. EXIT.

Your Choic Y ce: 0 Exiting... E .

// Write a Prog / e gram to perform Addit rm tion, Su ubtracti ion and Mult a tiplicat tion ope erations on 2D Array (matrix s D x)
#i include<st tdio.h> vo oid Matrix x_Display(i int a[][20 0],int n) { int i,j; for(i= =0; i<n; i+ ++) { for(j j=0; j<n; j++) j { pri intf(" %d", ,a[i][j]); } tf("\n"); print } } in main() nt { i,j,k; int n,i int a[2 20][20]; int b[2 20][20]; int c[2 20][20]; ("\n Enter the dimen nsions of t the 2 Squa are matrice es: "); printf( scanf(" "%d",&n); ("\n Enter elements of Matrix A: "); printf( for(i=0 i<n; i++ 0; +) for(j=0 j<n; j++ 0; +) scanf(" "%d",&a[i][ [j]); ("\n Enter elements of Matrix B: "); printf( for(i=0 i<n; i++ 0; +) for(j=0 j<n; j++ 0; +) scanf(" "%d",&b[i][ [j]); ("\n Matrix A: \n"); x printf( Matrix_ _Display(a, ,n); ("\n\n Matr rix B: \n" "); printf( Matrix_ _Display(b, ,n); //Addit tion for(i=0 i<n; i++ 0; +) for(j=0 j<n; j++ 0; +) c[i][j]=a[i][j]+b b[i][j]; printf( ("\n\n Addi ition of A and B giv ves: \n"); Matrix_ _Display(c, ,n); //Subt traction for(i=0 i<n; i++ 0; +) for(j=0 j<n; j++ 0; +)


c[i][j]=a[i][j]-b b[i][j]; printf( ("\n\n Subt traction of A and B gives: \n o n"); Matrix_ _Display(c, ,n); //Multi iplication for(i=0 i<n; i++ 0; +) for(j=0 j<n; j++ 0; +) { c[i][j j]=0; for(k= =0; k<n; k+ ++) c[i][j j]+=a[i][k] ]*b[k][j]; } printf( ("\n\n Mult tiplicatio of A and B gives: \n"); on d Matrix_ _Display(c, ,n); }

OU UTPUT: Enter the dimensions of the 2 Square ma E s atrices: 2 Enter elem E ments of Ma atrix A: 1 2 3 4 Enter elem E ments of Ma atrix B: 4 3 2 1 Matrix A: M 1 2 3 4

Matrix B: M 4 3 2 1

Addition of A and B gives: A o 5 5 5 5

Subtractio of A and B gives: S on d -3 -1 1 3

Multiplica M ation of A and B giv ves: 8 5 20 13 2

//WAP to implem / o ment Sta acks usi ing Lin nked Lis sts.
#i include <s stdio.h> st truct node e { int info; stru uct node * next; }* *top,*temp p;

vo oid PUSH(i int item) { st truct node *newnode; e ; ne ewnode = (struct nod *) mall ( de loc ( sizeo of(struct node) ); f(newnode= ==NULL) if pr rintf(" NO SPACE LEF "); O FT el lse { ewnode->in nfo=item; ne if top == NULL ) f( { ne ewnode->ne ext=NULL; to op=newnode e; } el lse { ne ewnode->ne ext=top; to op=newnode e; } } //end of outer else e }/ end of func //

vo oid POP() { if f(top==NUL LL) pr rintf("\n Stack Empt ty. \n"); el lse { rintf("\n Node with Info=%d is deleted. \n", top i . p->info); pr te emp=top; to op=top->ne ext; fr ree(temp); ; } } vo oid DISPLA AY() { if f(top==NUL LL) pr rintf("\n Stack is empty. "); e el lse


{ te emp=top; rintf("\n" "); pr wh hile(temp!=NULL) { rintf("\n |%d| ", te emp->info); pr te emp=temp-> >next; } rintf("\n" "); pr }/ //end of else e }

in main() nt { em,ch=1; int ite top=NUL LL;

while(ch h!=4) { ("\n MENU: "); printf( printf( ("\n 1. Ins sert a nod de. "); printf( ("\n 2. Del lete a nod de. "); printf( ("\n 3. Dis splay Stac ck. "); printf( ("\n 4. EXI \n Your Choice: " IT r "); scanf(" "%d",&ch); switch( (ch) { case 1: //INSERT : ("\n ENTER ITEM: "); printf( scanf(" "%d", &item m); PUSH(it tem); break; case 2: //DELETE : POP(); break; case 3: //DISPLAY : Y DISPLAY Y(); break; case 4: : printf( ("\n Exitin ng.. "); break; default printf(" INVALID CHOICE. ") t: " ); } }; //en of while loop nd e return 0; }

OU UTPUT: ME ENU: 1. Insert a node. 1 2. Delete a node. 2 3. Display Stack. 3 y 4. EXIT 4 Your Choic Y ce: 1 ENTER ITEM 64 E M: MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display Stack. 3 y 4. EXIT 4 Your Choic Y ce: 1 ENTER ITEM 89 E M: MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display Stack. 3 y 4. EXIT 4 Your Choic Y ce: 1 ENTER ITEM 90 E M: MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display Stack. 3 y 4. EXIT 4 Your Choic Y ce: 3

|90| | |89| | |64| | MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display Stack. 3 y 4. EXIT 4 Your Choic Y ce: 2 Node with Info=90 is deleted. N s

ME ENU: 1. Insert a node. 1 2. Delete a node. 2 3. Display Stack. 3 y 4. EXIT 4 Your Choic Y ce: 2

|89| | |64| | MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display Stack. 3 y 4. EXIT 4 Your Choic Y ce: 4 Exiting.. E

// WAP to imple / t ement Qu ueues us sing Li inked Li ists.


#i include<st tdio.h> st truct node e { int info; stru uct node * next; }* *start, *r rear, *temp p;

vo oid INS(in item) nt { truct node *newnode; e ; st ne ewnode = (struct nod *) mall ( de loc ( sizeo of(struct node) ); if f(newnode= ==NULL) pr rintf(" NO SPACE LEF "); O FT el lse { ne ewnode->in nfo=item; ne ewnode->ne ext=NULL; if start == NULL ) f( = { st tart=newno ode; re ear=newnod de; } el lse // INS at END S { re ear->next = newnode; ; re ear=rear-> >next; } } //end of outer if }/ end of func // vo oid DEL() { if f(start==N NULL) pr rintf("\n No item to delete. Queue is e o empty. \n" "); el lse { pr rintf("\n Node with Info = %d is Delete d ed. \n", s start->info ); o te emp=start; ; st tart=start t->next; fr ree(temp); ; } }


vo oid DISPLA AY() { if f(start==N NULL) pr rintf("\n Queue is empty. No Nodes. "); e ; el lse { te emp=start; ; pr rintf("\n |"); wh hile(temp!=NULL) { pr rintf(" - |%d|", tem mp->info); te emp=temp-> >next; } rintf(" - | \n"); pr }/ //end of else e } in main() nt { em,ch=1; int ite start=r rear=NULL; while(ch h!=4) { ("\n MENU: \n"); printf( printf( 1. Inser a node. "); (" rt printf( ("\n 2. Del lete a nod de. "); printf( ("\n 3. Dis splay Queu ue. "); printf( ("\n 4. EXI \n Your Choice: " IT r "); scanf(" "%d",&ch); switch( (ch) { : case 1: //INSERT printf( ("\n ENTER ITEM: "); scanf(" "%d", &item m); INS(ite em); break; case 2: //DELETE : DEL(); break; case 3: //DISPLAY : Y DISPLAY Y(); break; case 4: : printf( ("\n Exitin ng.. "); break; default printf(" INVALID CHOICE. ") t: " ); } }; //e end of whil loop le return 0; } n


OU UTPUT: ME ENU: 1. Insert a node. 1 2. Delete a node. 2 3. Display Queue. 3 y 4. EXIT 4 Your Choic Y ce: 1 ENTER ITEM 64 E M: MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display Queue. 3 y 4. EXIT 4 Your Choic Y ce: 1 ENTER ITEM 89 E M: MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display Queue. 3 y 4. EXIT 4 Your Choic Y ce: 1 ENTER ITEM 90 E M: MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display Queue. 3 y 4. EXIT 4 Your Choic Y ce: 3 | - |64| - |89| - |9 90| - | MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display Queue. 3 y 4. EXIT 4 Your Choic Y ce: 2 Node with Info = 64 is Delete N ed. MENU: M 1. Insert a node. 1 2. Delete a node. 2 3. Display Queue. 3 y 4. EXIT 4 Your Choic Y ce: 3 | - |89| - |90| - |

NEIL MATHE M EW A232 2471000 02 2CS4 Y330 4 05

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