0% found this document useful (0 votes)
35 views22 pages

DSA For B.Tech

The document discusses data structures and algorithms. It includes code for implementing various operations on stacks, queues, linked lists using arrays in C language. Specifically, it provides code to: 1) Perform push and pop operations on a stack using an array 2) Implement queue operations like insert, delete, display using an array 3) Implement a circular queue using an array 4) Create a single linked list and perform operations like display, insert at beginning/end/random position, delete from beginning/end/random position.

Uploaded by

Md Imrajul Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views22 pages

DSA For B.Tech

The document discusses data structures and algorithms. It includes code for implementing various operations on stacks, queues, linked lists using arrays in C language. Specifically, it provides code to: 1) Perform push and pop operations on a stack using an array 2) Implement queue operations like insert, delete, display using an array 3) Implement a circular queue using an array 4) Create a single linked list and perform operations like display, insert at beginning/end/random position, delete from beginning/end/random position.

Uploaded by

Md Imrajul Alam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Data Structure & Algorithm

B.Tech(CSE) 2020-24
1). WAP in C to perform array implementation of STACK
program function.
#include<stdio.h>
#include<conio.h>
#define MAX 5
void push();
void pop();
void display();
int stack[MAX];
int top=-1;
void main()
{
int ch;
clrscr();
printf("**----------------STACK---------------**");
printf("\n----------------------------------------\n");
while(1)
{
printf("\n#-----------------MENU-------------------#\n");
printf("\n1).PUSH\n2).POP\n3).DISPLAY\n4).EXIT\n");
printf("\nENTER YOUR CHOICE: ");
scanf("%d",&ch);
switch(ch)
{

1
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(4);
default:
printf("Oops!!!\nWRONG CHOICE!!!\n");
}
}
}
void push()
{
int x;
if(top==(MAX-1))
printf("\nOVERFLOW!!!\nSTACK IS FULL!!!\n");
else
{
printf("\nEnter an element: ");
scanf("%d",&x);
top=top+1;
stack[top]=x;

2
printf("%d is inserted",x);
}
}
void pop()
{
int x;
if(top==-1)
printf("\nUNDERFLOW!!!\nSTACK IS EMPTY!!!\n");
else
{
x=stack[top];
top=top-1;
printf("%d is deleted",x);
}
}
void display()
{
int k;
if(top==-1)
printf("\nSTACK IS EMPTY!!!\n");
else
{
printf("\n----------ELEMENTS OF THE STACK----------\n");
for(k=top;k>=0;k--)
printf("%d\n",stack[k]);
}
}

3
2). WAP in C to implement QUEUE operation using array.
#include<stdio.h>
#include<conio.h>
#define MAX 10
int queue[MAX],front=-1,rear=-1;
void insert();
void delete();
void display();
void main()
{
int ch;
clrscr();
printf("**---------------------QUEUE--------------------**");
printf("\n--------------------------------------------------\n");
while(1)
{
printf("\n\n#---------------MENU---------------#");
printf("\n\n1).INSERT\n2).DELETE\n3).DISPLAY\n4).EXIT");
printf("\n\nENTER YOUR CHOICE: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delete();

4
break;
case 3:
display();
break;
case 4:
exit(4);
default:
printf("Oops!!!\nINVALID INPUT!!!");
}
}
}
void insert()
{
int x;
if(rear==(MAX-1))
printf("QUEUE IS FULL!");
else
{
printf("\n\nENTER AN ELEMENT: ");
scanf("%d",&x);
if(rear==-1)
{
front=0;
rear=0;
queue[rear]=x;
}
else

5
{
rear=rear+1;
queue[rear]=x;
}
printf("\n\n%d IS INSERTED IN THE QUEUE",x);
}
}
void delete()
{
int x;
if(rear==-1)
printf("QUEUE IS EMPTY!");
else
{
x=queue[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=front+1;
}
printf("\n\n%d IS DELETED FROM THE QUEUE",x);
}
}

6
void display()
{
int k;
if(front==-1)
printf("QUEUE IS EMPTY!");
else
{
printf("\n\n*ELEMENTS OF THE QUEUE*");
printf("\n\nPrinting Values...\n\n");
for(k=front;k<=rear;k++)
printf("%d\t",queue[k]);
}
}

