Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 50

SCHOOL OF INFORMATION TECHNOLOGY AND

ENGINEERING

DATA STRUCTURES

LAB CYCLE SHEET-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~

 NAME: ANANDA KUMAR V.


 REG.NO: 21BCA0064.
 PROGRAMME: BCA.
 SLOT: C1+TC1.
 COURSE CODE: ITA3002.
 FACULTY NAME: PROF.NANCY VECTOR.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~
To be continued from cycle sheet-1.

1) Write a C program to implement various operations on queues using


array.

CODE:

#include<stdio.h>
#include<stdlib.h>
#define MAX 3
void push();
void pop();
void display();
int que[MAX],item,f=-1,r=-1,i;
int main()
{
printf("--------------------QUEUE
OPERATIONS------------------------\n");
printf("1.) PUSH OPERATION\n");
printf("2.) POP OPERATION\n");
printf("3.) DISPALY OPERATION\n");
printf("4.) EXIT\n");

printf("-------------------------------------------------
-----------\n");
int ch;
while(1)
{
printf("ENTER THE OPERATION TO DO ON QUEUE:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(1);
break;
default:
printf("WRONG CHOICE");
}
}
}
void push()
{
if(r==MAX-1)
{
printf("QUEUE IS OVERFLOWING.\n");
}
if(f==-1)
{
f=0;
}
printf("ENTER THE ELEMENT IN THE QUEUE:\n");
scanf("%d",&item);
r=r+1;
que[r]=item;
}
void pop()
{
if(f<0 || f>r)
{
printf("QUEUE IS UNDERFLOWING.\n");
}
else
{
printf("THE POPPED ELEMENT IS:%d\n",que[f]);
f=f+1;
}
}
void display()
{
if(f==-1)
{
printf("THE QUEUE IS EMPTY.\n");
}
else
for(i=f; i<=r; i++)
{
printf("%d",que[i]);
}
printf("\n");
}
SCREENSHOT OF THE OUTPUT:
2) Write a C program to evaluate postfix expression.

CODE:

#include<stdio.h>
#include<string.h>
void push(int);
int pop();
int st[100];
int top=-1;
int main()
{
char postfix[100];
int i,num,n1,n2,n3,res;
printf("ENTER THE POSTFIX EXPRESSION:\n");
scanf("%s",postfix);
for(i=0; postfix[i]!='\0';i++)
{
if(postfix[i]>='0'&&postfix[i]<='9')
{
num=postfix[i]-48;
push(num);
}
else
{
n1=pop();
n2=pop();
switch(postfix[i])
{
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;
}
push(n3);
}
}
res=pop();
printf("THE EVALUATION OF POSTFIX EXPRESSION IS: %d\
n",res);
}
void push(int el)
{
top=top+1;
st[top]=el;
}
int pop()
{
int el;
el=st[top];
top=top-1;
return el;
}

SCREENSHOT OF THE OUTPUT:

3) Write a C program to convert infix expression to its equivalent prefix


form.
CODE:

#include <stdio.h>
#include<string.h>
#include<ctype.h>
void push(char);
char pop();
int priority(char);
char st[100];
int top=-1;
int main()
{
char infix[100],postfix[100];
int i,p=0;
char x;
printf("ENTER THE INFIX EXPRESSION:");
scanf("%s",infix);
for(i=0;infix[i]!='\0';i++)
{
if(isalnum(infix[i]))
{
postfix[p]=infix[i];
p++;
}
else if(infix[i]=='(')
{
push(infix[i]);
}
else if(infix[i]==')')
{
while((x=pop())!='(')
{
postfix[p]=x;
p++;
}
}
else
{
while(priority(st[top])>=priority(infix[i]))
{
postfix[p]=pop();
p++;
}
push(infix[i]);
}
}
while(top!=-1)
{
postfix[p]=pop();
p++;
}
postfix[p]='\0';
printf("\nTHE POSTFIX EXPRESSION IS %s\n",postfix);
strrev(postfix);
printf("\nTHE PREFIX EXPRESSION IS %s",postfix);
}
void push(char c)
{
top=top+1;
st[top]=c;
}
char pop()
{
char ch;
if(top==-1)
{
return -1;
}
else
{
ch=st[top];
top=top-1;
return ch;
}
}
int priority(char c)
{
if(c=='(')
{
return 0;
}
if(c=='+'||c=='-')
{
return 1;
}
if(c=='/'||c=='*')
{
return 2;
}
if(c=='^')
{
return 3;
}
return 0;
}

SCREENSHOT OF THE OUTPUT:

4) Write a program to implement stack using linked list.

CODE:

#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
int main ()
{
int choice=0;
printf("\n~~~~~~~~~~~~~~~~~~Stack operations using
linked list~~~~~~~~~~~~~~~~~~\n");
while(choice != 4)
{
printf("\n1.PUSH\n2.POP\n3.SHOW\n4.EXIT\n");
printf("ENTER YOUR CHOICE:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("EXITING..");
break;
}
default:
{
printf("WRONG CHOICE");
}
};
}
}
void push ()
{
int val;
struct node *ptr = (struct
node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("NOT ABLE TO PUSH");
}
else
{
printf("ENTER THE ITEM:");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;

}
printf("ITEM IS PUSHED INTO THE STACK");

}
}

void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("UNDERFLOWING");
}
else
{
item = head->val;
ptr = head;
head = head->next;
printf("POPPED ELEMENT IS: %d\n",ptr->val);
free(ptr);
printf("ITEM IS POPPED\n");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("STACK IS EMPTY\n");
}
else
{
printf("ELEMENTS OF THE STACK IS:\n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
SCREENSHOT OF THE OUTPUT:
5) Write a program to implement queue using linked list.

CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void del();
void display();
void main ()
{
int choice;
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~Queue Operations
Using Linked List~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
while(choice != 4)
{
printf("\n1.INSERT AN ELEMENT\n2.DELETE AN
ELEMENT\n3.DISPLAY THE ELEMENTS IN QUEUE\n4.EXIT\n");
printf("ENTER YOUR CHOICE:\n");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("WRONG CHOICE");
}
}
}
void insert()
{
struct node *ptr;
int item;

ptr = (struct node *) malloc (sizeof(struct node));


if(ptr == NULL)
{
printf("OVERFLOWING\n");
return;
}
else
{
printf("ENTER THE VALUE:\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front=ptr;
rear=ptr;
front->next=NULL;
rear->next=NULL;
}
else
{
rear->next=ptr;
rear=ptr;
rear->next = NULL;
}
}
}
void del()
{
struct node *ptr;
if(front == NULL)
{
printf("UNDERFLOWING\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front==NULL)
{
printf("QUEUE IS EMPTY\n");
}
else
{ printf("ELEMENTS OF THE QUEUE:\n");
while(ptr!=NULL)
{
printf("%d\n",ptr -> data);
ptr=ptr->next;
}
}
}

SCREENSHOT OF THE OUTPUT:


6. Write a program to perform all the operations in a circular linked list.

CODE:

#include<stdio.h>
#include<stdlib.h>
void dis();
void addbeg(int);
void addbet(int);
void createlist(int);
void del();
struct node
{
int info;
struct node *next;
}*last;
int main()
{
int ch,n,i,item;
while(1)
{
printf("~~~~~~~~~~~~~~~~~~ENTER THE
CHOICE~~~~~~~~~~~~~~~~~~~~\n");
printf("1) CREATE\n");
printf("2) ADD BEGNNING\n");
printf("3) ADD BETWEEN\n");
printf("4) DELETE\n");
printf("5) DISPLAY\n");
printf("6) QUIT\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~\n");
printf("ENTER THE OPERATION TO DO ON CIRCULAR LINKED
LISTS\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("ENTER THE NO OF NODES:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("ENTER THE ITEM TO INSERTED:\n");
scanf("%d",&item);
createlist(item);
}
break;
case 2:
printf("ENTER THE ITEM:\n");
scanf("%d",&item);
addbeg(item);
break;
case 3:
printf("ENTER THE ITEM:\n");
scanf("%d",&item);
addbet(item);
break;
case 4:
del();
break;
case 5:
dis();
break;
case 6:
exit(1);
break;
default:
printf("WRONG CHOICE");

}
}
}
void addbeg(int item)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->info=item;
tmp->next=last->next;
last->next=tmp;
}
void addbet(int item)
{
int pos,i;
struct node*tmp,*ptr;
tmp=malloc(sizeof(struct node));
tmp->info=item;
printf("ENTER THE POSITION AFTER THE ELEMENT NEEDS TO
BE INSERTED:\n");
scanf("%d",&pos);
ptr=last->next;
for(i=0;i<pos-1;i++)
{
ptr=ptr->next;
tmp->next=ptr->next;
ptr->next=tmp;
}
}
void createlist(int item)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->info=item;
if(last==NULL)
{
last=tmp;
tmp->next=last;
}
else
{
tmp->next=last->next;
last->next=tmp;
last=tmp;
}
}
void del()
{
struct node *tmp,*ptr;
int item;
printf("ENTER THE ITEM TO BE DELETED:\n");
scanf("%d",&item);
if(last->next==last && last->info==item)
{
tmp=last;
free(tmp);
last=NULL;
return;
}
ptr=last->next;
if(ptr->info==item)
{
tmp=ptr;
last->next=tmp->next;
free(tmp);
return;
}
while(ptr->next!=last)
{
if(ptr->next->info==item)
{
tmp=ptr->next;
ptr->next=tmp->next;
free(tmp);
return;
}
else
{
ptr=ptr->next;
}
}
if(ptr->next->info==item)
{
tmp=ptr->next;
ptr->next=tmp->next;
last=ptr;
free(tmp);
return;
}
}
void dis()
{
struct node *ptr;
int n=1;
ptr=last->next;
while(ptr!=last)
{
printf("\nITEM IN THE %d NODE:%d \n",n,ptr->info);
n++;
ptr=ptr->next;
}
printf("\nITEM IN THE %d NODE:%d \n",n,ptr->info);
}

SCREENSHOT OF THE OUTPUT:


7.) Write a program to create a singly linked list and perform the following
operations
i. Insert at the beginning
ii. Insert at the end
iii. Insert at any position
iv. Delete at the beginning
v. Delete at the end.
vi. Delete at any position
vii. Display
viii. To search for a particular element. ix. To count the number of nodes in the
List.

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void createlist(int);
void addbeg(int);
void addbet(int);
void dis();
void del();
void search();
void count();
struct node
{
int info;
struct node *next;
}*start;
int main()
{
int ch,n,i,item,sear=0,total;
while(1)
{
printf("\n~~~~~~~~~~~~~~~~~~ENTER THE
CHOICE~~~~~~~~~~~~~~~~~~~~\n");
printf("1) CREATE\n");
printf("2) ADD BEGNNING\n");
printf("3) ADD BETWEEN\n");
printf("4) DISPLAY\n");
printf("5) COUNT\n");
printf("6) DELETE\n");
printf("7) SEARCH\n");
printf("8) QUIT\n");

printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~\n");
printf("ENTER THE OPERATION TO DO ON LINKED LISTS\
n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("ENTER THE NO OF NODES:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("ENTER THE ITEM TO INSERTED:\n");
scanf("%d",&item);
createlist(item);
}
break;
case 2:
printf("ENTER THE ITEM:\n");
scanf("%d",&item);
addbeg(item);
break;
case 3:
printf("ENTER THE ITEM:\n");
scanf("%d",&item);
addbet(item);
case 4:
dis();
break;
case 5:
count();
break;
case 6:
dis();
break;
case 7:
search();
break;
case 8:
exit(1);
break;
default:
printf("WRONG CHOICE");
}
}
// for(i=n;i>=0;i--)
// {
// printf("%d",)
// }

}
void createlist(int item)
{

struct node *tmp,*ptr;


tmp=malloc(sizeof(struct node));
tmp->info=item;
tmp->next=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=tmp;
}
}
void addbeg(int item)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->info=item;
tmp->next=start;
start=tmp;
}
void addbet(int item)
{
int pos,i;
struct node*tmp,*ptr;
tmp=malloc(sizeof(struct node));
tmp->info=item;
printf("ENTER THE POSITION AFTER THE ELEMENT NEEDS TO
BE INSERTED:\n");
scanf("%d",&pos);
ptr=start;
for(i=0;i<pos-1;i++)
{
ptr=ptr->next;
}
tmp->next=ptr->next;
ptr->next=tmp;
}
void del()
{
struct node *tmp,*ptr;
int item;
printf("ENTER THE ITEM TO BE DELETED:\n");
scanf("%d",&item);
ptr=start;
if(item==ptr->info)
{
tmp=ptr;
start=ptr->next;
free(tmp);
return;
}
while(ptr->next->next!=NULL)
{
if(ptr->next->info==item)
{
tmp=ptr->next;
ptr->next=tmp->next;
free(tmp);
return;
}
else
{
ptr=ptr->next;
}
}
if(ptr->next->info==item)
{
tmp=ptr->next;
ptr->next=NULL;
free(tmp);
return;
}
}
void dis()
{
int crt=0;
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
printf("%d\n",ptr->info);
ptr=ptr->next;
}
}
void search()
{
int search=0,item,i=0,flag;
struct node *ptr;
ptr=start;
if(ptr==NULL)
{
printf("EMPTY LIST\n");
}
else
{
printf("\nENTER THE ITEM TO SEARCH\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->info==item)
{
printf("ITEM IS FOUNDED IN %d
LOCATION.",i+1);
flag=0;
}
else
{
printf("ITEM IS NOT FOUND.\n");
flag=1;
}
i++;
ptr=ptr->next;
}
}
}
void count()
{
int crt=0;
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
crt++;
ptr=ptr->next;
}
printf("THE NUMBER OF NODES IN THE LINKEDLIST: %d \
n",crt);
}

