64
NAME :Pindariya bhavesh p SUBJECT : DS COURSE: MCA ROLL NO: B-29 1. Array operations: Traversing, Insertion, Deletion in array #include<stdio.h> #include<conio.h> void main() { int a[5],i,n,j,k,c,ch,b,m; clrscr(); printf("Enter the array;-"); scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("1 : Insert \n"); printf("2 : Delete \n"); printf("3 : Traiversing \n"); printf("Enter the coice :-"); scanf("%d",&c); switch(c) { case 1: printf("\nEnetr the position:-"); scanf("%d",&j); printf("\nEnter the value:-"); scanf("%d",&k); for(i=n;i>=j;i--) { a[i+1]=a[i]; } a[j]=k; for(i=0;i<n+1;i++) { printf("%d\n",a[i]); } break; case 2: printf("Enter the position to delete :-"); scanf("%d",&b); for(i=b;i<n;i++)

ds lab1

Embed Size (px)

Citation preview

Page 1: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

1. Array operations: Traversing, Insertion, Deletion in array

#include<stdio.h>#include<conio.h>void main() {

int a[5],i,n,j,k,c,ch,b,m;clrscr();printf("Enter the array;-");

scanf("%d",&n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("1 : Insert \n"); printf("2 : Delete \n"); printf("3 : Traiversing \n"); printf("Enter the coice :-"); scanf("%d",&c); switch(c) { case 1: printf("\nEnetr the position:-");

scanf("%d",&j);printf("\nEnter the value:-");

scanf("%d",&k); for(i=n;i>=j;i--)

{ a[i+1]=a[i];

} a[j]=k; for(i=0;i<n+1;i++) { printf("%d\n",a[i]); } break; case 2: printf("Enter the position to delete :-"); scanf("%d",&b); for(i=b;i<n;i++) a[i]=a[i+1]; for(i=0;i<n-1;i++) { printf("%d\n",a[i]); } break; case 3: for(i=0;i<n;i++) {

Page 2: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

printf("%d\n",a[i]); } break; } getch();}/*OUTPUT Print Array:-1,2,3,4,5,8 1) : Insert 2) : Delete 3) : Traiversing Enter the coice :-1 Enetr the position:-5 Enter the value:-10 Print Array:-1,2,3,4,5,10,8 Enter the coice :-2 Enetr the position To delete:-2 Print Array:-1,2,4,5,10,8

2. Addition and Multiplication of Two Polynomials.

CODE: /*POLYNOMIAL OF ADDITION USING LINKED LIST*/

#include<stdio.h>struct node{

int coff;int pow;struct list *next;

};typedef struct node POLY;void Insert(POLY **first,int co,int power){

POLY *newnode,*temp;newnode=(POLY *)malloc(sizeof(POLY));newnode->coff=co;newnode->pow=power;newnode->next=NULL;if(*first==NULL)

*first=newnode;else{

temp=*first;while(temp->next!=NULL)

temp=temp->next;temp->next=newnode;

Page 3: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

}}void Display(POLY *temp){

if(temp==NULL){

printf("\n\n\tLIST IS EMPTY.");return;

}printf("\nPOLYNOMIAL IS:\n\n");while(temp!=NULL){

printf("%dX^%d",temp->coff,temp->pow);if(temp->next!=NULL)

printf(" + ");temp=temp->next;

}return;

}void POLY_ADD(POLY *first,POLY *second,POLY **dest){

POLY *ptr1,*ptr2;ptr1=first;ptr2=second;while((ptr1!=NULL)&&(ptr2!=NULL)){

if((ptr1->pow)>(ptr2->pow)){

Insert(dest,ptr1->coff,ptr1->pow);ptr1=ptr1->next;

}else if((ptr1->pow)<(ptr2->pow)){

Insert(dest,ptr2->coff,ptr2->pow);ptr2=ptr2->next;

}else{

Insert(dest,ptr1->coff+ptr2->coff,ptr1->pow);ptr2=ptr2->next;ptr1=ptr1->next;

} }while(ptr1!=NULL){

Insert(dest,ptr1->coff,ptr1->pow);ptr1=ptr1->next;

Page 4: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

}while(ptr2!=NULL){

Insert(dest,ptr2->coff,ptr2->pow);ptr2=ptr2->next;

}printf("\n\n\tPOLYNOMIAL ADDED SUCCESSFULLY");return;

}void main(){

POLY *first,*second,*dest;int ch,ele,pow;first=NULL;second=NULL;dest=NULL;do{

clrscr();printf("\t\t\t\tMERGING MENU\n\t\t\t\t************\n");printf("\n\t\t\t1.INSERT INTO FIRST LIST\n\t\t\t2.INSERT INTO

SECOND LIST");printf("\n\t\t\t3.DISPLAY FIRST LIST\n\t\t\t4.DISPLAY SECOND

LIST");printf("\n\t\t\t5.MERGE\n\t\t\t6.DISPLAY MERGED LIST\n\t\t\

t7.EXIT");printf("\n\n\t\t\tENTER YOUR CHOICE:");scanf("%d",&ch);switch(ch){

case 1:printf("\nEnter The cofficient:");scanf("%d",&ele);printf("\nEnter The Power(DECREASING

ORDER):");scanf("%d",&pow);Insert(&first,ele,pow);break;

case 2:printf("\nEnter The Element:");scanf("%d",&ele);printf("\nEnter The Power(DECREASING

ORDER):");scanf("%d",&pow);Insert(&second,ele,pow);break;

case 3:

Page 5: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

Display(first);break;

case 4:Display(second);break;

case 6:Display(dest);break;

case 5:POLY_ADD(first,second,&dest);break;

case 7:exit(1);break;

default:printf("\n\n\tWRONG CHOICE.");

}getch();

}while(ch!=7);}

