Professional Documents
Culture Documents
Mcse Ads Amit Asthana
Mcse Ads Amit Asthana
P)
ADVANCED DATA STRUCTURES
5
(MCSE-107)
MASTERS OF TECHNOLOGY
IN
COMPUTER SCIENCE AND ENGINEERING
SUBMITTED BY:
AMIT ASTHANA (0103CS21MT05)
PRINCY JAIN(0103CS21MT20)
1. Write a menu based program to perform basic operations on
dynamic implementation of Stack.
#include <stdio.h>
#include <stdlib.h>
struct node
int data;
}*top = NULL;
#define MAX 5
// function prototypes
void push();
void pop();
void empty();
void stack_full();
void stack_count();
void destroy();
void print_top();
void main(){
int choice;
while (1) {
scanf("%d",&choice);
switch (choice) {
case 1:
push();
break;
case 2:
pop();
break;
case 3:
empty();
break;
case 4:
stack_full();
break;
case 5:
stack_count();
break;
case 6:
destroy();
break;
case 7:
print_top();
break;
case 8:
exit(0);
default:
printf("wrong choice\n");
}}}
void push(){
int val,count;
count = st_count();
printf("\nEnter value which you want to push into the stack :\n");
scanf("%d",&val);
temp->data = val;
temp->link = top;
top = temp;
}else
void pop(){
if (top = = NULL)
printf("**Stack is empty**\n");
else
{ temp = top;
top = top->link;
free(temp);
}}
void empty(){
if (top == NULL)
printf("STACK IS EMPTY\n");
else
void stack_full(){
int count;
count = st_count();
if (count = = MAX)
{ printf("stack is full\n");
} else
void stack_count(){
int count = 0;
temp = top;
printf(" %d\n",temp->data);
temp = temp->link;
count++;
int st_count(){
int count = 0;
temp = top;
temp = temp->link;
count++;
} return count;
void destroy(){
temp = top;
pop();
temp = temp->link;
}printf("stack destroyed\n");
void print_top(){
if (top == NULL)
else
}
2. Write a program which takes infix expression as input and
convert it into an equivalent postfix expression
#include<stdio.h>
#include<ctype.h>
char stack[100];
stack[++top] = x;
char pop(){
if(top == -1)
return -1;
else
return stack[top--];
if(x == '(')
return 0;
return 1;
return 2;
return 0;
}
int main(){
char exp[100];
char *e, x;
scanf("%s",exp);
printf("\n");
e = exp;
while(*e != '\0') {
if(isalnum(*e))
printf("%c ",*e);
push(*e);
} else {
printf("%c ",pop());
push(*e);
}e++; }
while(top != -1) {
printf("%c ",pop());
}return 0;
}
3. Write a menu based program for dynamic implementation of
Circular queue.
# define MAX 5
int cqueue_arr[MAX];
return;
if(front == -1)
front = 0;
rear = 0;
else
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
void deletion()
if(front == -1)
printf("Queue Underflown");
return ;
if(front == rear)
front = -1;
rear=-1;
else
if(front == MAX-1)
front = 0;
else
front = front+1;
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
printf("Queue is emptyn");
return;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
else
printf("%d ",cqueue_arr[front_pos])
front_pos++;
front_pos = 0;
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
printf("n");
int main()
int choice,item;
do
printf("1.Insertn");
printf("2.Deleten");
printf("3.Displayn");
printf("4.Quitn");
scanf("%d",&choice);
switch(choice)
case 1 :
scanf("%d", &item);
insert(item);
break;
case 2 :
deletion();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choicen");
}while(choice!=4);
return 0;
}
4. Write a program to extend assignment-1 to perform deletion of a
node in a singly linked list.
Iterative Method:
To delete a node from the linked list, we need to do the following steps.
1) Find the previous node of the node to be deleted.
2) Change the next of the previous node.
3) Free memory for the node to be deleted.
#include <bits/stdc++.h>
class Node{
public:
int data;
Node* next;
};
// list.
new_node->next = (*head_ref);
(*head_ref) = new_node;
return;
else {
prev = temp;
temp = temp->next;
if (temp == NULL)
return;
prev->next = temp->next;
// Free memory
delete temp;
}}
// given node
node = node->next;
}}
// Driver code
int main(){
push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
printList(head);
deleteNode(&head, 1);
printList(head);
return 0;
}
5. Write a program to perform preorder traversal in a binary tree.
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
};
temp->val = item;
return temp;
if (root != NULL) {
preorder(root->left);
preorder(root->right);
}}
// Here we are finding where to insert the new node so BST is followed
return node;
int main() {
200
/ \
120 280
/ \ / \
insert(root, 120);
insert(root, 80);
insert(root, 160);
insert(root, 280);
insert(root, 240);
insert(root, 320);
preorder(root);
return 0;
}
6. Write a program to search and insert a node in a Binary Search
Tree (BST).
ALGORITHM:
1. Create a new BST node and assign values to it.
2. insert(node, key)
i) If root == NULL,
call the insert function with root=>right and assign the return value in root=>right.
root->right = insert(root=>right,key)
call the insert function with root->left and assign the return value in root=>left.
root=>left = insert(root=>left,key)
/*
* 2. if the tree traversal reaches NULL, it will return the new node
*/
if(root == NULL)
return getNewNode(val);
/*
* if given val is greater than root->key,
* we should find the correct place in the right subtree and insert the new node
*/
root->right = insert(root->right,val);
/*
* we should find the correct place in the left subtree and insert the new node
*/
root->left = insert(root->left,val);
/*
* 1.if root->key == val it will straight away return the address of the root node
* 2.After the insertion, it will return the original unchanged root's address
*/
return root;
}
7. Write a program to sort element of array using following
techniques:
a. Insertion sort.
b. Bubble sort.
soln:
a. insertion sort:
// Insertion sort in C++
#include <iostream>
int j = step - 1;
// Compare key with each element on the left of it until an element smaller than
// it is found.
array[j + 1] = array[j];
--j;
} array[j + 1] = key;
}}
// Driver code
int main() {
insertionSort(data, size);
printArray(data, size);
b. Bubble sort:
#include<stdio.h>
int i;
printf("%d ",a[i]);
}}
int i, j, temp;
temp = a[i];
a[i] = a[j];
a[j] = temp;
}}}}
int i, j,temp;
int n = sizeof(a)/sizeof(a[0]);
print(a, n);
bubble(a, n);
print(a, n);
}
8. Write a program to perform Quick sort on elements of array.
#include<stdio.h>
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}}int main(){
int i, count, number[25];
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;