0% found this document useful (0 votes)
96 views

Lab Program Ds 3rd Sem

The document contains C code for implementing various operations on data structures like stacks, arrays, queues and linked lists. It includes functions for push, pop, peep etc on stacks, insert, delete, search, sort etc on arrays, insert, delete, display on queues and create, display, count, search nodes of linked lists. The code provides menus to choose different operations and call corresponding functions to perform the chosen operation.

Uploaded by

sankrajegowda
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views

Lab Program Ds 3rd Sem

The document contains C code for implementing various operations on data structures like stacks, arrays, queues and linked lists. It includes functions for push, pop, peep etc on stacks, insert, delete, search, sort etc on arrays, insert, delete, display on queues and create, display, count, search nodes of linked lists. The code provides menus to choose different operations and call corresponding functions to perform the chosen operation.

Uploaded by

sankrajegowda
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

#include<stdio.

h>
#include<stdlib.h>
#define max_size 5
int stack[max_size],top=-1;
/*------ Function Prototype------------*/
void push();
void pop();
void peep();
void display();
/*-------------------------------------*/
int main()
{
int choice;
do{
//printf("\n");
printf("\n\n--------STACK OPERATIONS-----------\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Peep\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peep();
break;
case 4:
display();
break;
case 5:
exit(0);

break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=5);
return 0;
}
void push() //Inserting element in to the stack
{
int item;
if(top==(max_size-1))
{
printf("\nStack Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
void pop()

//deleting an element from the stack

{
int item;
if(top==-1)
{
printf("Stack Underflow:");
}
else
{
item=stack[top];
top=top-1;
printf("\nThe poped element: %d\t",item);
}
}
void peep()
{
if(top==-1)
{

printf("\nStack is empty:");
}
else
{
printf("The topmost element of the stack is %d",stack[top]);
}
}
void display()
{
int i;
if(top==-1)
{
printf("\nStack is Empty:");
}
else
{
printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
}

#include<stdio.h>
#include<stdlib.h>
int a[20],b[20],c[40];
int m,n,p,val,i,j,key,pos,temp;
/*Function Prototype*/
void create();
void display();
void insert();
void del();
void search();
void merge();
void sort();
int main()
{
int choice;
do{
printf("\n\n--------Menu-----------\n");
printf("1.Create\n");
printf("2.Display\n");
printf("3.Insert\n");
printf("4.Delete\n");
printf("5.Search\n");
printf("6.Sort\n");
printf("7.Merge\n");
printf("8.Exit\n");
printf("-----------------------");
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:

create();
break;

case 2:
display();
break;
case 3:
insert();
break;

case 4:
del();

break;
case 5:
search();
break;
case 6:
sort();
break;
case 7:
merge();
break;
case 8:
exit(0);
break;
default:
printf("\nInvalid choice:\n");
break;
}
}while(choice!=8);
return 0;
}
void create() //creating an array
{
printf("\nEnter the size of the array elements:\t");
scanf("%d",&n);
printf("\nEnter the elements for the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}//end of create()
void display() //displaying an array elements
{
int i;
printf("\nThe array elements are:\n");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
}//end of display()
void insert() //inserting an element in to an array
{
printf("\nEnter the position for the new element:\t");

scanf("%d",&pos);
printf("\nEnter the element to be inserted :\t");
scanf("%d",&val);
for(i=n-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
}//end of insert()

void del()

//deleting an array element

{
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d",&pos);
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("\nThe deleted element is =%d",val);
}//end of delete()
void search() //searching an array element
{
printf("\nEnter the element to be searched:\t");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("\nThe element is present at position %d",i);
break;
}
}
if(i==n)
{
printf("\nThe search is unsuccessful");
}
}//end of serach()

void sort()

//sorting the array elements

{
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)

if(a[j]>a[j+1])

{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nAfter sorting the array elements are:\n");
display();
}//end of sort
void merge()

//merging two arrays

{
printf("\nEnter the size of the second array:\t");
scanf("%d",&m);
printf("\nEnter the elements for the second array:\n");
for(i=0;i<m;i++)
{
scanf("%d",&b[i]);
}
for(i=0,j=0;i<n;i++,j++)
{
c[j]=a[i];
}
for(i=0;i<m;i++,j++)
{
c[j]=b[i];
}
p=n+m;
printf("\nArray elements after merging:\n");
for(i=0;i<p;i++)
{
printf("%d\t",c[i]);
}
}//end of merge()

#include<stdio.h>
#include<conio.h>
#define MAX 3
int a[MAX];
int front=-1;
int rear=-1;
void insert();
void del();
void disp();
void main()
{
int ch;
clrscr();
do
{
printf(\nMenu \n);
printf(\n1.INSERT);
printf(\n2.del);
printf(\n3.disp);
printf(\n4.exit);
printf(\nEnter ur choice);
scanf(%d,&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
void insert()
{
char n;
if((front==0 && rear==MAX-1)||(front==rear+1 && front>0))
{
printf(Q is Full);
}
else
{
if(front==-1)
{
front=0;
}
else
{
if(rear==MAX-1 && front>0)
{
rear=0;
printf(\n\nENTER THE DATA);
// scanf(%c,&n);
n=getchar();
n=getchar();
a[rear]=n;
}
else
{
rear++;
printf(\nEnter the Element);
n=getchar();
n=getchar();
//scanf(%c,&n);
a[rear]=n;
}
}
}
}
void del()
{
if(front==-1)
{
printf(\nQ is Empty);
}
else
{
printf(Deleted Element is %c, a[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
if(front==MAX-1)
{
front=0;
}
else
{
front++;
}
}
}
}
void disp()
{
int i;
if(front==-1)
{

printf(\nQ is Empty);
}
else
{
if(front<=rear)
{
for(i=front;i<=rear;i++)
{
printf(%c->,a[i]);
}
}
else
{
for(i=front;i<=MAX-1;i++)
{
printf(%c->,a[i]);
}
for(i=0;i<=rear;i++)
{
printf(%c->,a[i]);
}
}
}
}

#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
}*start;
void create(int);
void disp();
void count();
void ser(int);
void main()
{
int ch,n,i,m,a,pos;
clrscr();
start=NULL;
do
{
printf(\n\nMENU\n\n);
printf(\n1.CREATE\n);
printf(\n2.DISPLAY\n);
printf(\n3.COUNT\n);
printf(\n4.SEARCH \n);
printf(\n5.EXIT\n);
printf(\nENTER UR CHOICE\n);
scanf(%d,&ch);
switch(ch)
{
case 1:
printf(\n\nHOW MANY NODES U WANT TO CREATE\n);
scanf(%d,&n);
for(i=0;i<n;i++)
{
printf(\nENTER THE DATA);
scanf(%d,&m);
create(m);
}
break;
case 3:
count();
break;
case 2:
disp();
break;
case 4:
printf(\nENTER THE ELEMENT FOR SEARCH);
scanf(%d,&m);
ser(m);
break;
case 5:
exit(0);
}
}
while(ch!=7);
getch();
}
void count()
{
struct node *q;
int cnt=0;
q=start;
while(q!=NULL)
{
cnt++;
q=q->link;
}
printf(\n\No Of Nodes Are\t %d,cnt);
}
void create(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=data;
tmp->link=NULL;
if(start==NULL)
{
start=tmp;
}
else

{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}
void disp()
{
struct node *q;
if(start==NULL)
{
printf(\n\nLIST IS EMPTY);
}
else
{
q=start;
while(q!=NULL)
{
printf(%d->,q->data);
q=q->link;
}
printf(NULL);
}
}
void ser(int data)
{
struct node *q,*tmp;
q=start;
while(q!=NULL)
{
if(q->data==data)
{
printf(\nElement Is Found);
break;
}
else
{
q=q->link;
}
}
if(q==NULL)
{
printf(\nElement is Not Found);
}
}

You might also like