OUTPUT:

2

ADDED POLYNOMIAL

Page 6: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

3. Addition and Transpose of Sparse Matrices. #include<stdio.h>#include<conio.h>#define SIZE 10typedef struct sp{

int row,col,val;}SPR;void readmat(int a[][SIZE],int m, int n);int mak_sparse(int a[][SIZE], int m,int n, SPR s1[]);void display_spr(SPR s[],int n);void main(){int a[SIZE][SIZE],b[SIZE][SIZE],i,j,k,m,n,i1,i2,n1,n2,i3,n3;SPR s1[SIZE],s2[SIZE],s3[SIZE];i1=i2=i3=1;clrscr();printf("\n\tEnter size for matrix A(col n row):- ");scanf("%d%d",&n,&m);printf("\n\tEnter %d elements for matrix A:- ",m*n);readmat(a,m,n);n1=mak_sparse(a,m,n,s1);display_spr(s1,n1);getch();printf("\n\tEnter size for matrix B(col n row):- ");scanf("%d%d",&n,&m);printf("\n\tEnter %d elements for matrix B:- ",m*n);readmat(b,m,n);n2=mak_sparse(b,m,n,s2);display_spr(s2,n2);getch();s3[0].row = s1[0].row > s2[0].row ? s1[0].row : s2[0].row;s3[0].col = s1[0].col > s2[0].col ? s1[0].col : s2[0].col;while(i1 < n1 && i2 < n2){

if(s1[i1].row == s2[i2].row){

if(s1[i1].col == s2[i2].col){

s3[i3].row=s1[i1].row;s3[i3].col=s1[i1].col;s3[i3].val=s1[i1].val+s2[i2].val;i3++;i2++;i1++;

}

Page 7: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

else if(s1[i1].col < s2[i2].col){

s3[i3].row=s1[i1].row;s3[i3].col=s1[i1].col;s3[i3].val=s1[i1].val;i1++;i3++;

}else{

s3[i3].row=s2[i2].row;s3[i3].col=s2[i2].col;s3[i3].val=s2[i2].val;i3++;i2++;

}}else if(s1[i1].row < s2[i2].row){

s3[i3].row=s1[i1].row;s3[i3].col=s1[i1].col;s3[i3].val=s1[i1].val;i3++;i1++;

}else{

s3[i3].row=s2[i2].row;s3[i3].col=s2[i2].col;s3[i3].val=s2[i2].val;i2++;i3++;

}}while(i2<n2){s3[i3].row=s2[i2].row;s3[i3].col=s2[i2].col;s3[i3].val=s2[i2].val;i2++;i3++;}while(i1<n1){s3[i3].row=s1[i1].row;s3[i3].col=s1[i1].col;s3[i3].val=s1[i1].val;

Page 8: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

i1++;i3++;}n3=i3;s3[0].val=n3-1;printf("\n\tAdded sparse matrix is:- \n");display_spr(s3,n3);getch();}void readmat(int a[][SIZE],int m, int n){int i,j;for(i=0;i<n;i++){

for(j=0;j<m;j++){

scanf("%d",&a[i][j]);}

}}int mak_sparse(int a[][SIZE], int m,int n, SPR s1[]){int i,j,k=1;for(i=0;i<n;i++){

for(j=0;j<m;j++){

if (a[i][j] != 0){

s1[k].row=i;s1[k].col=j;s1[k].val=a[i][j];k++;

}}

}s1[0].row=m;s1[0].col=n;s1[0].val=k-1;return(k);}void display_spr(SPR s[],int n){int i;printf("\n\tRow\tCol\tVal\n");for(i=0;i<n;i++)printf("\t%d\t%d\t%d\n",s[i].row,s[i].col,s[i].val);

Page 9: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

}

4. Singly Linked List: Create, Display, Insertion, Deletion, Search, Reverse

CODE:

#include<stdio.h>#include<conio.h>#include<stdlib.h>typedef struct student{

int no;char name[20];struct student *next;

}stud;stud *head,*tail,*new1;void add();void disp();void search();void del(int);void rev();int count=0;void main(){

char ch;int delet;clrscr();while(1){

clrscr();printf("\n\t\t--------------------------------------------");printf("\n\t\t\t1. Create List..");printf("\n\t\t\t2. Add Node to List");printf("\n\t\t\t3. Display List");printf("\n\t\t\t4. Search Node");printf("\n\t\t\t5. Delete Node");printf("\n\t\t\t6. Reverse of List..");printf("\n\t\t\t7. Exit...");printf("\n\t\t--------------------------------------------");printf("\n\n\tEnter Your Choice...");ch=getchar();fflush(stdin);switch(ch){

case '1':head=tail=new1=NULL;

Page 10: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

break;case '2':

add();break;

case '3':disp();break;

case '4':search();break;

case '5':printf("\nEnter Node no:- ");scanf("%d",&delet);del(delet);break;

case '6':rev();break;

case '7':exit(0);//break;

default:printf("\nInvalid Choice...");break;

}}

}void add(){

char ch;int n1;//clrscr();while(1){new1=(stud *)malloc(sizeof(stud));printf("\nEnter No:- ");scanf("%d",&new1->no);printf("\nEnter name:- ");fflush(stdin);gets(new1->name);count++;new1->next=NULL;if(head==NULL)

head=new1;else

tail->next=new1;tail=new1;

Page 11: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

printf("\n\tDo you want to Continue[Y/N]...");ch=getchar();fflush(stdin);if(ch=='n' || ch=='N')return 0;

}

}