3). WAP in C to implement CIRCULAR QUEUE using array.


#include<stdio.h>
#include<conio.h>
#define MAX 5
int queue[MAX],front=-1,rear=-1;
void insert();
void delete();
void display();
void main()
{
int choice;
clrscr();
printf("**---------------CIRCULAR QUEUE---------------**");
printf("\n----------------------------------------------\n");

7
while(1)
{
printf("\n\n#-----------------MENU--------------------#\n");
printf("\n1).INSERT\n2).DELETE\n3).DISPLAY\n4).EXIT\n");
printf("\nENTER YOUR CHOICE: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(4);
default:
printf("INVALID INPUT!!!");
}
}
}
void insert()
{
int x;

8
if(rear==(MAX-1))
printf("QUEUE IS FULL!!!");
else
{
printf("\nENTER AN ELEMENT: ");
scanf("%d",&x);
if(rear==-1)
{
front=0;
rear=0;
queue[rear]=x;
}
else
{
rear=(rear+1)%MAX;
queue[rear]=x;
}
printf("\n %d IS INSERTED IN THE CIRCULAR QUEUE",x);
}
}
void delete()
{
int x;
if(front==-1)
printf("QUEUE IS EMPTY!!!");
else
{

9
x=queue[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=(front+1)%MAX;
}
printf("\n %d IS DELETED FROM THE CIRCULAR QUEUE",x);
}
}
void display()
{
int k;
if(front==-1)
{
printf("QUEUE IS EMPTY!!!");
}
else
{
printf("\nQUEUE ELEMENTS ARE: \n");
printf("\nPrinting Values...\n");
if(front<=rear)
{
for(k=front;k<=rear;k++)

10
printf("%d\t",queue[k]);
}
else
{
for(k=front;k<=(MAX+rear);k++)
printf("%d\t",queue[k%MAX]);
}
}
}

4). WAP in C to create a SINGLE LINKED LIST & also perform


the following operations-
1). Display
2). Insert At the Beginning
3). Insert At the End
4). Insert At Any Given Position
5). Delete From the Beginning
6). Delete From the End
7). Delete A Specific Node
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create();
void insert_beginning();
void insert_end();
void insert_random();

