DSA C Programs
DSA C Programs
int stack[MAX];
int top;
int isEmpty()
{
if (top==-1)
return 1;
else
return 0;
}
int size()
{
return top+1;
}
int pop()
{
int t;
if(isEmpty())
{
printf("Stack empty");
return -1;
}
t=stack[top];
top=top-1;
return t;
}
int peek()
{
int t;
if(isEmpty())
{
printf("Stack empty");
return -1;
}
t=stack[top];
// top=top-1;
return t;
Page 1 of 30
Neepa Shah
void show()
{
int i;
printf("\nThe Stack elements are:");
for(i=top;i>=0;i--)
{
printf("%d\t",stack[i]);
}
}
int main()
{
char ch , a='y';
int choice, data;
top=-1;
printf("1.Insert");
printf("\n2.Delete");
printf("\n3.show or display");
do
{
printf("\nEnter your choice for the operation: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the token to be inserted:");
scanf("%d",&data);
push(data);
show();
break;
case 2:
data=pop();
printf("\nThe token deleted is %d",data);
show();
break;
case 3:
data = peek();
printf("\nThe token at top is %d",data);
show();
break;
default:
printf("Wrong choice");
break;
}
printf("\nDo you want to continue(y/n):");
scanf(" %c", &ch);
}while(ch=='y'||ch=='Y');
}
Page 2 of 30
Neepa Shah
char s[SIZE];
int top = -1;
char pop()
{
return (s[top--]);
}
void main()
{
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("\n\nRead the Infix Expression ? ");
scanf("%s", infx);
push('#');
else
{ /* Operator */
while (pr(s[top]) >= pr(ch))
Page 3 of 30
Neepa Shah
pofx[k++] = pop();
push(ch);
}
}
LINEAR QUEUE
#define MAX 4
#include<stdio.h>
#include<conio.h>
int queue[MAX];
int front, rear;
int dequeue()
{
int t;
if (front == - 1) // if (isEmpty())
{
printf("Queue Underflow \n");
return -999;
}
else
{
if (front == rear)
{
Page 4 of 30
Neepa Shah
t = queue[front];
front = rear = -1;
}
else
{
t = queue[front];
front = front + 1;
}
return t;
}
}
int front1()
{
int t;
if (front == - 1) // if (isEmpty())
{
printf("Queue Underflow \n");
return -999;
}
else
{
t = queue[front];
return t;
}
}
void show()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("\nThe Queue elements are:");
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}
int main()
{
char ch , a='y';
int choice, data;
front = rear = -1;
printf("\n1.Insert");
printf("\n2.Delete");
printf("\n3.show or display");
do
{
printf("\nEnter your choice for the operation: ");
scanf("%d",&choice);
switch(choice)
Page 5 of 30
Neepa Shah
{
case 1:
printf("\nEnter the token to be inserted:");
scanf("%d",&data);
enqueue(data);
show();
printf("Element at front %d", front1());
show();
break;
case 2:
data=dequeue();
if(data != -999)
printf("\nThe token deleted is %d",data);
show();
break;
case 3:
show();
break;
default:
printf("Wrong choice");
break;
}
printf("\nDo you want to continue(y/n):");
scanf(" %c", &ch);
}while(ch=='y'||ch=='Y');
}
CIRCULAR QUEUE
WAY 1:
#include <stdio.h>
#include<stdlib.h>
#define SIZE 5
int a[SIZE];
int rear,front;
int temp;
int isEmpty()
{
if(front==-1 && rear==-1)
return 1;
else
return 0;
}
void enqueue(int v)
{
if((front == 0 && rear == SIZE-1) || (front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if (isEmpty()) /*If queue is empty */
{
front = 0;
Page 6 of 30
Neepa Shah
rear = 0;
}
else
{
if(rear == SIZE-1) /*rear is at last position of queue */
rear = 0;
else
rear = rear + 1;
}
a[rear] = v ;
printf("\n\nRear = %d Front = %d ",rear,front);
}
int dequeue()
{
int temp;
if(isEmpty())
printf("queue is empty");
temp = a[front];
if(front == rear) /* queue has only one element */
{
front = -1;
rear = -1;
}
else
{
if(front == SIZE-1)
front = 0;
else
front = front + 1;
}
printf("\n\nRear = %d Front = %d ",rear,front);
return temp;
}
void display()
{
int i;
if(isEmpty())
printf("Circular Queue is Empty");
else
{
if(rear < front)
{
for(i = front;i <= SIZE-1;i++)
printf("%d ", a[i]);
for(i = 0;i <= rear;i++)
printf("%d ", a[i]);
}
else
{
for(i = front;i <= rear;i++)
printf("%d ", a[i]);
printf("\n");
Page 7 of 30
Neepa Shah
}
}
}
void main()
{
int t, ch, num;
rear = -1, front = -1;
while(1)
{
printf("\n\nMAIN MENU\n1.INSERTION\n2.DELETION\n3.EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nENTER THE QUEUE ELEMENT : ");
scanf("%d",&num);
enqueue(num);
display();
break;
case 2:
t = dequeue();
printf("\n\nDELETED ELEMENT FROM QUEUE IS : %d ",t);
display();
break;
case 3:
exit(0);
default: printf("\n\nInvalid Choice . ");
}
}
}
WAY 2
#include <stdio.h>
#include<stdlib.h>
#define SIZE 5
int a[SIZE];
int rear,front;
int temp;
int isEmpty()
{
if(front==-1 && rear==-1)
return 1;
else
return 0;
}
int size()
{
Page 8 of 30
Neepa Shah
void enqueue(int v)
{
if(SIZE == size())
{
printf("queue is full");
return;
}
else
{
if(isEmpty())
{
front=0;
rear=0;
}
else
rear=(rear+1) % SIZE;
}
a[rear]=v ;
printf("\n\nRear = %d Front = %d ",rear,front);
}
int dequeue()
{
int temp;
if(isEmpty())
printf("queue is empty");
else
{
temp=a[front];
if(front==rear)
front = rear = -1;
else
front=(front+1) % SIZE;
}
printf("\n\nRear = %d Front = %d ",rear,front);
return temp;
}
void display()
{
int i;
if(isEmpty())
printf("Circular Queue is Empty");
else
{
if(rear < front)
{
for(i = front;i <= SIZE-1;i++)
printf("%d ", a[i]);
for(i = 0;i <= rear;i++)
printf("%d ", a[i]);
}
Page 9 of 30
Neepa Shah
else
{
for(i = front;i <= rear;i++)
printf("%d ", a[i]);
printf("\n");
}
}
}
void main()
{
int t, ch, num;
rear = -1, front = -1;
while(1)
{
printf("\n\nMAIN MENU\n1.INSERTION\n2.DELETION\n3.EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nENTER THE QUEUE ELEMENT : ");
scanf("%d",&num);
enqueue(num);
display();
break;
case 2:
t = dequeue();
printf("\n\nDELETED ELEMENT FROM QUEUE IS : %d ",t);
display();
break;
case 3:
exit(0);
default: printf("\n\nInvalid Choice . ");
}
}
}
SLL
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head,*var,*trav;
Page 10 of 30
Neepa Shah
if(head == NULL)
{
head = var;
var->next = NULL;
}
else
{
var->next = head;
head = var;
}
}
if(head==NULL)
{
head = var;
head->next = NULL;
}
else
{
while(trav->data != d)
trav = trav->next;
var->next = trav->next;
trav->next = var;
}
}
Page 11 of 30
Neepa Shah
while(temp != NULL)
{
if(temp->data == value)
{
if(temp == head)
{
head = temp->next;
free(temp);
return 0;
}
else
{
var->next = temp->next;
free(temp);
return 0;
}
}
else
{
var = temp;
temp = temp->next;
}
}
printf("data deleted from list is %d",value);
}
int delete_from_end()
{
struct node *temp;
temp = head;
while(temp->next != NULL)
{
var = temp;
temp = temp->next;
}
if(temp == head)
{
head = temp->next;
free(temp);
return 0;
}
printf("data deleted from list is %d",temp->data);
var->next=NULL;
free(temp);
return 0;
}
void display()
{
trav = head;
if(trav == NULL)
printf("\nList is Empty");
else
{
printf("\nElements in the List: ");
Page 12 of 30
Neepa Shah
while(trav!=NULL)
{
printf(" -> %d ",trav->data);
trav=trav->next;
}
printf("\n");
}
}
int main()
{
int ch = 0, value, data;
head = NULL;
printf("1:insertion at begning of linked list");
printf("\n2:insertion at the end of linked list");
printf("\n3:insertion at the middle where you want");
printf("\n4:deletion from the end of linked list");
printf("\n5:deletion of the data that you want");
printf("\n6:exit\n");
while(1)
{
printf("\nenter the choice of operation to perform on linked list");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("\nenter the value to be inserted");
scanf("%d",&value);
insert_at_begin(value);
display();
break;
case 2:
printf("\nenter value to be inserted");
scanf("%d",&value);
insert_at_end(value);
display();
break;
case 3:
printf("\nafter which data you want to insert the data");
scanf("%d",&data);
printf("\nenter the value to be inserted");
scanf("%d",&value);
insert_at_middle(value, data);
display();
break;
case 4:
delete_from_end();
display();
break;
case 5:
display();
printf("\nenter the data that you want to delete from the list shown above");
scanf("%d",&value);
delete_from_middle(value);
display();
Page 13 of 30
Neepa Shah
break;
case 6:
exit(0);
}
}
}
Stack using LL
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int Data;
struct Node *next;
}*top;
void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}
void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
Page 14 of 30
Neepa Shah
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}
int main()
{
int i=0, value;
top=NULL;
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf(" \nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
printf("\nEnter a value to push into Stack: ");
scanf("%d",&value);
push(value);
display();
break;
case 2:
popStack();
display();
break;
case 3:
display();
break;
case 4:
exit(0);
}
}
}
Queue using LL
#include<stdio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
};
struct node *front,*rear;
void insert(void)
{
struct node *nn;
Page 15 of 30
Neepa Shah
void deletion(void)
{
struct node *temp;
int x;
if(front==NULL)
{
printf("queue is empty\n");
return;
}
temp=front;
x=temp->data;
if(front==rear)
{
front=rear=NULL;
}
else
{
front=front->next;
}
free(temp);
printf("%d is succ. deleted\n",x);
return;
}
void display(void)
{
struct node *temp;
if(front==NULL)
{
printf("queue is empty\n");
return;
}
temp=front;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
return;
Page 16 of 30
Neepa Shah
void main()
{
int c;
front=rear=NULL;
do
{
printf("1:insert\n2:delete\n3:display\n4:exit\nenter choice:");
scanf("%d",&c);
switch(c)
{
case 1:insert();break;
case 2:deletion();break;
case 3:display();break;
case 4:printf("program ends\n");break;
default:printf("wrong choice\n");
}
}while(c!=4);
}
Develop Code to Implement different operations on linked list –copy, concatenate, split, reverse,
count no. of nodes etc.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head,*var,*trav;
Page 17 of 30
Neepa Shah
q = p;
p = p->next;
q->next = r;
}
return q;
}
Page 18 of 30
Neepa Shah
}
while(q!=NULL)
{
c++;
q=q->next;
}
return c;
}
int main()
{
int i=0;
struct node *rev, *dest, *cat, *splt;
head=NULL;
for(i=0; i<4; i++)
insert_at_begning(i);
printf("\nOriginal: 3 to 0");
display(head);
Page 19 of 30
Neepa Shah
rev = reverse(head);
printf("\nReverse: 0 to 3");
display(rev);
dest = copy(rev);
printf("\nCopy: 0 to 3");
display(dest);
i=count(cat);
printf("\nConcated list count: %d", i);
DLL
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
struct node {
int data;
struct node *next;
struct node *prev;
};
int length() {
int length = 0;
struct node *current;
return length;
}
void displayForward() {
struct node *ptr = head;
Page 20 of 30
Neepa Shah
while(ptr != NULL) {
printf("%d ",ptr->data);
ptr = ptr->next;
}
}
void displayBackward() {
struct node *ptr = tail;
while(ptr != NULL) {
printf("%d ",ptr->data);
ptr = ptr ->prev;
}
}
if (head != NULL)
head->prev = new_node;
else
tail = new_node;
new_node->next = head;
head = new_node;
}
if (head == NULL) {
new_node->prev = NULL;
head = new_node;
}
else
{
tail->next = new_node;
new_node->prev = tail;
}
tail = new_node;
}
Page 21 of 30
Neepa Shah
insertFirst(key);
else
next_node = next_node->next;
new_node->next = prev_node->next;
prev_node->next = new_node;
new_node->prev = prev_node;
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
}
Page 22 of 30
Neepa Shah
}
else
{
while(del->data != key)
del = del->next;
if (del != NULL)
{
if (head == del)
head = del->next;
if(tail == del)
tail = tail->prev;
if (del->next != NULL)
del->next->prev = del->prev;
if (del->prev != NULL)
del->prev->next = del->next;
free(del);
}
else
printf("Node not found");
}
}
int main() {
insertFirst(10);
insertFirst(20);
insertFirst(30);
insertFirst(40);
insertFirst(50);
printf("\n");
printf("\nList (Last to first): ");
displayBackward();
Page 23 of 30
Neepa Shah
delete1(30);
displayForward();
insertLast(100);
displayForward();
return 0;
}
Page 24 of 30
Neepa Shah
if(data >(node->data))
node->right = Insert(node->right,data);
else if(data < (node->data))
node->left = Insert(node->left,data);
return node;
}
Page 25 of 30
Neepa Shah
if(node==NULL)
return;
printf("%d ",node->data);
PrintPreorder(node->left);
PrintPreorder(node->right);
}
int main()
{
treeNode *root = NULL;
treeNode * temp;
root = Insert(root, 5);
root = Insert(root, -1);
root = Insert(root, 3);
root = Insert(root, -14);
root = Insert(root, 8);
root = Insert(root, 10);
root = Insert(root, 9);
root = Insert(root, 6);
PrintInorder(root);
printf("\n");
root = Delete(root,5);
printf("\n");
PrintInorder(root);
root = Delete(root,-1);
printf("\n");
PrintInorder(root);
printf("\n");
Merge Sort
#include<stdio.h>
#define MAX 50
void main()
{
int a[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);
Page 26 of 30
Neepa Shah
sort(a,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}
if(l>mid)
{
for(k=m;k<=high;k++)
{
temp[i]=arr[k];
i++;
}
}
Page 27 of 30
Neepa Shah
else
{
for(k=l;k<=mid;k++)
{
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++)
{
arr[k]=temp[k];
}
}
Quick Sort
#include<stdio.h>
#define MAX 50
void main()
{
int i, n=8;
int a[] = {121,9,4,99,-120,1,3,10};
/* int a[MAX];
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
} */
if (q-p < 2)
{
return;
}
int m = partition(a, p, q) ;
Page 28 of 30
Neepa Shah
sort(a, p, m);
sort(a, m+1, q);
}
Linear Search
#include <stdio.h>
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int result = search(arr, 5, 5);
if (result == -1)
printf("Element is not present in array");
else
printf("Element is present at index %d", result);
return 0;
}
Page 29 of 30
Neepa Shah
count++;
}
}
return count;
}
int main(void)
{
int arr[] = { 2, 10, 4, 10, 40 };
int result = search(arr, 5, 10);
if (result == 0)
printf("Element is not present in array");
else
printf("Element is present %d times", result);
return 0;
}
Binary Search
#include <stdio.h>
if (a[mid] < x)
low = mid + 1;
else if (x < a[mid])
high = mid - 1;
else
return mid; // found
}
return -1; // not found
}
int main(void)
{
int arr[] = { 2, 4, 5, 10, 40, 50 };
int result = binarySearch(arr, 5, 6);
if (result == -1)
printf("Element is not present in array");
else
printf("Element is present at %d", result);
}
Page 30 of 30