Professional Documents
Culture Documents
LAB ASSESSMENT-1
void push(void);
void pop(void);
void display(void);
struct stack
{
int a[10];
int top;
}st; // st is the variable name for structure stack
void main()
{
int choice;
st.top=-1;
do
{
printf("\n1.push\n2.pop\n3.display\n4.exit");
printf("\nenter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
while (choice<4);
}
// getch - stays on the output terminal even after giving 1 input
getch();
void push()
{
int item;
if (st.top==9)
printf("\nstack overlow");
else
{
printf("\nenter the item to push: ");
scanf("%d",&item);
st.a[++st.top]=item;
}
}
void pop()
{
int item;
if (st.top==-1)
printf("\nstack underflow");
else
{
item=st.a[st.top];
st.top--;
printf("\nthe deleted item is %d", item);
}
}
void display()
{
int temp;
if (st.top==-1)
printf("\nthe stack is empty");
else
{
temp=st.top;
while(temp>=0)
{
printf("%d\t",st.a[temp]);
temp--;
}
}
Sample input/output:
2. Infix to postfix conversion
Code:
// postfix conversion
#include <stdio.h>
#include <ctype.h>
struct stack
{
char s[100];
int top;
}st;
void push(char x)
{
st.s[++st.top]=x;
}
char pop()
{
if(st.top==-1)
printf("stack underflow");
else
return st.s[st.top--];
}
int priority(char x)
{
if(x=='(')
return 0;
if(x=='+'|| x=='-')
return 1;
if(x=='*' || x=='/')
return 2;
}
int main()
{
char infix[100];
char *e,x;
printf("Enter the the infix expression: ");
scanf("%s",infix);
printf("\n");
e=infix;
while(*e!='\0')
{
if(isalnum(*e))
printf("%c",*e);
else if(*e=='(')
push(*e);
else if (*e==')')
{
while(x=pop()!='(')
printf("%c",x);
}
else
{
while(priority(st.s[st.top])>=priority(*e))
printf("%c",pop());
push(*e);
}
}
while(st.top!=-1)
printf("%c",pop());
return 0;
}
Sample input/output:
void pfix();
void push(int);
char pop();
char infix[20],prefix[20],revexp[20];
struct stack
{
int s[size];
int top;
}st;
int main( )
{
st.top=0;
printf("enter the infix expression:");
scanf("%s",infix);
strrev(infix);
strcpy(revexp,infix);
pfix();
//getch();
return 0;
}
void pfix()
{
int i,j=0;
for(i=0;revexp[i]!='\0';i++)
{
switch(revexp[i])
{
case '+':
{
while(st.s[st.top]>=1)
prefix[j++]=pop();
push(1);
break;
}
case '-':
{
while(st.s[st.top]>=1)
prefix[j++]=pop();
push(2);
break;
}
case '*':
{
while(st.s[st.top]>=3)
prefix[j++]=pop();
push(3);
break;
}
case '/':
{
while(st.s[st.top]>=3)
prefix[j++]=pop();
push(4);
break;
}
case '^':
{
while(st.s[st.top]>=4)
prefix[j++]=pop();
push(5);
break;
}
case ')':
{
push(0);
break;
}
case '(':
{
while(st.s[st.top]!=0)
prefix[j++]=pop();
st.top--;
break;
}
default :prefix[j++]=revexp[i];
}
}while(st.top>0)
prefix[j++]=pop();
printf("the prefix expression is: %s",prefix);
}
void push(int ele)
{
st.top++;
st.s[st.top]=ele;
}
char pop()
{
int el;
char e;
el=st.s[st.top];
st.top--;
switch(el)
{
case 1:e='+';
break;
case 2:e='-';
break;
case 3:e='*';
break;
case 4:e='/';
break;
case 5:e='^';
break;
}
return(e);
}
Sample input/output:
4. Evaluation of prefix
Code:
#include<stdio.h>
#include<string.h>
int s[50];
int top=0;
void push(int ch);
int pop();
int main(){
int a,b,c,i;
char prefix[50];
printf("Enter the prefix expression: \n");
gets(prefix);
for(i=strlen(prefix)-1;i>=0;i--){
if(prefix[i]=='+'){
c=pop()+pop();
push(c);
}
else if(prefix[i]=='-'){
a=pop();
b=pop();
c=b-a;
push(c);
}
else if(prefix[i]=='*'){
a=pop();
b=pop();
c=b*a;
push(c);
}
else if(prefix[i]=='/'){
a=pop();
b=pop();
c=b/a;
push(c);
}
else{
push(prefix[i]-48);
}
}
printf("The final answer is: \n %d",pop());
return 0;
}
void push(int ch){
top++;
s[top]=ch;
}
int pop(){
int ch;
ch=s[top];
top=top-1;
return(ch);
}
Sample input/output:
5. Evaluation of postfix
Code:
#include<stdio.h>
int stack[20];
int top=-1;
int pop(){
return stack [top--];
}
int main(){
char exp[20];
int *e;
int n1,n2,n3,num;
printf("enter the infix expression\n");
scanf("%d",&exp);
e=exp;
while(*e!='\0')
if(isdigit(e)){
num = *e-48;
push(num);
}
else{
push(n1);
push(n2);
switch(*e)
{
case '+':
{
n3 = n1 + n2;
break;
}
case '-':
{
n3 = n2 - n1;
break;
}
case '*':
{
n3 = n1 * n2;
break;
}
case '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
Sample input/output:
int main()
{
int choice;
q.f=0;
q.r=-1;
do
{
printf("\n1.Add\n2.Delete\n3.Display\n4.Exit");
printf("\nEnter the operation you wish to do: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
add();break;
case 2:
del();break;
case 3:
display();break;
}
}
while (choice<4);
}
void add()
{
int item;
if (q.r==4)
{
printf("queue is full");
}
else
{
printf("Enter the number you wish to add: ");
scanf("%d",&item);
q.r=q.r+1;
q.arr[q.r]=item;
}
}
void del()
{
if(q.f>q.r)
{
printf("queue is empty");
}
else
{
printf("the deleted number is %d",q.arr[q.f]);
q.f++;
}
}
void display()
{
int temp;
if (q.f>q.r)
{
printf("Queue is empty!");
}
else
{
temp=q.f;
while(temp<=q.r)
{
printf("%d\t",q.arr[temp]);
temp++;
}
}
}
Sample input/output:
7. Implementation of circular queue
Code:
// circular queue implementation
#include <stdio.h>
#include <stdlib.h>
#define size 5
void push(void);
void del(void);
void display(void);
struct cirqueue
{
int arr[size];
int R;
int F;
int te;
}cq;
int main()
{
cq.R=-1,cq.F=0,cq.te=0;
int ch;
do
{
printf("\n1. Add");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
add(); break;
case 2:
del();break;
case 3:
display();break;
default:
printf("\nWrong Choice");
}
}
while (ch<4);
return 0;
}
void add()
{
int n;
if(cq.te==size)
{
printf("Queue is full");
}
else
{
printf("Enter a number ");
scanf("%d",&n);
cq.R=(cq.R+1)%size;
cq.arr[cq.R]=n;
cq.te=cq.te+1;
}
}
void del()
{
if(cq.te==0)
{
printf("Queue is empty");
}
else
{
printf("Number Deleted = %d",cq.arr[cq.F]);
cq.F=(cq.F+1)%size;
cq.te=cq.te-1;
}
void display()
{
int x,i;
if(cq.te==0)
{
printf("Queue is empty");
}
else
{
x=cq.F;
for(i=1; i<=cq.te; i++)
{
printf("%d ",cq.arr[x]);
x=(x+1)%size;
}
}
}
Sample input/output:
8. Implementation of priority queue
Code:
// implementation of 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;
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");
}
}
}
pri_que[i] = -99;
rear--;
if (rear == -1)
front = -1;
return;
}
}
printf("\n%d not found in queue to delete", data);
}
front = 0;
}
Sample input/output:
9. Implementation of Deque
Code:
// implementation of deque
// chaitanya goenka
#include <stdio.h>
#include <stdlib.h>
#define size 5
void addRear(void);
void addFront(void);
void delRear(void);
void delFront(void);
struct deque
{
int arr[size];
int F,R,te;
}dq;
int main()
{
dq.F=0;
dq.R=-1;
dq.te=0;
int choice;
do
{
printf("\n1.addRear\n2.addFront\n3.delFront\n4.delRear\n5.display");
printf("\n Enter the operation you wish to do: ");
scanf("%d",&choice);
switch(choice)
{
case 1: addRear(); break;
case 2: addFront(); break;
case 3: delFront(); break;
case 4: delRear(); break;
case 5: display(); break;
}
}
while(choice<6);
}
void addRear()
{
int n;
if(dq.te==size)
{
printf("Queue is full");
}
else
{
printf("Enter a number ");
scanf("%d",&n);
dq.R=(dq.R+1)%size;
dq.arr[dq.R]=n;
dq.te=dq.te+1;
}
}
void delRear()
{
if(dq.te==0)
{
printf("Queue is empty");
}
else
{
if(dq.R==-1)
{
dq.R=size-1;
}
printf("Number Deleted From Rear End = %d",dq.arr[dq.R]);
dq.R=dq.R-1;
dq.te=dq.te-1;
}
}
void addFront()
{
int n;
if(dq.te==size)
{
printf("Queue is full");
}
else
{
printf("Enter a number ");
scanf("%d",&n);
if(dq.F==0)
{
dq.F=size-1;
}
else
{
dq.F=dq.F-1;
}
dq.arr[dq.F]=n;
dq.te=dq.te+1;
}
}
void delFront()
{
if(dq.te==0)
{
printf("Queue is empty");
}
else
{
printf("Number Deleted From Front End = %d",dq.arr[dq.F]);
dq.F=(dq.F+1)%size;
dq.te=dq.te-1;
}
}
void display()
{
int x,i;
if(dq.te==0)
{
printf("Queue is empty");
}
else
{
x=dq.F;
for(i=1; i<=dq.te; i++)
{
printf("%d ",dq.arr[x]);
x=(x+1)%size;
}
}
}
Sample input/output:
10. Implementation of singly linked list
Code:
// implementation of singly linked list
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
void insertion(void);
void creation(void);
void deletion(void);
void display(void);
struct node
{
int data,n;
struct node *next;
}sn; struct node *head,*tail,*temp1,*temp2,*nw;
int main()
{
int choice;
do
{
printf("\n1.creation\n2.insertion\n3.deletion\n4.display");
printf("\nEnter the operation you wish to do: ");
scanf("%d",&choice);
switch(choice)
{
case 1: creation(); break;
case 2: insertion(); break;
case 3: deletion(); break;
case 4: display(); break;
}
} while(choice<5);
return 0;
}
void creation()
{
int i,n,item;
printf("Enter the number of nodes: ");
scanf("%d",&sn.n);
nw=(struct node*)malloc(sizeof(struct node));
nw->data=0;
nw->next=NULL;
head=nw;
tail=nw;
for(i=0;i<sn.n;i++)
{
printf("Enter the data: ");
scanf("%d",&item);
if(nw->data==0)
{
nw->data=item;
}
else
{
nw=(struct node*)malloc(sizeof(struct node));
nw->data=item;
nw->next=NULL;
tail->next=nw;
tail=nw;
}
}
}
void insertion()
{
int i,pos,item;
printf("Enter the position: ");
scanf("%d",&pos);
printf("Enter the item to be inserted: ");
scanf("%d",&item);
if(pos==0) // insertion at beginning
{
nw=(struct node*)malloc(sizeof(struct node));
nw->data=item;
nw->next=head;
head=nw;
sn.n+=1;
}
else if(pos==sn.n-1) // insertion at end
{
nw=(struct node*)malloc(sizeof(struct node));
nw->data=item;
tail->next=nw;
tail=nw;
sn.n+=1;
}
else // insertion at middle
{
temp1=head;
for(i=0;i<pos-1;i++)
temp1=temp1->next;
temp2=temp1->next;
nw=(struct node*)malloc(sizeof(struct node));
nw->data=item;
temp1->next=nw;
nw->next=temp2;
sn.n+=1;
void deletion()
{
int pos,i;
printf("enter position: ");
scanf("%d",&pos);
if(pos==0) // deletion at beginning
{
temp1=head;
head=temp1->next;
temp1->next=NULL;
sn.n-=1;
}
else if(pos==sn.n-1) //deletion at end
{
temp1=head;
for(i=0;i<sn.n-2;i++);
temp1=temp1->next;
tail=temp1;
temp1=tail->next;
tail->next=NULL;
sn.n-=1;
}
else // deletion at beginning
{
temp1=head;
for(i=0;i<pos-1;i++)
temp1=temp1->next;
temp2=temp1->next;
temp1->next=temp2->next;
temp2->next=NULL;
free(temp2);
sn.n-=1;
}
void display()
{
temp1=head;
while(temp1!=NULL)
{
printf("%d\t",temp1->data);
temp1=temp1->next;
}
}
Sample input/output:
11. Implementation of double linked list
Code:
// implementation of doubly linked list
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
void insertion(void);
void creation(void);
void deletion(void);
void display(void);
struct node
{
int data,n;
struct node *next,*prev;
}dl; struct node *head,*tail,*temp1,*temp2,*nw;
int main()
{
int choice;
do
{
printf("\n1.creation\n2.insertion\n3.deletion\n4.display");
printf("\nEnter the operation you wish to do: ");
scanf("%d",&choice);
switch(choice)
{
case 1: creation(); break;
case 2: insertion(); break;
case 3: deletion(); break;
case 4: display(); break;
}
} while(choice<5);
return 0;
}
void creation()
{
int item,i;
printf("Enter the number of nodes: ");
scanf("%d",&dl.n);
nw=(struct node*)malloc(sizeof(struct node));
nw->data=0;
nw->next=NULL;
nw->prev=NULL;
head=nw;
tail=nw;
for(i=0;i<dl.n;i++)
{
printf("Enter data: ");
scanf("%d",&item);
if(head->data==0)
head->data=item;
else
{
nw=(struct node*)malloc(sizeof(struct node));
nw->data=item;
tail->next=nw;
nw->prev=tail;
tail=tail->next;
tail=nw;
}
}
void insertion()
{
int i,item,pos;
printf("Enter the position: ");
scanf("%d",&pos);
printf("Enter item: ");
scanf("%d",&item);
nw=(struct node*)malloc(sizeof(struct node));
if(pos==0)// insertion at beginning
{
nw->data=item;
nw->next=head;
head->prev=nw;
head=nw;
dl.n+=1;
}
else if(pos==dl.n-1) // insertion at end
{
nw->data=item;
nw->next=NULL;
tail->next=nw;
tail=nw;
dl.n+=1;
}
else // insertion at middle
{
temp1=head;
for(i=0;i<pos-1;i++)
temp1=temp1->next;
temp2=temp1->next;
nw->data=item;
temp1->next=nw;
nw->next=temp2;
nw->prev=temp1;
temp2->prev=nw;
dl.n+=1;
}
}
void deletion()
{
int pos,i;
printf("Enter the position: ");
scanf("%d",&pos);
if(pos==0) // deletion at beginning
{
temp1=head;
head=temp1->next;
temp1->next=NULL;
head->prev=NULL;
free(temp1);
dl.n-=1;
}
else if (pos==dl.n-1) // deletion at end
{
temp1=tail;
tail=temp1->prev;
temp1->prev=NULL;
tail->next=NULL;
free(temp1);
dl.n-=1;
}
else
{
temp1=head;
for(i=0;i<pos-1;i++)
temp1=temp1->next;
temp2=temp1->next;
temp1->next=temp2->next;
temp2->prev=NULL;
temp2=temp1->next;
temp2->prev=temp1;
dl.n-=1;
}
}
void display()
{
temp1=head;
while(temp1!=NULL)
{
printf("%d\t",temp1->data);
temp1=temp1->next;
}
}
Sample input/output:
void creation(void);
void push(void);
void pop(void);
void display(void);
struct node
{
int data;
struct node *next;
}; struct node *temp,*top,*nw;
int main()
{
int choice;
do
{
printf("\n1.creation\n2.push\n3.pop\n4.display");
printf("\nEnter the operation you wish to do: ");
scanf("%d",&choice);
switch(choice)
{
case 1: creation(); break;
case 2: push(); break;
case 3: pop(); break;
case 4: display(); break;
}
} while(choice<5);
return 0;
}
void creation()
{
int item;
nw=(struct node*)malloc(sizeof(struct node));
printf("Enter the item to be pushed: ");
scanf("%d",&item);
nw->data=item;
nw->next=NULL;
top=nw;
void display()
{
temp=top;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}
Sample input/output:
struct node
{
int data;
struct node *next;
};
struct node *rear,*front,*temp,*nw;
int main()
{
int choice;
do
{
printf("\n1.creation\n2.insertion\n3.deletion\n4.display");
printf("\nEnter the operation you wish to do: ");
scanf("%d",&choice);
switch(choice)
{
case 1: creation(); break;
case 2: insertion(); break;
case 3: deletion(); break;
case 4: display(); break;
}
} while(choice<5);
return 0;
}
void creation()
{
int item;
printf("Enter the data: ");
scanf("%d",&item);
nw=(struct node *)malloc(sizeof(struct node));
nw->data=item;
nw->next=NULL;
rear=nw;
front=nw;
}
void insertion()
{
int item;
printf("Enter the item to be added: ");
scanf("%d",&item);
if(nw==0)
nw->data=item;
else
{
nw=(struct node *)malloc(sizeof(struct node));
nw->data=item;
rear->next=nw;
rear=nw;
}
}
void deletion()
{
front=front->next;
}
void display()
{
temp=front;
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->next;
}
}
Sample input/output:
14. Polynomial addition using linked list
Code:
// implementation of polynomial addition
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
void add(void);
void creation1(void);
void creation2(void);
struct node
{
int coeff,power;
struct node *next;
}; struct node *ptr1,*ptr2;*nw,*ptr3;
int main()
{
int choice;
do
{
printf("\n1.creation of list 1\n2.creation of list 2\n3.add both the
lists");
printf("\nEnter the operation you wish to do: ");
scanf("%d",&choice);
switch(choice)
{
case 1: creation1(); break;
case 2: creation2(); break;
case 3: add(); break;
}
} while (choice<4);
return 0;
}
void creation1()
{
int coeff1,power1,i,n;
printf("Enter the number of nodes: ");
scanf("%d",&n);
nw=(struct node*)malloc(sizeof(struct node));
nw->coeff=0;
nw->power=0;
nw->ptr1=NULL;
ptr1=nw;
for(i=0;i<n;i++)
{
printf("enter coefficient: ");
scanf("%d",&coeff1);
printf("enter power1: ");
scnaf("%d",&power1);
if(nw->coeff==0 && nw->power==0)
{
nw->coeff=coeff1;
nw->power=power1;
}
else
{
nw=(struct node*)malloc(sizeof(struct node));
nw->coeff=coeff1;
nw->power=power1;
nw->next=ptr1;
ptr1=nw;
}
}
}
void creation2()
{
int coeff2,power2,i,n;
printf("Enter the number of nodes: ")
scanf("%d",&n);
nw=(struct node*)malloc(sizeof(struct node));
nw->coeff=0;
nw->power=0;
nw->ptr2=NULL;
ptr2=nw;
for(i=0;i<n;i++)
{
printf("enter coefficient: ");
scanf("%d",&coeff2);
printf("enter power: ");
scnaf("%d",&power2);
if(nw->coeff==0 && nw->power==0)
{
nw->coeff=coeff2;
nw->power=power2;
}
else
{
nw=(struct node*)malloc(sizeof(struct node));
nw->coeff=coeff2;
nw->power=power2;
nw->next=ptr2;
ptr2=nw;
}
}
}
void add()
{
while(ptr1!=NULL && ptr2!=NULL)
{
nw=(struct node*)malloc(sizeof(struct node));
if(ptr1->power==ptr->power)
{
nw->coeff=ptr1->coeff+ptr2->coeff;
nw->power=ptr1->power;
nw->next=NULL;
nw=ptr3;
ptr1=ptr1->next;
ptr2=ptr2->next;
}
else if (ptr1->power>ptr2->power)
{
nw->coeff=ptr1->coeff;
nw->power=ptr1->power;
ptr3->next=nw;
ptr3=nw;
ptr1=ptr1->next;
}
else
{
nw->coeff=ptr2->coeff;
nw->power=ptr2->power;
ptr3->next=nw;
ptr2=nw;
ptr2=ptr2->next;
}
}
}