11
void remove_beginning();
void remove_end();
void remove_random();
void display();
typedef struct linked_list
{
int data;
struct linked_list *next;
} node;
node *start;
void main()
{
int choice;
clrscr();
printf("\n**---------------SINGLE LINKED LIST--------------**");
printf("\n===================================================\n");
while(1)
{
printf("\n#---------------MAIN MENU------------------#\n");
printf("\n===================================================\n");
printf("\n1).CREATE\n2).DISPLAY\n3).EXIT\n");
printf("\nINSERT AT-\n\t\t4).BEGINNING\n\t\t5).END\n\t\t6).AS PER USER'S
CHOICE\n");
printf("\nDELETE FROM-\n\t\t7).BEGINNING\n\t\t8).END\n\t\t9).AS PER USER'S
CHOICE\n");
printf("\nENTER YOUR CHOICE: ");
scanf("%d",&choice);
switch(choice)

12
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
exit(0);
case 4:
insert_beginning();
break;
case 5:
insert_end();
break;
case 6:
insert_random();
break;
case 7:
remove_beginning();
break;
case 8:
remove_end();
break;
case 9:
remove_random();
break;

13
default:
printf("INVALID CHOICE!!!");
}
}
}
void create()
{
node *present,*temp;
char ch;
if(start==NULL)
{
present=(node *)malloc(sizeof (node));
printf("\nENTER DATA FOR THE FIRST NODE: ");
scanf("%d",&present->data);
printf("\n**LINKED LIST IS CREATED**\n");
start=present;
do
{
temp=(node *)malloc(sizeof (node));
printf("\nENTER DATA FOR THE NEXT NODE: ");
scanf("%d",&temp->data);
present->next=temp;
present=temp;
printf("PRESS 'Y' IF YOU WANT TO ADD MORE DATA OTHER WISE PRESS 'N'");
ch=getch();
}
while(ch=='y');

14
present->next=NULL;
}
else
{
printf("\n**LINKED LIST IS ALREADY CREATED**");
}
}
void display()
{
node *present;
printf("LINKED LIST IS: \n");
printf("Printing Values...\n");
present=start;
while(present!=NULL)
{
printf("%d\t",present->data);
present=present->next;
}
}
void insert_beginning()
{
node *temp;
temp=(node *)malloc(sizeof (node));
printf("ENTER THE DATA TO BE INSERTED AT THE BEGINNING: ");
scanf("%d",&temp->data);
temp->next=start;
start=temp;

15
printf("\n**ELEMENT IS INSERTED**");
}
void insert_end()
{
node *present,*temp;
temp=(node *)malloc(sizeof (node));
printf("ENTER THE DATA TO BE INSERTED AT THE END: ");
scanf("%d",&temp->data);
present=start;
while(present->next!=NULL)
{
present=present->next;
}
present->next=temp;
temp->next=NULL;
printf("\n**ELEMENT IS INSERTED**");
}
void insert_random()
{
int x;
node *present,*temp;
printf("ENTER THE NODE AFTER WHICH YOU WANT TO ADD THE NEW NODE: ");
scanf("%d",&x);
temp=(node *)malloc(sizeof (node));
printf("ENTER THE NEW NODE: ");
scanf("%d",&temp->data);
present=start;

16
while(present->data!=x)
{
present=present->next;
}
temp->next=present->next;
present->next=temp;
printf("\n**ELEMENT IS INSERTED**");
}
void remove_beginning()
{
node *temp;
if(start==NULL)
{
printf("LINKED LIST IS EMPTY!!!");
return;
}
temp=start;
start=temp->next;
free(temp);
printf("\n**ELEMENT IS DELETED FROM THE BEGINNING OF THE LINKED LIST**");
}
void remove_end()
{
node *temp,*present;
if(start==NULL)
{
printf("LINKED LIST IS EMPTY!!!");

17
return;
}
present=start;
while(present->next!=NULL)
{
temp=present;
present=present->next;
}
temp->next=NULL;
free(present);
printf("\n**ELEMENT IS DELETED FROM THE END OF THE LINKED LIST**");
}
void remove_random()
{
node *temp,*present;
int x;
if(start==NULL)
{
printf("LINKED LIST IS EMPTY!!!");
return;
}
present=start;
printf("ENTER THE ELEMENT TO BE DELETED: ");
scanf("%d",&x);
while(present->data!=x)
{
temp=present;

18
present=present->next;
}
temp->next=present->next;
free(present);
printf("\n**ELEMENT IS DELETED FROM THE LINKED LIST**");
}

5). WAP in C to implement STACK USING LINKED LIST.


#include<stdio.h>
#include<conio.h>
void push();
void pop();
void display();
typedef struct linked_list
{
int data;
struct linked_list *next;
}node;
node *top;
void main()
{
int ch;
clrscr();
printf("**----------STACK USING LINKED LIST----------**\n");
printf("===============================================\n");
while(1)
{
printf("\n\n#----------MAIN MENU----------#\n");

19
printf("\n1).PUSH\n2).POP\n3).DISPLAY\n4).EXIT\n");
printf("\nENTER YOUR CHOICE: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("INVALID INPUT!!!");
}
}
}
void display()
{
node *present;
if(top!=NULL)
{
printf("STACK ELEMENTS ARE: \n");

20
printf("\nPrinting Values...\n");
present=top;
while(present!=NULL)
{
printf("%d\n",present->data);
present=present->next;
}
}
else
{
printf("\nEMPTY STACK!!!");
}
}
void push()
{
node *temp;
temp=(node *)malloc(sizeof(node));
if(top==NULL)
{
printf("ENTER AN ELEMENT: ");
scanf("%d",&temp->data);
printf("\n**ELEMENT IS PUSHED INTO THE STACK**");
temp->next=NULL;
top=temp;
}
else
{

21
if(temp==NULL)
{
printf("STACK OVERFLOW!!!");
return;
}
printf("ENTER AN ELEMENT: ");
scanf("%d",&temp->data);
temp->next=top;
top=temp;
printf("\n**ELEMENT IS PUSHED INTO THE STACK**");
}
}
void pop()
{
node *temp;
if(top==NULL)
{
printf("\nSTACK UNDERFLOW!!!");
return;
}
temp=top;
top=temp->next;
free(temp);
printf("\n**ELEMENT IS POPED FROM THE STACK**");
}

22

You might also like