0% found this document useful (0 votes)
10 views17 pages

DS Experiments

The document outlines the implementation of various data structures in C, including stacks, queues, lists, and linked lists using arrays and dynamic memory allocation. Each section provides an aim, algorithm, program code, and output verification for the respective data structure. The implementations demonstrate basic operations such as insertion, deletion, and display of elements.

Uploaded by

sec23it049
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)
10 views17 pages

DS Experiments

The document outlines the implementation of various data structures in C, including stacks, queues, lists, and linked lists using arrays and dynamic memory allocation. Each section provides an aim, algorithm, program code, and output verification for the respective data structure. The implementations demonstrate basic operations such as insertion, deletion, and display of elements.

Uploaded by

sec23it049
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/ 17

EX.NO.

1 ARRAY IMPLEMENTATION OF STACK

A stack is a container of objects that are inserted and removed according to the last-in
first-out (LIFO) principle. In the pushdown stacks only two operations are allowed: push the
item into the stack, and pop the item out of the stack.

AIM:
To write a ‘C’ program for implementing stacks using array.

ALGORITHM

Step1:Define a array which stores stack elements.


Step 2: Increment Top pointer and then PUSH data into the stack at the position pointed by
the Top.
Step 3: POP the data out the stack at the position pointed by the Top.
Step 5. The stack represented by array is traversed to display its content.

PROGRAM

#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");

8
printf("\n\t ");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
} } }
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}

9
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}}

OUTPUT
Enter the size of STACK[MAX=100]:10

STACK OPERATIONS USING ARRAY

1. PUSH
2. POP
3. DISPLAY
4. EXIT

10
Enter the Choice:1
Enter a value to be pushed:12
Enter the Choice:1
Enter a value to be pushed:24
Enter the Choice:1
Enter a value to be pushed:98
Enter the Choice:3

The elements in STACK


98
24
12
Press Next Choice
Enter the Choice:2

The popped elements is 98


Enter the Choice:3

The elements in STACK


24
12
Press Next Choice
Enter the Choice:4

EXIT POINT

RESULT:

Thus the program to implement stack using array has been implemented and the output is
verified.

11
Ex. No. 2 ARRAY IMPLEMENTATION OF QUEUE

Queue is a linear structure which follows a particular order in which the operations are
performed. The order is First in First out (FIFO). A good example of queue is any queue of
consumers for a resource where the consumer that came first is served first. The difference
between stacks and queues is in removing. In a stack we remove the item the most recently
added; in a queue, we remove the item the least recently added.

AIM:
To write a ‘C’ program for implementing Queue using array.

ALGORITHM:

Step 1 : To insert Check if the queue is full or not.


Step 2 : If the queue is full, then print overflow error and exit the program.
Step 3 : If the queue is not full, then increment the tail and add the element.
Step 4 : Check if the queue is empty or not.
Step 5 : If the queue is empty, then print underflow error and exit the program. Step 6 :If
the queue is not empty, then print the element at the head and incrementthe he

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;

12
//clrscr();
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
} break;
case 2:
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\n Queue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{ for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);

13
default:
printf("Wrong Choice: please see the options");
}
}
}
getch();
}
OUTPUT:
Queue using Array
1.Insertion
2.Deletion
3.Display
4.Exit
Enter the Choice:1
Enter no 1:10
Enter the Choice:1
Enter no 2:54
Enter the Choice:1
Enter no 3:98
Enter the Choice:1
Enter no 4:234
Enter the Choice:3
Queue Elements are:
10
54
98
234
Enter the Choice:2
Deleted Element is 10
Enter the Choice:3
Queue Elements are:
54
98
234

Enter the Choice:4

RESULT:
Thus the ‘C’ program to implement Queue using array has been executed and the output is
verified.

14
EX.NO.3 ARRAY IMPLEMENTATION OF LIST ADT

AIM:
To write a ‘C’ program for implementing List ADT using array.

ALGORITHM:
Step 1: Define an array to store the elements in the List
Step 2: Perform Insertion, Deletion operation in the List
Step 3: Search the obtained element in the List
Step 4: Traverse the list to display the elements.

PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20], n, p, e, f, i, pos;
void main()
{
//clrscr();
int ch;
char g='y';
do
{
printf("\n main Menu");
printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit \n");
printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();
break;
case 2:
deletion();
break;

15
case 3:
search();
break;
case 4:
insert();
break;
case 5:
display();
break;
case 6:
exit();
break;
default:
printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y');
getch();
}
void create()
{
printf("\n Enter the number of nodes");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}}
void deletion()
{
printf("\n Enter the position u want to delete::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");
}
else
{
for(i=pos+1;i<n;i++)

16
{
b[i-1]=b[i];
}
n--;
}
printf("\n The Elements after deletion");
for(i=0;i<n;i++)
{
printf("\t%d", b[i]);
}}
void search()
{
printf("\n Enter the Element to be searched:");
scanf("%d", &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
printf("Value is in the %d Position", i);
}
else
{
printf("Value %d is not in the list::", e);
continue;
}}}
void insert()
{
printf("\n Enter the position u need to insert::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n invalid Location::");
}
else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);