void disp(){

stud *temp;temp=head;if(head==NULL)

printf("\nSorry! List in Empty...");elsewhile(temp!=NULL){

printf("\nNo:- %d",temp->no);printf("\nName:- %s",temp->name);temp=temp->next;

}printf("\n\nTotal No of Nodes=%d",count);getch();

}void search(){

stud *temp;int n,f=0;temp=head;printf("\nEnter Search No:- ");scanf("%d",&n);if(temp==NULL)

printf("\nSorry! List is Empty...");else

while(temp!=NULL){

if(temp->no==n){

printf("\nNo:- %d",temp->no);printf("\nName:- %s",temp->name);f++;

}temp=temp->next;

}if(f==1)

printf("\n[%d] Found [%d] times...",n,f);

Page 12: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

elseprintf("\nSorry! Not Found....");

getch();}void del(int delet){

stud *temp;int n;temp=head;if(temp==NULL){

printf("\nSorry! List is Empty....");sleep(3);exit(0);;

}else{

while(temp->next!=NULL){

count++;if(count==delet){

temp=temp->next;free(temp);

}}

}}void rev()

{stud *p1,*p2,*p3,*temp;p1=head;p2=p1->next;p3=p2->next;head->next=NULL;p2->next=p1;while(p3!=NULL){

p1=p2;p2=p3;p3=p3->next;p2->next=p1;

}head=p2;temp=head;while(temp!=NULL){

Page 13: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

printf("\nRoll-> %d Name:-%s",temp->no,temp->name);temp=temp->next;

}getch();

}

5. Singly Circular Linked List: Create, Display, Insertion, Deletion, Search.

CODE:

#include<stdio.h>#include<conio.h>#include<stdlib.h>typedef struct student{

int no;char name[20];struct student *next;

}stud;stud *head,*tail,*new1;void add();void add_beg();void add_after();void disp();void search();void del(int);void del(int);int count=0;void main(){

char ch;int delet;int pos;clrscr();while(1){

clrscr();printf("\n\t\t--------------------------------------------");printf("\n\t\t\t1. Create List..");printf("\n\t\t\t2. Add Node to List");printf("\n\t\t\t3. Display List");printf("\n\t\t\t4. Search Node");printf("\n\t\t\t5. Delete Node");printf("\n\t\t\t6. Add at begining..");printf("\n\t\t\t7. Add After position...");printf("\n\t\t\t8. Delete particular node...");printf("\n\t\t\t0. Exit...");

Page 14: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

printf("\n\t\t--------------------------------------------");printf("\n\n\tEnter Your Choice...");ch=getchar();fflush(stdin);switch(ch){

case '1':head=tail=new1=NULL;break;

case '2':add();break;

case '3':disp();break;

case '4':search();break;

case '5':printf("\nEnter Node no:- ");scanf("%d",&delet);del(delet);break;

case '6':add_beg();break;

case '7':printf("\nEnter the position after to insert:- ");scanf("%d",&pos);add_after(pos);break;

case '8':printf("\nEnter Node no to delete:- ");scanf("%d",&pos);del(pos);break;

case '0':exit(0);//break;

default:printf("\nInvalid Choice...");break;

}}

}void add(){

Page 15: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

char ch;int n1;//clrscr();while(1){new1=(stud *)malloc(sizeof(stud));printf("\nEnter No:- ");scanf("%d",&new1->no);printf("\nEnter name:- ");fflush(stdin);gets(new1->name);count++;new1->next=head;if(head==NULL)

head=new1;else

tail->next=new1;tail=new1;printf("\n\tDo you want to Continue[Y/N]...");ch=getchar();fflush(stdin);if(ch=='n' || ch=='N')return 0;

}

}

void disp(){

int c=0;stud *temp;temp=head;if(head==NULL)

printf("\nSorry! List in Empty...");else{

while(temp->next!=head){

printf("\nNo:- %d",temp->no);printf("\nName:- %s",temp->name);temp=temp->next;c++;

}printf("\nNo:- %d",temp->no);printf("\nName:- %d",temp->name);c++;

Page 16: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

}printf("\n\nTotal No of Nodes=%d",c);getch();

}void search(){

stud *temp;int n,f=0;temp=head;printf("\nEnter Search No:- ");scanf("%d",&n);if(temp==NULL)

printf("\nSorry! List is Empty...");else

while(temp->next!=head){

if(temp->no==n){

printf("\nNo:- %d",temp->no);printf("\nName:- %s",temp->name);f++;

}temp=temp->next;

}if(f==1)

printf("\n[%d] Found [%d] times...",n,f);else

printf("\nSorry! Not Found....");getch();

}void del(int delet){

stud *temp,*temp1;int n;temp=head;if(temp==NULL){

printf("\nSorry! List is Empty....");sleep(3);exit(0);;

}else{

if(head->no==delet){

temp=head;head=head->next;

Page 17: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

free(temp);}while(temp->next!=head){

if(temp->next->no==delet){

temp1=temp->next;temp->next=temp1->next;free(temp1);

}temp=temp->next;

}if(temp->next->no==delet){

temp1=temp->next;temp->next=head;free(temp1);

}}

}void add_beg(){

clrscr();new1=(stud *)malloc(sizeof(stud));printf("\nEnter data to add at beginig....");printf("\nEnter no:- ");scanf("%d",&new1->no);printf("\nEnter Name:- ");fflush(stdin);gets(new1->name);if(head==NULL){

head=new1;new1->next=new1;

}else{

new1->next=head;head=new1;tail->next=head;

}getch();

}void add_after(int pos){

int count=0;stud *temp;

Page 18: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

temp=head;while(temp->next!=head){

count++;if(count==pos){

new1=(stud *)malloc(sizeof(stud));printf("\nEnter No. :- ");scanf("%d",&new1->no);printf("\nEnter Name:- ");fflush(stdin);gets(new1->name);new1->next=temp->next;temp->next=new1;

}temp=temp->next;

}}

