A. Add An Element at The Beginning of An Array B. Insert An Element at Given Index of Array
A. Add An Element at The Beginning of An Array B. Insert An Element at Given Index of Array
Write a program to read ‘N’ numbers of elements into an array and also perform
the following operation on an array
#include<stdio.h>
if(pos<0||pos>*n||*n>=capacity)
return;
for(int i=*n;i>pos;i--)
arr[i]=arr[i-1];
arr[pos]=x;
(*n)++;
int main()
int capacity=10;
int arr[capacity]={10,20,30,40,50};
int n=5;
int x=25;
int pos=2;
insertElement(arr,&n,x,pos,capacity);
for(int i=0;i<n;i++)
printf(“%d”,arr[i]);
printf(“\n”);
return 0;
OUTPUT:
#include<stdio.h>
if(pos<0||pos>=*n)
printf(“Error:Invalid position.\n”);
return;
for(int i=pos;i<*n-1;i++)
arr[i]=arr[i+1];
(*n)--;
int main()
int arr[]={10,20,30,40,50};
int n=sizeof(arr)/sizeof(arr[0]);
int pos=2;
deleteElement(arr,&n,pos);
for(int i=0;i<n;i++)
printf(“%d”,arr[i]);
}
printf(“\n”);
return 0;
OUTPUT:
2. Write Program to implement Single Linked List with insertion,
deletion and traversal operations
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
struct node*next;
};
struct node*head;
struct node*newNode;
newNode->data=value;
newNode->next=head;
head=newNode;
struct node*newNode;
newNode=malloc(sizeof(struct node));
newNode->data=value;
newNode->next=NULL;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newNode;
int i;
struct node*newNode;
newNode=malloc(sizeof(struct node));
newNode->data=value;
for(i=0;i<position;i++)
if(temp->next!=NULL)
temp=temp->next;
newNode->next=temp->next;
temp->next=newNode;
}
void print_list()
while(temp->next!=NULL)
printf("%d--->",temp->data);
temp=temp->next;
printf("%d",temp->data);
void main()
insert_beg(12);
insert_beg(22);
insert_end(30);
insert_pos(33,2);
printf("Linked list:");
print_list();
OUTPUT:
Linked list:
List elements are-
22 123033
DELETION:
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
struct node*next;
};
struct node*head;
struct node*newNode;
newNode=malloc(sizeof(struct node));
newNode->data=value;
newNode->next=head;
head=newNode;
void delete_begin()
head=head->next;
}
void delete_end()
struct node*temp=head;
while(temp->next->next!=NULL)
temp=temp->next;
temp->next=NULL;
int i;
struct node*temp=head;
for(i=0;i<position;i++)
if(temp->next!=NULL)
temp=temp->next;
temp->next=temp->next->next;
}
void print_list()
struct node*temp=head;
while(temp->next!=NULL)
printf(“%d->”,temp->data);
temp=temp->next;
printf(“%d”,temp->data);
void main()
int i;
for(i=11;i<20;i++)
insert(i);
printf(“\n\nBefore Deletion:\n”);
print_list();
delete_begin();
print_list();
delete_end();
print_list();
delete_pos(3);
print_list();
OUTPUT:
Before Deletion:
#include<stdio.h>
#include<stdlib.h>
struct Node
int data;
struct Node*next;
struct Node*prev;
};
struct Node*head=NULL;
newNode->data=value;
if(head==NULL)
newNode->next=newNode;
newNode->prev=newNode;
head=newNode;
else
{
struct Node*tail=head->prev;
newNode->next=head;
newNode->prev=tail;
tail->next=newNode;
head->prev=newNode;
head=newNode;
if(head==NULL)
printf("List is empty.\n");
return;
struct Node*temp=head;
struct Node*toDelete=NULL;
do
if(temp->data==value)
toDelete=temp;
break;
temp=temp->next;
while(temp!=head);
if(toDelete==NULL)
return;
if(toDelete->next==toDelete&&toDelete->prev==toDelete)
head=NULL;
free(toDelete);
return;
if(toDelete==head)
head=head->next;
toDelete->prev->next=toDelete->next;
toDelete->next->prev=toDelete->prev;
free(toDelete);
}
if(head==NULL)
printf("List is empty.\n");
return;
struct Node*temp=head;
int position=1;
do
if(temp->data==value)
return;
temp=temp->next;
position++;
while(temp!=head);
}
void printList()
if(head==NULL)
printf("List is empty.\n");
return;
struct Node*temp=head;
printf("List elements:");
do
printf("%d",temp->data);
temp=temp->next;
while(temp!=head);
printf("\n");
int main()
insertAtBeginning(10);
insertAtBeginning(20);
insertAtBeginning(30);
printList();
search(20);
search(40);
deleteNode(20);
printList();
deleteNode(30);
printList();
deleteNode(10);
printList();
return 0;
OUTPUT:
List elements:30 20 10
List elements: 30 10
List elements:10
List is empty
#include<stdio.h>
int stack[MAX_SIZE];
int top=-1;
void push(int item)
if(top==MAX_SIZE-1)
printf("Stack Overflow\n");
return;
top++;
stack[top]=item;
int pop()
int item;
if(top==-1)
printf("Stack Underflow\n");
return-1;
item=stack[top];
top--;
return item;
int peek()
{
if(top==-1)
printf("Stack Underflow\n");
return-1;
return stack[top];
int isFull()
if(top==MAX_SIZE-1)
printf("Stack is full\n");
else
int isEmpty()
if(top==-1)
printf("Stack is empty\n");
else
void printStack()
{
int i;
if(top==-1)
printf("Stack is empty\n");
return;
printf("Stack elements:");
for(i=top;i>=0;i--)
printf("%d",stack[i]);
printf("\n");
int main()
int choice,item;
while(1)
printf("\n1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case1:
scanf("%d",&item);
push(item);
break;
case2:
item=pop();
if(item!=-1)
printf("Popped item:%d\n",item);
break;
case3:
item=peek();
if(item!=-1)
printf("Top element:%d\n",item);
break;
case4:
isFull();
break;
case5:
isEmpty();
break;
case6:
printStack();
break;
case7:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice\n");
return 0;
OUTPUT:
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
Enter your choice:4
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
Stack elements:20 10
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
Popped item:40
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
Top element:30
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
Stack elements:30 20 10
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
Exiting
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int prec(char c)
if(c=='^')
return 3;
else if(c=='/'||c=='*')
return 2;
else if(c=='+'||c=='-')
return 1;
else
return-1;
}
char associativity(char c)
if(c=='^')
return'R';
return'L';
int i;
char result[1000];
int resultIndex=0;
int len=strlen(s);
char stack[1000];
int stackIndex=-1;
for(i=0;i<len;i++)
char c=s[i];
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||(c>='0'&&c<='9'))
result[resultIndex++]=c;
}
else if(c=='(')
stack[++stackIndex]=c;
else if(c==')')
while(stackIndex>=0 &&stack[stackIndex]!='(')
result[resultIndex++]==stack[stackIndex--];
stackIndex--;
else
while(stackIndex>=0&&(prec(s[i])<prec(stack[stackIndex]) ||
prec(s[i])==prec(stack[stackIndex])&&associativity(s[i])=='L'))
result[resultIndex++]=stack[stackIndex--];
stack[++stackIndex]=c;
}
while(stackIndex>=0)
result[resultIndex++]=stack[stackIndex--];
result[resultIndex]='\0';
int main()
char exp[100];
gets(exp);
infixToPostfix(exp);
return 0;
OUTPUT:
#include<stdio.h>
#include<stdlib.h>
struct Node
int data;
struct Node*next;
};
struct Node*top=NULL;
if(newNode==NULL)
return;
newNode->data=value;
newNode->next=top;
top=newNode;
void pop()
if(top==NULL)
{
printf("Stack Underflow!Cannot pop.\n");
return;
struct Node*temp=top;
int value=top->data;
top=top->next;
free(temp);
int peek()
if(top==NULL)
printf("Stack is empty!\n");
return-1;
return top->data;
int isFull()
}
int isEmpty()
if(top==NULL)
printf("Stack is empty!\n");
else
void print_stack()
struct Node*temp=top;
if(temp==NULL)
printf("Stack is empty.\n");
return;
printf("Stack elements:\n");
while(temp!=NULL)
printf("%d\n",temp->data);
temp=temp->next;
int main()
{
int choice,item;
while(1)
printf("\n1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit\
n");
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&item);
push(item);
break;
case 2:
pop();
break;
case 3:
break;
case 4:
isFull();
break;
case 5:
isEmpty();
break;
case 6:
print_stack();
break;
case 7:
printf("Exiting...\n");
return 0;
default:
printf("Invalid choice\n");
return 0;
OUTPUT:
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
Stack is empty!
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
Stack elements:
22
11
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
Stack elements
22
11
1.Push 2.Pop 3.Peek 4.Is Full 5.Is Empty 6.Print Stack 7.Exit
Exiting…
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
int queue[SIZE];
int front=-1;
int rear=-1;
void isFull()
{
if(rear == SIZE -1)
printf(“Queue is full\n”);
Void is Empty()
If(front==-1||front>rear)
else
If(rear==SIZE -1)
Return;
if(front == -1)
front=0;
rear++;
queue[rear]=value;
printf(“Enqueued:%d\n”,value);
void dequeue()
if(front==-1||front>rear)
return;
printf(“Dequeued:%d\n”,queue[front]);
front++;
if(front>rear)
front=rear=-1;
void peek()
return;
}
printf(“Front item:%d\n”,queue[front]);
void print_queue()
int i;
f(front==-1 || front>rear)
return;
printf(“Queue elements:”);
for(i=front;i<=rear;i++)
printf(“%d”,queue[i]);
printf(“\n”);
int main()
int choice,value;
do
printf(“\nQueue operations:\n”);
printf(“1.Enqueue 2.Dequeue 3.Peek 4.Check if full 5.Check if Empty
6.print_queue 7.Exit\n”);
Scanf(“%d”,&choice);
switch(choice)
case 1:
scanf("%d",&value);
push(value);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
isFull();
break;
case 5:
isEmpty();
break;
case 6:
print_queue();
break;
case 7:
printf("Exiting...\n");
return 0;
default:
while(choice !=7);
return 0;
OUTPUT:
Queue operations:
Enqueued:11
Queue operations:
1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue
7.Exit
Enqueued:22
Queue operations:
Enqueued:33
Queue operations:
Queue elements:11 22 33
Queue operations:
Front item:11
Queue operations:
1.Enqueue 2.dequeue 3.Peek 4. Check if full 5.Check if empty 6.print_queue
7.Exit
Queue operations:
Queue operations:
Dequeued:11
Queue operations:
Queue elements:22 33
Queue operations:
Exiting…
8. Write Programs to implement the Queue operations using Liked
List.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
struct Node* front=NULL;
struct Node* rear=NULL;
int isFull()
{
printf(" A linked list is dynamically allocated,\n so
its never full unless memory allocation fails\n");
}
int isEmpty()
{
if(front==NULL)
printf("Queue is empty.\n");
else
printf("Queue is not empty.\n");
}
void insert(int value)
{
struct Node*
newNode=(structNode*)malloc(sizeof(structNode));
if(newNode==NULL)
{
printf(" Memory allocation failed! Queue is
full.\n");
return;
}
newNode->data=value;
newNode->next=NULL;
if(front==NULL && rear==NULL)
{
front=rear=newNode;
}
else
{
rear->next=newNode;
rear=newNode;
}
printf("Inserted %d into the queue.\
n",value);
}
void delete()
{
if(front==NULL)
{
printf("Queue is empty! cannot delete.\
n");
return;
}
struct Node* temp=front;
int value=temp->data;
front=front->next;
if(front==NULL)
rear=NULL;
free(temp);
printf("Deleted %d from the queue.\
n",value);
}
void peek()
{
if(front==NULL)
printf("Queue is empty! cannot peek.\n");
else
printf("Front element is:%d\n",front->data);
}
void print()
{
if(front==NULL)
{
printf("Queue is empty.\n");
return;
}
struct Node*temp=front;
printf("Queue elements:");
while(temp !=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
printf("\n");
}
int main()
{
int choice,value;
do
{
printf("1.Insert 2.Delete 3.Peek 4.Print 5.Check
if Empty 6. Check if full 7.Exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter value to insert:");
scanf("%d",&value);
insert(value);
break;
case 2:
delete();
break;
case 3:
peek();
break;
case 4:
print();
break;
case 5:
isEmpty();
break;
case 6:
isFull();
break;
case 7:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
}
while(choice!=7);
return 0;
}
OUTPUT:
1.Insert 2.Delete 3.Peek 4.Print 5.Check if Empty
6.Check if Full 7.Exit
Enter your choice:5
Queue is empty.
#include<stdio.h>
int main()
int arr[100],n,target,i,found=0;
scanf(“%d”,&n);
printf(“Enter %d elements :\n”,n);
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
scanf(“%d,&target”);
for(i=0;i<n;i++)
if(arr[i]==target)
found=1;
break;
if(!found)
return 0;
OUTPUT 1:
Enter 7 Elements:
11 16 21 13 19 9 4
OUTPUT 2:
Enter 7 Elements:
11 16 21 13 19 9 4
b. Binary Search.
#include<stdio.h>
int main()
int arr[100];
int n,i,target,low,high,mid,found=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
low=0;
high=n-1;
while(low<=high)
mid=(low+high)/2;
if(arr[mid]==target)
found=1;
break;
else if(arr[mid]<target)
low=mid+1;
} else
high=mid-1;
if(!found)
return 0;
}
OUTPUT 1:
1 3 5 7 9 11 13 15 17 19
OUTPUT 2:
1 3 5 7 9 11 13 15 17 19
a. Bubble Sort
b. Insertion Sort
c. Quick Sort
a. Bubble sort:
#include<stdio.h>
int main()
int n,i,j,temp;
int A[100];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
for(i=0;i<n-1;i++)
for(j=0;j<n-i-1;j++)
if(A[j]>A[j+1])
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
printf("Sorted array:\n");
for(i=0;i<n;i++)
printf("%d",A[i]);
printf("\n");
return 0;
}
OUTPUT:
4 1 9 5 0 6
Sorted array:
0 1 4 5 6 9
b. Insertion sort:
#include<stdio.h>
int main()
int A[100],n,i,j,key;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
for(i=1;i<n;i++)
key=A[i];
j=i-1;
while(j>=0&&A[j]>key)
{
A[j+1]=A[j];
j=j-1;
A[j+1]=key;
printf("Sorted array:\n");
for(i=0;i<n;i++)
printf("%d",A[i]);
printf("\n");
return 0;
OUTPUT:
9 1 5 3 7 2 6
Sorted array:
1 2 3 5 6 7 9
c. Quick sort:
#include<stdio.h>
int pivot=arr[high];
int i=low-1;
int j;
for(j=low;j<high;j++)
if(arr[j]<=pivot)
i++;
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
int temp=arr[i+1];
arr[i+1]=arr[high];
arr[high]=temp;
return i+1;
if(low<high)
int pi=partition(arr,low,high);
quicksort(arr,low,pi-1);
quicksort(arr,pi+1,high);
int main()
int arr[100],n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
quicksort(arr,0,n-1);
printf("Sorted array:");
for(i=0;i<n;i++)
printf("%d",arr[i]);
printf("\n");
return 0;
OUTPUT:
Sorted array:
1 2 3 5 7 8 9