Program - 1 Write A Program To Perform Insertion & Deletion in An Array
Program - 1 Write A Program To Perform Insertion & Deletion in An Array
void main()
{
clrscr();
int arr[20], choice = 0, size = 0, elem = 0, pos = 0, i = 0;
do
{
clrscr();
printf("\n1. Create Array");
printf("\n2. Delete elements from the array");
printf("\n3. Add elements in the array");
printf("\n4. Display array");
printf("\n5. EXIT");
printf("\n\nEnter your choice please: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n\nEnter the size of the array: ");
scanf("%d", &size);
printf("\nNow enter the elements: ");
for(i = 0; i < size; i++)
scanf("%d", &arr[i]);
break;
case 2:
SHAILESH GUPTA I-789
02596403113
break;
default:
continue;
}
}while(choice != 5);
getch();
return;
}
Output:
Program 2
Write a program to implement Stack using Array
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 10
int push(int x)
{
if(top == size-1)
{
printf("\n\nOverflow");
return top;
}
else
{
++top;
a[top] = x;
printf("\nStack = ");
for(i=0;i<top+1;i++)
printf(" %d\t",a[i]);
}
return top;
}
int pop()
{
SHAILESH GUPTA I-789
02596403113
if(top == 0)
printf("\n\nStack underflow");
else
{
--top;
printf("\nStack = ");
for(i=0;i<=top;i++)
printf("%d\t",a[i]);
}
return top;
}
void display()
{
if(top == -1)
{
printf("\n\nStack not yet created!");
return;
}
printf("Stack is: ");
for(int i = 0; i <= top;i++)
printf("%d, ", a[i]);
return;
}
int main()
{
int item,n,ch;
do
{
clrscr();
printf("1. Push\n2. Display stack\n3. Pop\n4. EXIT \n Enter
choice:");
SHAILESH GUPTA I-789
02596403113
scanf("%d",&n);
switch(n)
{
case 1 :
printf("\n\nEnter an element to push:\t");
scanf("%d",&item);
push(item);
break;
case 2:
display();
getch();
case 3 :
pop();
break;
case 4:
exit(0);
default :
printf("\n\nIvalid choice");
getch();
}
}while(ch < 1);
return 0;
}
Output:-
Program 3
Write a program to implement Linear Queue
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
clrscr();
int front = -1, rear = -1, arr[20], choice = 0, val = 0;
do
{
clrscr();
printf("1. Insert element in array");
printf("\n2. Delete element");
printf("\n3. Display array");
printf("\n4. EXIT");
printf("\nEnter choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n\nEnter value: ");
scanf("%d", &val);
if(rear < 20)
{
if(rear == -1)
front = rear = 0;;
arr[rear] = val;
rear++;
SHAILESH GUPTA I-789
02596403113
}
else
printf("\n\nQueue overflow!";
getch();
break;
case 2:
if(front > rear)
{
printf("\n\nQueue is empty!");
getch();
}
else
{
printf("\n\nElement deleted: %d", arr[front]);
arr[front] = -1;
front++;
getch();
}
break;
case 3:
printf("\n\nQueue: ");
for(int i = front; i <= rear; i++)
printf("%d, ", arr[i]);
getch();
break;
case 4:
exit(0);
default:
continue;
}
getch();
}
Output:
10
Program 4
Write a program to implement Singly Linked List
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* next;
}*head;
void insertNode();
void deleteNode();
void searchList();
void traverseList();
void main()
{
head = NULL;
int choice;
while(1)
{
clrscr();
SHAILESH GUPTA I-789
02596403113
11
12
case 5:
exit(0);
break;
default:
continue;
}
}
}
void insertNode()
{
int data;
printf("\n\nEnter data: ");
scanf("%d", &data);
struct node*temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = (struct node*)malloc(sizeof(struct node));
head->data = data;
head->next = NULL;
return;
}
struct node*ptr = head;
SHAILESH GUPTA I-789
02596403113
13
void traverseList()
{
struct node* ptr = head;
if(head == NULL)
{
printf("\n\nList already empty!!");
return;
}
printf("\nList: ");
while(ptr != NULL)
{
printf("%d, ", ptr->data);
ptr = ptr->next;
}
return;
}
void deleteNode()
{
SHAILESH GUPTA I-789
02596403113
14
void searchList()
{
int data, found = 0, pos = 0;
printf("\n\nEnter item to search for: ");
scanf("%d", &data);
struct node* ptr = head;
while(ptr != NULL){
if(ptr->data == data)
found = 1;
ptr = ptr->next;
pos++;
SHAILESH GUPTA I-789
02596403113
15
}
if(found == 1)
printf("\nItem found at position: %d", pos);
else
printf("\nItem not found!");
return;
}
Output:
16