void del(int n){

stud *temp,*q;clrscr();q=head;while(q->next->next!=NULL){

if(q->next==n){

Temp=q->next;q->next=temp->next;free(temp);

}q=q->next;

}}

6. Doubly Linked List: Create, Display, Insertion, Deletion, Search, Reverse.

CODE:

#include<stdio.h>#include<conio.h>typedef struct student{

int no;struct student *prev;struct student *next;

Page 19: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

}stud;stud *head,*tail,*new1;void add(int);void ins_beg();void ins_after(int);void disp();void search(int);void disp_rev();void main(){

int n;char ch;while(1){

clrscr();printf("\n\t\t---------------------------------------------");printf("\n\t\t\t1. Add Node");printf("\n\t\t\t2. Display");printf("\n\t\t\t3. Add at begining...");printf("\n\t\t\t4. Add after node..");printf("\n\t\t\t5. Search Node..");printf("\n\t\t\t6. Display Reverse...");printf("\n\t\t\t0. Exit");printf("\n\n\t\t-------------------------------------------");printf("\n\n\t\tEnter Your Choice....");fflush(stdin);ch=getchar();switch(ch){

case '1':printf("\nNumber of Node:- ");scanf("%d",&n);add(n);break;

case '2':disp();break;

case '3':ins_beg();break;

case '4':printf("\nEnter Node no after to insert:- ");scanf("%d",&n);ins_after(n);break;

case '5':printf("\nEnter Node no:- ");

Page 20: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

scanf("%d",&n);search(n);break;

case '6':disp_rev();break;

case '0':exit(0);return;

default:printf("\nInvalid Choice..");break;

}}

}void add(int n){

int i;for(i=0;i<n;i++){

new1=(stud *)malloc(sizeof(stud));new1->next=NULL;printf("\nEnter number:- ");scanf("%d",&new1->no);if(head==NULL){

head=new1;head->prev=NULL;

}else{

new1->prev=tail;tail->next=new1;

}tail=new1;

}getch();

}void disp(){

stud *temp;int count=0;temp=head;clrscr();while(temp!=NULL){

count++;

Page 21: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

printf("\nNo=%d",temp->no);temp=temp->next;

}printf("\nNo. of times=%d",count);getch();

}void ins_beg(){

stud *temp;new1=(stud *)malloc(sizeof(stud));printf("\nEnter no:- ");scanf("%d",&new1->no);new1->prev=NULL;new1->next=head;head=new1;

}void ins_after(int n){

stud *temp,*temp1;temp=head;while(temp->next!=head){

if(temp->no==n){

new1=(stud *)malloc(sizeof(stud));printf("\nEnter Number:- ");scanf("%d",&new1->no);new1->next=temp->next;temp->next=new1;new1->prev=temp;return;

}temp=temp->next;

}}void search(int n){

stud *temp;int count=0;temp=head;while(temp!=NULL){

if(temp->no==n){

count++;}temp=temp->next;

Page 22: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

}printf("\n%d found %d times...",n,count);getch();

}void disp_rev(){

stud *temp;temp=tail;while(tail->next!=NULL){

printf("\nNo.:- %d",temp->no);temp=temp->prev;

}getch();

}

7. Stack Implementation

CODE:

#include<stdio.h>#include<conio.h>typedef struct student{

int no;struct student *next;

}stud;stud *top=NULL;void push();void disp();void pop();void main(){

char ch;while(1){

clrscr();printf("\n\t\t------------------Main Menu-----------------");printf("\n\t\t\t1.PUSH");printf("\n\t\t\t2.POP");printf("\n\t\t\t3.Display");printf("\n\t\t\t4.Exit");printf("\n\t\t--------------------------------------------");printf("\n\n\tEnter Your Choice....");fflush(stdin);ch=getchar();switch(ch)

Page 23: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

{case '1':

push();break;

case '2':pop();break;

case '3':disp();break;

case '4':exit(0);return;

default:printf("\nSorry! Invalid Choice...");

}}

}void push(){

char ch;stud *temp;while(1){temp=(stud *)malloc(sizeof(stud));printf("\nEnter Element:- ");scanf("%d",&temp->no);temp->next=top;top=temp;printf("\nContinue[Y/N]...");fflush(stdin);ch=getchar();if(ch=='n' || ch=='N')break;}

}void pop(){

stud *temp;if(top==NULL){

printf("\nSorry! Stack underflow..");

}else{

temp=top;

Page 24: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

printf("\nPopped element is %d",temp->no);top=temp->next;free(temp);

}}void disp(){

stud *temp;if(top==NULL)

printf("\nSorry! Stack is underflow...");else{

temp=top;while(temp!=NULL){

printf("\nElement=%d",temp->no);temp=temp->next;

}}getch();

}