8. Write a program to create a doubly linked list and perform the following
operations.
i. Insert at the beginning
ii. Insert at the end
iii. Insert at any position
iv. Delete at the beginning
v. Delete at the end.
vi. Delete at any position
vii. Display
viii. Display elements in reversed order.

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
void dis();
void addbeg(int);
void addbet(int);
void createlist(int);
void del();
void reverselist();
void search();
struct node
{
struct node *prev;
int info;
struct node *next;
}*start;
int main()
{
int ch,n,i,item,sear=0;
while(1)
{
printf("\n~~~~~~~~~~~~~~~~~~ENTER THE
CHOICE~~~~~~~~~~~~~~~~~~~~\n");
printf("1) CREATE\n");
printf("2) ADD BEGNNING\n");
printf("3) ADD BETWEEN\n");
printf("4) DELETE\n");
printf("5) DISPLAY\n");
printf("6) DISPLAY REVERSE\n");
printf("7) SEARCH\n");
printf("8) QUIT\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~\n");
printf("ENTER THE OPERATION TO DO ON LINKED
LISTS:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("ENTER THE NUMBER OF NODES IN THE LIST:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("ENTER THE ITEM:");
scanf("%d",&item);
createlist(item);
}
break;
case 2:
printf("ENTER THE ITEM:");
scanf("%d",&item);
addbeg(item);
break;
case 3:
printf("ENTER THE ITEM:");
scanf("%d",&item);
addbet(item);
break;
case 4:
del();
break;
case 5:
dis();
break;
case 6:
reverselist();
break;
case 7:
search();
break;
case 8:
exit(1);
break;
default:
printf("Wrong Choice");
}
}
}
void dis()
{
struct node *ptr;
int n=1;
ptr=start;
while(ptr!=NULL)
{
printf(" ITEM IN THE %d NODE: %d\n",n,ptr->info);
n++;
ptr=ptr->next;

}
}
void addbeg(int item)
{
struct node *tmp;
tmp=malloc(sizeof(struct node));
tmp->info=item;
tmp->prev=NULL;
tmp->next=start;
start->prev=tmp;
start=tmp;
}
void createlist(int item)
{
struct node *tmp,*ptr;
tmp=malloc(sizeof(struct node));
tmp->info=item;
tmp->next=NULL;
if(start==NULL)
{
tmp->prev=NULL;
start=tmp;
}
else
{
ptr=start;
while(ptr ->next!=NULL)
{
ptr=ptr->next;
}
tmp->prev=ptr;
ptr->next=tmp;
}
}
void addbet(int item)
{
int pos,i;
struct node*tmp,*ptr;
tmp=malloc(sizeof(struct node));
tmp->info=item;
printf("ENTER THE POSITION:");
scanf("%d",&pos);
ptr=start;
for(i=0;i<pos-1;i++)
{
ptr=ptr->next;
}
tmp->next=ptr->next;
ptr->next->prev=tmp;
ptr->next=tmp;
tmp->prev=ptr;
}
void del()
{
int item;
struct node *tmp,*ptr;
printf("ENTER THE ITEM: ");
scanf("%d",&item);
if(start->info==item)
{
tmp=start;
start=tmp->next;
start->prev=NULL;
free(tmp);
return;
}
ptr=start;
while(ptr->next->next!=NULL)
{
if(ptr->next->info==item)
{
tmp=ptr->next;
ptr->next=tmp->next;
tmp->next->prev=ptr;
free(tmp);
return;
}
else
{
ptr=ptr->next;
}
}
if(ptr->next->info==item)
{
tmp=ptr->next;
ptr->next=NULL;
free(tmp);
return;
}
}
void reverselist()
{
struct node *prev,*ptr;
if(start!=NULL)
{
prev=start;
ptr=start->next;
start=start->next;
prev->next=NULL;
while(start!=NULL)
{
prev=start;
ptr=start->next;
prev=ptr;
ptr=start;
}
start=prev;
}
if(start!=NULL)
{
printf("AFTER REVERSING THE LIST IS:\n");
while(start->next!=NULL)
{
printf("%d\t",ptr->info);
start=start->next;
}
}
else
{
printf("LIST IS EMPTY");
}
}
void search()
{
int search=0,item,i=0,flag;
struct node *ptr;
ptr=start;
if(ptr==NULL)
{
printf("EMPTY LIST\n");
}
else
{
printf("\nENTER THE ITEM TO SEARCH\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->info==item)
{
printf("ITEM IS FOUNDED IN %d
LOCATION.",i+1);
flag=0;
}
else
{
printf("ITEM IS NOT FOUND.\n");
flag=1;
}
i++;
ptr=ptr->next;
}
}
}

You might also like