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

Program - 1 Write A Program To Perform Insertion & Deletion in An Array

The document contains 4 programs written in C to implement different data structures using arrays: 1. An array program that allows insertion and deletion of elements from an array. 2. A stack implementation using an array that allows push and pop operations. 3. A queue implementation using an array that allows enqueue and dequeue operations. 4. A singly linked list implementation that allows insertion, deletion, searching and traversal of nodes. Each program contains the necessary functions and main function to perform the operations and display outputs.

Uploaded by

Shailesh Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Program - 1 Write A Program To Perform Insertion & Deletion in An Array

The document contains 4 programs written in C to implement different data structures using arrays: 1. An array program that allows insertion and deletion of elements from an array. 2. A stack implementation using an array that allows push and pop operations. 3. A queue implementation using an array that allows enqueue and dequeue operations. 4. A singly linked list implementation that allows insertion, deletion, searching and traversal of nodes. Each program contains the necessary functions and main function to perform the operations and display outputs.

Uploaded by

Shailesh Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Program 1

Write a program to perform Insertion & Deletion in an Array


#include <stdlib.h>
#include <conio.h>
#include <stdio.h>

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

printf("\n\nEnter the position of the element to be deleted: ");


scanf("%d", &pos);
pos--;
for(i = pos; i < size - 1; i++)
arr[i] = arr[i+1];
size--;
break;
case 3:
if(size > 20)
continue;
printf("\n\nEnter element to be added: ");
scanf("%d", &elem);
printf("Enter element position: ");
scanf("%d", &pos);
pos -= 1;
if(pos < size)
{
for(i = size; i > pos; i--)
arr[i] = arr[i-1];
size++;
arr[pos] = elem;
}
else continue;
break;
case 4:
printf("\n\nArray elements are: ");
for(i = 0; i < size; i++)
printf("%d, ", arr[i]);
getch();
break;
case 5:
exit(0);

SHAILESH GUPTA I-789


02596403113

break;
default:
continue;
}
}while(choice != 5);
getch();
return;
}

Output:

SHAILESH GUPTA I-789


02596403113

Program 2
Write a program to implement Stack using Array
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define size 10

int a[size],i,top = -1;

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;
}

SHAILESH GUPTA I-789


02596403113

Output:-

SHAILESH GUPTA I-789


02596403113

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;
}

}while(choice < 3 && choice > 0);

SHAILESH GUPTA I-789


02596403113

getch();
}

Output:

SHAILESH GUPTA I-789


02596403113

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

printf("1. Create a node");


printf("\n2. Search node in list");
printf("\n3. Delete a node");
printf("\n4. Traverse the list");
printf("\n5. EXIT");
printf("\n\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insertNode();
getch();
break;
case 2:
searchList();
getch();
break;
case 3:
deleteNode();
getch();
break;
case 4:
traverseList();
getch();
break;
SHAILESH GUPTA I-789
02596403113

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

while(ptr -> next != NULL)


ptr = ptr->next;
ptr->next = temp;
return;
}

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

struct node*ptr = head;


int data;
if(head == NULL)
{
printf("\n\nList already empty!!");
return;
}
head = head->next;
data = ptr->data;
free(ptr);
printf("\nDeleted node: %d", data);
return;
}

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:

SHAILESH GUPTA I-789


02596403113

16

You might also like