8. Stack Application: Palindrome & Matching Parenthesis.

CODE:

#include<stdio.h>#include<conio.h>#define MAX 20char st_arr[MAX];int top=-1;void push(char);char pop();void main(){

int i,c=0;char s[20],s1[20];clrscr();printf("\nEnter String:- ");fflush(stdin);gets(s);for(i=0;s[i]!=NULL;i++){

push(s[i]);c++;

}

Page 25: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

for(i=0;i<c;i++){

if(s[i]!=pop())break;

}if(top==-1)

printf("\nPalindrome...");else

printf("\nNot Palindrome...");getch();

}void push(char ch){

top++;st_arr[top]=ch;

}char pop(){

char ch;int j;j=top;ch=st_arr[top];top--;return ch;

}

9. STACK IMPLEMENTATION USING AN ARRAY

// Program implements array as a stack

#include <stdio.h>#include <conio.h>#define MAX 10struct stack{

int arr[MAX];int top ;

};struct stack s;void push( int item ) ;int pop( ) ;void display();// adds an element to the stackvoid push ( int item ){

if ( s.top == MAX - 1 )

Page 26: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

{printf("Stack is full" );return ;

}s.top++ ;s.arr[s.top] = item ;

}

// removes an element from the stackint pop( ){

int data;if ( s.top == -1 ){

printf("Stack is empty");return NULL ;

}data = s.arr[s.top] ;s.top-- ;return data ;

}void display(){

int i;if(s.top==-1){ printf("Stack is empty\n"); return;}printf("Stack contains following data \n");for(i=s.top;i>=0;i--){

printf("%d\n",s.arr[i]);}

}

void main( ){ int choice,num,v; s.top=-1; do {

clrscr();printf("\n");for(i=0;i<=50;i++){

printf("-");

Page 27: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

delay(30);}printf("\n ENTER 1 FOR PUSH :: ");printf("\n ENTER 2 FOR POP :: ");printf("\n ENTER 3 FOR DISPLAY :: ");printf("\n ENTER 4 FOR EXIT :: ");scanf("%d",& choice);printf("\n");switch(choice){

case 1: printf("\n\nEnter number you want to push in a stack "); scanf("%d",&num); push(num); getch(); break;case 2: v = pop( ) ; printf("\n\nItem popped: %d",v) ; getch(); break;case 3: display(); getch(); break;case 4: break;default : printf("Wrong choice \n");

} } while(choice != 4); getch();}

10. INTERCONVERSION OF INFIX, POSTFIX, AND PREFIX

INFIX TO POSTFIX CONVERSION

// Program to convert an Infix form to Postfix form

#include <stdio.h>#include <conio.h>#include<string.h>#include <ctype.h>

Page 28: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

#define MAX 50

char target[MAX];char stack[MAX];char *s, *t ;int top ;char opr ;

void infix( ) ;void setexpr ( char *str ) ;void push ( char c ) ;char pop( ) ;void convert( ) ;int priority ( char c ) ;void show( ) ;

void infix( ){

top = -1 ;strcpy ( target, "" ) ;strcpy ( stack, "" ) ;t = target ;s = "" ;

}

void setexpr ( char *str ){

s = str ;}

void push ( char c ){

if ( top == MAX )printf("\nStack is full\n");

else{

top++ ;stack[top] = c ;

}}

char pop( ){

if ( top == -1 )

Page 29: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

{printf("\nStack is empty\n");return -1 ;

}else{

char item = stack[top] ;top-- ;return item ;

}}

void convert( ){

while ( *s ){

if ( *s == ' ' || *s == '\t' ){

s++ ;continue ;

}if ( isdigit ( *s ) || isalpha ( *s ) ){

while ( isdigit ( *s ) || isalpha ( *s ) ){

*t = *s ;s++ ;t++ ;

}}if ( *s == '(' ){

push ( *s ) ;s++ ;

}

if ( *s == '*' || *s == '+' || *s == '/' || *s == '%' || *s == '-' || *s == '$' ){

if ( top != -1 ){

opr = pop( ) ;while ( priority ( opr ) >= priority ( *s ) ){

*t = opr ;t++ ;opr = pop( ) ;

}

Page 30: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

if ( opr != -1)push ( opr ) ;

push ( *s ) ;}else

push ( *s ) ;s++ ;

}if ( *s == ')' )

{opr = pop( ) ;while ( ( opr ) != '(' ){

*t = opr ;t++ ;opr = pop( ) ;

}s++ ;

}}while ( top != -1 ){ {

char opr = pop( ) ;*t = opr ;t++ ;

}

*t = '\0' ;}// returns the priority of an operatorint priority ( char c ){

if ( c == '$' )return 3 ;

if ( c == '*' || c == '/' || c == '%' )return 2 ;

else{

if ( c == '+' || c == '-' )return 1 ;

elsereturn 0 ;

}}// displays the postfix form of given expr.void show( ){

printf("%s",target);

Page 31: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

}void main( ){

char expr[MAX] ;clrscr();printf("\nEnter an expression in infix form: ") ;scanf("%s",&expr);infix();setexpr (expr) ;convert( ) ;printf("\nThe postfix expression is: ");show( ) ;getch();

}

INFIX TO PREFIX CONVERSION

// Program to convert an Infix expression to Prefix form

#include <stdio.h>#include <string.h>#include <ctype.h>#define MAX 50

char target[MAX], stack[MAX] ;char *s, *t ;int top, l ;