17
b[pos]=p;
n++;
}
printf("\n The list after insertion::\n");
display();
}
void display()
{
printf("\n The Elements of The list ADT are:");
for(i=0;i<n;i++)
{
printf("\n\n%d", b[i]);
}}

OUTPUT:

RESULT:
Thus the C program to implement List ADT using array has been executed and the output is
verified.

18
EX.NO.4 LINKED LIST IMPLEMENTATION OF LIST
(SINGLY LINKED LIST)

AIM:
To write a ‘C’ program to create a singly linked list implementation.

ALGORITHM:
Step 1: Start the program.
Step 2: Get the choice from the user.
Step3: If the choice is to add records, get the data from the user and add them to the
list.
Step 4: If the choice is to delete records, get the data to be deleted and delete it from
the list.
Step 5: If the choice is to display number of records, count the items in the list and
display.
Step 6: If the choice is to search for an item, get the item to be searched and respond
yes if the item is found, otherwise no.
Step 7: Terminate the program

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define NULL 0
struct info
{
int data;
struct info *next;
};
struct info *head,*temp,*disp;
void additem();
void delitem();
void display();
int size();
void search();
void main()
{
int choice;
clrscr();

19
while(1)
{
printf("\n1.Add records");
printf("\n2.Delete records");
printf("\n3.Display records");
printf("\n4.Count no. of items in the list");
printf("\n5.Searching an item in the list");
printf("\n6.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
fflush(stdin);
switch(choice)
{
case 1:
additem();
break;
case 2:
delitem();
break;
case 3:
display();
break;
case 4:
printf("\nThe size of the list is %d",size());
break;
case 5:
search();
break;
case 6:
exit(0);
}}}
void additem()
{
struct info *add;
char proceed='y';
while(toupper(proceed)=='Y')
{
add=(struct info*)malloc(sizeof(struct info));
printf("Enter data:");
scanf("%d",&add->data);
fflush(stdin);

20
if(head==NULL)
{
head=add;
add->next=NULL;
temp=add;
}
else
{
temp->next=add;
add->next=NULL;
temp=add;
}
printf("\nWant to proceed y/n");
proceed=getchar();
fflush(stdin);
}}
void delitem()
{
struct info *curr,*prev;
int tdata;
if(head==NULL)
{
printf("\nNo records to delete");
return;
}
printf("\nEnter the data to delete");
scanf("%d",&tdata);
fflush(stdin);
prev=curr=head;
while((curr!=NULL)&&(curr->data!=tdata))
{
prev=curr;
curr=curr->next;
}
if(curr==NULL)
{
printf("\nData not found");
return;
}
if(curr==head)
head=head->next;

21
else
{
/*for inbetween element deletion*/
prev->next=curr->next;
/*for the last element deletion*/
if(curr->next==NULL)
temp=prev;
}
free(curr);
}
void display()
{
if(head==NULL)
{
printf("\nNo data to display");
return;
}
for(disp=head;disp!=NULL;disp=disp->next)
{
printf("Data->%d",disp->data);
}}
int size()
{
int count=0;
if(head==NULL)
return count;
for(disp=head;disp!=NULL;disp=disp->next)
count++;
return count;
}
void search()
{
int titem,found=0;
if(head==NULL)
{
printf("\nNo data in the list");
return;
}
printf("\Enter the no. to search:");
scanf("%d",&titem);
for(disp=head;disp!=NULL&&found==0;disp=disp->next)

22
{
if(disp->data==titem)
found=1;
}
if(found==0)
printf("\nSearch no. is not present in the list");
else
printf("\nSearch no. is present in the list");
return;
}

OUTPUT:

1.Add records
2.Delete records
3.Display records
4.Count no. of items in the list
5.Searching an item in the list
6.Exit
Enter your choice:1
Enter data:12
Want to proceed y/ny
Enter data:13
Want to proceed y/ny
Enter data:41
Want to proceed y/nn

1.Add records
2.Delete records
3.Display records
4.Count no. of items in the list
5.Searching an item in the list
6.Exit
Enter your choice:3
Data->12Data->13Data->41
1.Add records
2.Delete records
3.Display records
4.Count no. of items in the list
5.Searching an item in the list
6.Exit

23
Enter your choice:4
The size of the list is 3
1.Add records
2.Delete records
3.Display records
4.Count no. of items in the list
5.Searching an item in the list
6.Exit
Enter your choice:2
Enter the data to delete13
1.Add records
2.Delete records
3.Display records
4.Count no. of items in the list
5.Searching an item in the list
6.Exit
Enter your choice:3
Data->12Data->41
1.Add records
2.Delete records
3.Display records
4.Count no. of items in the list
5.Searching an item in the list
6.Exit
Enter your choice:5
Enter the no. to search:13
Search no. is not present in the list
1.Add records
2.Delete records
3.Display records
4.Count no. of items in the list
5.Searching an item in the list
6.Exit
Enter your choice:6
RESULT:
The given program is implemented, executed, tested and verified successfully.

24

You might also like