DSA_Program_file1
DSA_Program_file1
Faridabad
Sec on : D
Index
S.No Program/AIM Page No.
1. Write a program to find max number and sum using Array
if(head == NULL)
head = temp;
else
{
temp->link = head;
head = temp;
}
prin ("\n Element Inserted at Beginning");
display();
}
void insEnd(int ele)
{
struct Node *temp;
temp = malloc(sizeof(struct Node));
temp->info = ele;
temp->link = NULL;
if(head == NULL)
head = temp;
else
{
struct Node *last = head;
while(last->link != NULL)
last=last->link;
last->link=temp;
}
prin ("\n Element Inserted at END");
display();
}
void insAny(int ele, int pos)
{
struct Node *temp;
temp = malloc(sizeof(struct Node));
temp->info = ele;
temp->link = NULL;
int count=countNodes();
if(pos>=1 && pos<=count+1)
{
if(pos==1)
insBeg(ele);
else if(pos==count+1)
insEnd(ele);
else
{
struct Node *prev = head;
int cnt=1;
while(cnt<pos-1)
{
cnt++;
prev = prev->link;
}
temp->link = prev->link;
prev->link = temp;
}
prin ("\n Element Inserted at posi on: %d", pos);
display();
}
else
prin ("\n Invalid posi on of inser on \n");
}
void delBeg()
{
if(head==NULL)
{
prin ("\n Underflow \n");
}
else
{
struct Node *temp = head;
head=head->link;
free(temp);
prin ("\n Element Deleted from Beginning");
display();
}
}
void delEnd()
{
if(head==NULL)
{
prin ("\n Underflow \n");
return;
}
if(head->link==NULL)
delBeg();
else
{
struct Node *temp = head;
// Move to last but one node
while(temp->link->link != NULL)
{
temp=temp->link;
}
free(temp->link);
temp->link=NULL;
prin ("\n Element Deleted from END");
display();
}
}
void delAny(int pos)
{
int count;
if(head==NULL)
{
prin ("\n Underflow \n");
return;
}
count=countNodes();
return 0;
}
Program No. 8
Aim: Write a program to implement push pop opera on in stack.
#include <stdio.h>
#define SIZE 5
int Top = -1;
void display(int Stack[]);
void push (int Stack[], int data);
void pop (int Stack[]);
int main()
{
int Stack[SIZE], data, choice;
do
{
prin ("\n Select an op on from below: ");
prin ("\n 1. Push into Stack ");
prin ("\n 2. Pop from Stack ");
prin ("\n 3. Display Stack ");
prin ("\n 4. Exit ");
prin ("\n Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
prin ("\n Input Element: ");
scanf("%d", &data);
push(Stack, data);
break;
case 2:
pop(Stack);
break;
case 3:
display(Stack);
break;
case 4:
break;
default:
prin ("\n Invalid Choice");
}
}while(choice!=4);
return 0;
}
void display(int Stack[])
{
int i;
if (Top == -1)
prin ("\n Stack is empty\n");
else
{
prin ("\n Elements in Stack are: ");
for(i=Top; i>=0; i--)
{
prin ("\t%d", Stack[i]);
}
prin ("\n");
}
}
void push(int Stack[], int data)
{
if (Top == SIZE-1)
prin ("\n Stack OVERFLOW\n");
else
{
Top++;
Stack[Top] = data;
prin (" Element Pushed: %d\n", Stack[Top]);
}
display(Stack);
}
void pop (int Stack[])
{
if (Top == -1)
prin ("\n Stack UNDERFLOW\n");
else
{
prin (" Element Popped: %d\n", Stack[Top]);
Top--;
}
display(Stack);
}
Program No. 9
Aim: Write a program to implement push pop opera on in stack using
linked list.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int info;
struct Node *link;
}*top;
void create();
void display();
int countNodes();
void push(int ele);
void pop();
int main()
{
int choice, ele, pos;
create();
do
{
prin ("\n ****MENU for Dynamic Stack***** ");
prin ("\n 1:Display");
prin ("\n 2:Count Nodes");
prin ("\n 3:Push");
prin ("\n 4:Pop");
prin ("\n 0:Exit");
prin ("\n Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 0:
prin ("Exi ng .... ");
break; //System.exit(0);
case 1:
display();
break;
case 2:
int cnt = countNodes();
prin (" No of nodes = %d \n", cnt);
break;
case 3:
prin ("Input element to insert: ");
scanf("%d", &ele);
push(ele);
break;
case 4:
pop();
break;
default:
prin ("Wrong choice");
}
}while(choice!=0);
}
void create()
{
struct Node *temp, *newNode;
int ch, info;
temp = malloc(sizeof(struct Node));
prin ("\n Input info for First Node: ");
scanf("%d",&info);
temp->info = info;
temp->link = NULL;
top = temp;
}
void display()
{
prin ("\n Linked list is ");
prin ("\n top=%u\n", top);
struct Node *temp = top;
while(temp!=NULL)
{
prin (" %d and Next Address: %u\n ", temp->info, temp->link);
temp = temp->link;
}
}
int countNodes()
{
int count = 0;
struct Node *temp = top;
while(temp!=NULL)
{
count = count + 1;
temp = temp->link;
}
return count;
}
void push(int ele)
{
struct Node *temp;
temp = malloc(sizeof(struct Node));
temp->info = ele;
temp->link = NULL;
if(top == NULL)
top = temp;
else
{
temp->link = top;
top = temp;
}
prin ("\n Element Inserted at Beginning");
display();
}
void pop()
{
if(top==NULL)
{
prin ("\n Underflow \n");
}
else
{
struct Node *temp = top;
top=top->link;
free(temp);
prin ("\n Element Deleted from Beginning");
display();
}
}
Program No. 10
Aim: Write a program to implement inser on and dele on opera on in
Queue.
#include <stdio.h>
#define SIZE 5
int Queue[SIZE];
int Front = -1;
int Rear = -1;
void display();
void enqueue (int data);
void dequeue ();
int main()
{
int data, choice;
do
{
prin ("\n Select an op on from below: ");
prin ("\n 1. Insert into Queue ");
prin ("\n 2. Delete from Queue ");
prin ("\n 3. Display Queue ");
prin ("\n 4. Exit ");
prin ("\n Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
prin ("\n Input Element: ");
scanf("%d", &data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
break;
default:
prin ("\n Invalid Choice");
}
}while(choice!=4);
return 0;
}
void display()
{
int i;
if (Front == -1)
prin ("\n Queue is empty\n");
else
{
prin ("\n Elements in Queue are: ");
for(i=Front; i<=Rear; i++)
{
prin ("\t%d", Queue[i]);
}
prin ("\n");
}
}
void enqueue(int data)
{
if (Rear == SIZE-1)
prin ("\n Queue OVERFLOW\n");
else
{
if (Front == -1)
Front++;
Rear++;
Queue[Rear] = data;
prin (" Element Enqueued: %d\n", Queue[Rear]);
}
display();
}
void dequeue ()
{
if (Front == -1)
prin ("\n Queue UNDERFLOW\n");
else
{
prin (" Element Removed: %d\n", Queue[Front]);
Front++;
if (Front > Rear)
Front = Rear = -1;
}
display();
}
Program No. 11
break;
case 2:
display_original(arr, size);
prin ("\n Execu ng Selec on Sort \n");
start = clock();
selec on_sort(arr, size);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;
case 3:
display_original(arr, size);
prin ("\n Execu ng Inser on Sort \n");
start = clock();
inser on_sort(arr, size);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;
case 4:
display_original(arr, size);
prin ("\n Execu ng Heap Sort \n");
start = clock();
heap_sort(arr, size);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;
case 5:
display_original(arr, size);
prin ("\n Execu ng Merge Sort \n");
start = clock();
merge_sort(arr, 0, size - 1);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;
case 6:
display_original(arr, size);
prin ("\n Execu ng Quick Sort \n");
start = clock();
quick_sort(arr, 0, size - 1);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;
case 7:
display_original(arr, size);
prin ("\n Execu ng Radix Sort \n");
start = clock();
radix_sort(arr, size);
end = clock();
exec_ me = ((double) (end - start)) / CLOCKS_PER_SEC;
display(arr, size, exec_ me);
break;
default:
prin ("\n\n Invalid Input \n\n");
}
}while(choice !=0);
return 0;
}