void infix( ) ;void setexpr ( char *str ) ;void push ( char c ) ;char pop( ) ;void convert( ) ;int priority ( char c ) ;void show( ) ;

// initializes data membervoid infix( ){

top = -1 ;strcpy (target,"" ) ;strcpy (stack,"" ) ;l = 0 ;

}

Page 32: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

// reverses the given expressionvoid setexpr ( char *str ){

s = str ;strrev ( s ) ;l = strlen ( s ) ;* ( target + l ) = '\0' ;t = target + ( l - 1 ) ;

}

// adds operator to the stackvoid push ( char c ){

if ( top == MAX - 1 )printf("\nStack is full\n");

else{

top++ ;stack[top] = c ;

}}

// pops an operator from the stackchar pop( ){

if ( top == -1 ){

printf("Stack is empty\n");return -1 ;

}else{

char item = stack[top] ;top-- ;return item ;

}}

// converts the infix expr. to prefix formvoid convert( ){

char opr ;

while ( *s ){

if ( *s == ' ' || *s == '\t' ){

Page 33: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

s++ ;continue ;

}

if ( isdigit ( *s ) || isalpha ( *s ) ){

while ( isdigit ( *s ) || isalpha ( *s ) ){

*t = *s ;s++ ;t-- ;

}}

if ( *s == ')' ){

push ( *s ) ;s++ ;

}

if ( *s == '*' || *s == '+' || *s == '/' || *s == '%' || *s == '-' || *s == '$' )

{if ( top != -1 ){

opr = pop( ) ;

while ( priority ( opr ) > priority ( *s ) ){

*t = opr ;t-- ;opr = pop( ) ;printf("%c",opr );

}if(opr != -1)

push ( opr ) ;push ( *s ) ;

}else

push ( *s ) ;s++ ;

}

if ( *s == '(' ){

opr = pop( ) ;while ( ( opr ) != ')' )

Page 34: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

{*t = opr ;t-- ;opr = pop ( ) ;

}s++ ;

}}

while ( top != -1 ){

opr = pop( ) ;*t = opr ;t-- ;

}t++ ;

}

// the priotity of the operatorint priority ( char c ){

if ( c == '$' )return 3 ;

if ( c == '*' || c == '/' || c == '%' )return 2 ;

else{

if ( c == '+' || c == '-' )return 1 ;

elsereturn 0 ;

}}

// the prefix form of given expr.void show( ){

while ( *t ){

printf("%c",*t);t++ ;

}}

void main( ){

char expr[MAX] ;

Page 35: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

clrscr();printf("\nEnter an expression in infix form: ");scanf("%s",&expr);infix();setexpr( expr ) ;convert( ) ;printf("The Prefix expression is: ");show( );getch();

}

PREFIX TO POST FIX

#include <stdio.h>#include <conio.h>#include <ctype.h>#define MAX 50

char target[MAX], stack[MAX] ;char *s, *t ;int top ;char opr ;

void infix( ) ;void setexpr ( char *str ) ;void push ( char c ) ;char pop( ) ;void convert( ) ;int priority ( char c ) ;void show( ) ;

// initializes data membersvoid infix( ){

top = -1 ;strcpy ( target, "" ) ;strcpy ( stack, "" ) ;t = target ;s = "" ;

}

// sets s to point to given expr.void setexpr ( char *str ){

s = str ;

Page 36: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

}

// adds an operator to the stackvoid push ( char c ){

if ( top == MAX )printf("\nStack is full\n");

else{

top++ ;stack[top] = c ;

}}

// pops an operator from the stackchar pop( ){

if ( top == -1 ){

printf("\nStack is empty\n");return -1 ;

}else{

char item = stack[top] ;top-- ;return item ;

}}

// converts the given expr. from infix to postfix formvoid convert( ){

while ( *s ){

if ( *s == ' ' || *s == '\t' ){

s++ ;continue ;

}if ( isdigit ( *s ) || isalpha ( *s ) ){

while ( isdigit ( *s ) || isalpha ( *s ) ){

*t = *s ;s++ ;t++ ;

Page 37: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

}}if ( *s == '(' ){

push ( *s ) ;s++ ;

}

if ( *s == '*' || *s == '+' || *s == '/' || *s == '%' || *s == '-' || *s == '$' ){

if ( top != -1 ){

opr = pop( ) ;while ( priority ( opr ) >= priority ( *s ) ){

*t = opr ;t++ ;opr = pop( ) ;

}if ( opr != -1)

push ( opr ) ;push ( *s ) ;

}else

push ( *s ) ;s++ ;

}

if ( *s == ')' ){

opr = pop( ) ;while ( ( opr ) != '(' ){

*t = opr ;t++ ;opr = pop( ) ;

}s++ ;

}}

while ( top != -1 ){

char opr = pop( ) ;*t = opr ;t++ ;

}

Page 38: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

*t = '\0' ;}

// returns the priority of an operatorint priority ( char c ){

if ( c == '$' )return 3 ;

if ( c == '*' || c == '/' || c == '%' )return 2 ;

else{

if ( c == '+' || c == '-' )return 1 ;

elsereturn 0 ;

}}// displays the postfix form of given expr.void show( ){ printf("%s",target); getch();}void main( ){

char expr[MAX] ;clrscr();printf("\nEnter an expression in infix form: ");scanf("%s",&expr) ;infix();setexpr ( expr ) ;convert( ) ;printf("\nThe postfix expression is: " );show( ) ;getch();

}7. PALENDROME USING STACK

/*PROGRAM FOR PALINDROME USING STACK*/#include<stdio.h>#include<conio.h>#include<string.h>#define max 10int top=-1;char stack[max];

Page 39: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

void push(char *);void display(char *);void main(){

int ch=1;char str[10];char *t;char *s;clrscr();printf("Enter any string\n");gets(str);t=str;s=str;if(top==max-1)

printf("Stack is full");while(*t!='\0'){

top=top+1;stack[top]=*t;t++;

}while(*s!='\0'){

if(*s!=stack[top]){

ch=0;break;

}s++;top--;

}

if(ch==0)printf("It is not pallindrom");

elseprintf("It is pallindrom");

getch();}/*OUTPUTEnter any stringNARROWIt is not pallindromEnter any stringNITINIt is pallindrom*/

Page 40: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

11. QUEUE IMPLENTATION

Circular Queue

#include<stdio.h>#include<conio.h>#define MAX 100int dquee_arr[MAX];int left = -1;int right = -1;void add_right();void add_left();void del_left();void del_right();void display();void main(){

int choice,i;clrscr();for(;;){

printf("\n");for(i=0;i<=50;i++){

printf("-");delay(30);

}printf("\n ENTER 1 FOR ADD ELEMENT AT RIGHT:: ");printf("\n ENTER 2 FOR ADD ELEMENT AT LEFT:: ");printf("\n ENTER 3 FOR DELETE ELEMENT FROM RIGHT:: ");printf("\n ENTER 4 FOR DELETE ELEMENT FROM LEFT:: ");printf("\n ENTER 5 FOR DISPLAY QUEUE::");printf("\n ENTER 0 FOR EXIT:: ");scanf("%d",& choice);printf("\n");switch(choice){

case 1: add_right();break;

case 2: add_left();break;

case 3: del_right();break;

case 4: del_left();break;

case 5: display();

Page 41: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

break;case 0: exit(1);default:printf("\n ENTERE THE CORRECT CHOICE!!");

}}

}

void add_right(){

int add_ele;if((left==0 && right==MAX-1)||left==right+1)

{printf("\n queue size overflow!!");return;

}if(left==-1){

left=0;right=0;

}else

if(right==MAX-1)right=0;

elseright=right+1;

printf("\n ENTER ELEMENT TO ADD:: ");scanf("%d",&add_ele);dquee_arr[right]=add_ele;

}

void add_left(){

int add_ele;if((left==0 && right==MAX-1)||left==right+1)

{printf("\n queue size overflow!!");return;

}if(left==-1){

left=0;right=0;

}else

if(left==0)

Page 42: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

left=MAX-1;else

left=left-1;

printf("\n ENTER ELEMENT TO ADD:: ");scanf("%d",&add_ele);dquee_arr[left]=add_ele;

}

void del_left(){

if(left==-1)printf("\n QUEUE IS EMPTY!!");

elseprintf("\n DELETED ITEM IS ::%d",dquee_arr[left]);

if(left==right){

left=0;right=0;

}else{

if(left==MAX-1)left=0;elseleft=left+1;

}}

void del_right(){

if(left==-1){

printf("\n QUEUE IS EMPTY!!");return;

}else

printf("\n DELETED ITEM IS ::%d",dquee_arr[right]);if(left==right){

left=-1;right=-1;

}else{

if(right==MAX-1)

Page 43: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

right=0;elseright=right-1;

}}

void display(){

int f,r;printf("\n\n");if(left<=right)

for(f=left;f<=right;f++)printf("\n element --> %d",dquee_arr[f]);

else{

for(f=left;f<=MAX-1;f++)printf("\n element --> %d",dquee_arr[f]);

for(f=0;f<=right;f++)printf("\n element --> %d",dquee_arr[f]);

}printf("\n\n");

}

12. DeQueue

#include<stdio.h>#include<conio.h>#define MAX 100int dquee_arr[MAX];int left = -1;int right = -1;void add_right();void add_left();void del_left();void del_right();void display();void main(){

int choice,i;clrscr();for(;;){

printf("\n");for(i=0;i<=50;i++){

printf("-");

Page 44: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

delay(30);}printf("\n ENTER 1 FOR ADD ELEMENT AT RIGHT:: ");printf("\n ENTER 2 FOR ADD ELEMENT AT LEFT:: ");printf("\n ENTER 3 FOR DELETE ELEMENT FROM RIGHT:: ");printf("\n ENTER 4 FOR DELETE ELEMENT FROM LEFT:: ");printf("\n ENTER 5 FOR DISPLAY QUEUE::");printf("\n ENTER 0 FOR EXIT:: ");scanf("%d",& choice);printf("\n");switch(choice){

case 1: add_right();break;

case 2: add_left();break;

case 3: del_right();break;

case 4: del_left();break;

case 5: display();break;

case 0: exit(1);default:printf("\n ENTERE THE CORRECT CHOICE!!");

}}

}

void add_right(){

int add_ele;if((left==0 && right==MAX-1)||left==right+1)

{printf("\n queue size overflow!!");return;

}if(left==-1){

left=0;right=0;

}else

if(right==MAX-1)right=0;

elseright=right+1;

Page 45: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

printf("\n ENTER ELEMENT TO ADD:: ");scanf("%d",&add_ele);dquee_arr[right]=add_ele;

}

void add_left(){

int add_ele;if((left==0 && right==MAX-1)||left==right+1)

{printf("\n queue size overflow!!");return;

}if(left==-1){

left=0;right=0;

}else

if(left==0)left=MAX-1;

elseleft=left-1;

printf("\n ENTER ELEMENT TO ADD:: ");scanf("%d",&add_ele);dquee_arr[left]=add_ele;

}

void del_left(){

if(left==-1)printf("\n QUEUE IS EMPTY!!");

elseprintf("\n DELETED ITEM IS ::%d",dquee_arr[left]);

if(left==right){

left=0;right=0;

}else{

if(left==MAX-1)left=0;else

Page 46: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

left=left+1;}

}

void del_right(){

if(left==-1){

printf("\n QUEUE IS EMPTY!!");return;

}else

printf("\n DELETED ITEM IS ::%d",dquee_arr[right]);if(left==right){

left=-1;right=-1;

}else{

if(right==MAX-1)right=0;elseright=right-1;

}}

void display(){

int f,r;printf("\n\n");if(left<=right)

for(f=left;f<=right;f++)printf("\n element --> %d",dquee_arr[f]);

else{

for(f=left;f<=MAX-1;f++)printf("\n element --> %d",dquee_arr[f]);

for(f=0;f<=right;f++)printf("\n element --> %d",dquee_arr[f]);

}printf("\n\n");

}

Page 47: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

13. JOB SCHEDULING

// Program that implements a priority queue using an array#include <string.h>#include <conio.h>#define MAX 5struct data{

char job[MAX] ;int prno ;int ord ;

}d[MAX] ;

int front, rear ;void add ( struct data dt ) ;struct data remove( ) ;

// adds item to the priority queuevoid add ( struct data dt ){

int i,j;struct data temp ;if ( rear == MAX - 1 ){

printf("\nQueue is full") ;return ;

}rear++ ;d[rear] = dt ;

if ( front == -1 )front = 0 ;

for ( i = front ; i <= rear ; i++ ){

for ( j = i + 1 ; j <= rear ; j++ ){

if ( d[i].prno > d[j].prno ){

temp = d[i] ;d[i] = d[j] ;d[j] = temp ;

}else

Page 48: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

{if ( d[i].prno == d[j].prno ){

if ( d[i].ord > d[j].ord ){

temp = d[i] ;d[i] = d[j] ;d[j] = temp ;

}}

}}

}}

// removes item from priority queuestruct data remove( ){

struct data t ,t1;strcpy ( t.job, "" ) ;t.prno = 0 ;t.ord = 0 ;

if ( front == -1 ){

printf("\nQueue is Empty") ;return t ;

}

t1 = d[front] ;d[front] = t ;if ( front == rear )

front = rear = -1 ;else

front++ ;

return t1 ;}

void main( ){

struct data dt,temp;int i,j = 0 ;front = rear = -1;clrscr();printf("Enter Job description (max 4 chars) and its priority\n");printf("Lower the priority number, higher the priority") ;

Page 49: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

printf("\nJob Priority\n");

for ( i = 0 ; i < MAX ; i++ ){

scanf("%s%d", &dt.job,&dt.prno);dt.ord = j++;add(dt);

}printf("\nProcess jobs prioritywise\n");printf("Job Priority\n");

for ( i = 0 ; i < MAX ; i++ ){

temp = remove( ) ;printf("%s\t\t%d\n", temp.job,temp.prno);

}getch();

}

14. EVALUATION OF POSTFIX EXPRESSION

// Program to evaluate an epression entered in postfix form

#include <stdio.h>#include <conio.h>#include <math.h>#include <ctype.h>#define MAX 50

int stack[MAX] ;int top, nn,data;char *s ;

void postfix( ) ;void setexpr ( char *str ) ;void push ( int item ) ;int pop( ) ;void calculate( ) ;void show( ) ;

void postfix( ){

Page 50: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

top = -1 ;}

void setexpr ( char *str ){

s = str ;}

void push ( int item ){

if ( top == MAX - 1 )printf("Stack is full");

else{

top++ ;stack[top] = item ;

}}

int pop( ){

if ( top == -1 ){

printf("Stack is empty");return NULL ;

}data = stack[top] ;top-- ;return data ;

}

void calculate( ){

int n1, n2, n3 ;while ( *s ){

/* skip whitespace, if any*/if ( *s == ' ' || *s == '\t' ){

s++ ;continue ;

}

Page 51: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

/* if digit is encountered */if ( isdigit ( *s ) ){

nn = *s- '0';push ( nn ) ;

}else{

/* if operator is encountered */n1 = pop( ) ;n2 = pop( ) ;switch ( *s ){

case '+' : n3 = n2 + n1 ; break ;

case '-' : n3 = n2 - n1 ; break ;

case '/' : n3 = n2 / n1 ; break ;

case '*' : n3 = n2 * n1 ; break ;

case '%' : n3 = n2 % n1 ; break ;

case '$' : n3 = pow ( n2 , n1 ) ; break ;

default : printf("Unknown operator"); exit ( 1 ) ;

}

push ( n3 ) ;}s++ ;

}}

Page 52: ds lab1

NAME :Pindariya bhavesh p SUBJECT : DSCOURSE: MCA ROLL NO: B-29

// displays the resultvoid show( ){

nn = pop ( ) ;printf("\nResult is:%d ",nn);

}

void main( ){

char expr[MAX] ;clrscr();printf("\nEnter postfix expression to be evaluated : ") ;scanf("%s",expr);clrscr();postfix ();setexpr ( expr ) ;calculate( ) ;show( ) ;getch();

}