Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

LNCT GROUP OF COLLEGES BHOPAL(M.

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;

struct node *link;

}*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) {

printf("1. push an element \n");

printf("2. pop an element \n");


printf("3. check if stack is empty \n");

printf("4. check if stack is full \n");

printf("5. count/display elements present in stack \n");

printf("6. empty and destroy stack \n");

printf("7. Print top of the stack \n");

printf("8. exit \n");

printf("Enter your choice \n");

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");

}}}

// to insert elements in stack

void push(){

int val,count;

struct node *temp;

temp = (struct node*)malloc(sizeof(struct node));

count = st_count();

if (count <= MAX - 1){

printf("\nEnter value which you want to push into the stack :\n");

scanf("%d",&val);

temp->data = val;

temp->link = top;

top = temp;
}else

printf("WARNING: STACK FULL\n");

// to delete elements from stack

void pop(){

struct node *temp;

if (top = = NULL)

printf("**Stack is empty**\n");

else

{ temp = top;

printf("Value popped out is %d \n",temp->data);

top = top->link;

free(temp);

}}

// to check if stack is empty

void empty(){

if (top == NULL)

printf("STACK IS EMPTY\n");

else

printf("elements are present, stack is not empty \n");

// to check if stack is full

void stack_full(){

int count;
count = st_count();

if (count = = MAX)

{ printf("stack is full\n");

} else

printf("stack is not full \n");

// to count the number of elements

void stack_count(){

int count = 0;

struct node *temp;

temp = top;

while (temp! = NULL){

printf(" %d\n",temp->data);

temp = temp->link;

count++;

} printf("size of stack is %d \n",count);

int st_count(){

int count = 0;

struct node *temp;

temp = top;

while (temp! = NULL) {

temp = temp->link;

count++;
} return count;

// to empty and destroy the stack

void destroy(){

struct node *temp;

temp = top;

while (temp! = NULL){

pop();

temp = temp->link;

}printf("stack destroyed\n");

// to print top element of stack

void print_top(){

if (top == NULL)

printf("\n**Top is not available for an EMPTY stack**\n");

else

printf("\nTop of the stack is %d \n",top->data);

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

int top = -1;

void push(char x){

stack[++top] = x;

char pop(){

if(top == -1)

return -1;

else

return stack[top--];

}int priority(char x){

if(x == '(')

return 0;

if(x == '+' || x == '-')

return 1;

if(x == '*' || x == '/')

return 2;

return 0;

}
int main(){

char exp[100];

char *e, x;

printf("Enter the expression : ");

scanf("%s",exp);

printf("\n");

e = exp;

while(*e != '\0') {

if(isalnum(*e))

printf("%c ",*e);

else if(*e == '(')

push(*e);

else if(*e == ')') {

while((x = pop()) != '(')

printf("%c ", x);

} else {

while(priority(stack[top]) >= priority(*e))

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

int front = -1;

int rear = -1;

void insert(int item)

if((front == 0 && rear == MAX-1) || (front == rear+1))

printf("Queue Overflow n");

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 ;

printf("Element deleted from queue is : %dn",cqueue_arr[front]);

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("Queue elements :n");

if( front_pos <= rear_pos )

while(front_pos <= rear_pos)

printf("%d ",cqueue_arr[front_pos]);

front_pos++;

else

while(front_pos <= MAX-1)

printf("%d ",cqueue_arr[front_pos])

front_pos++;

front_pos = 0;

while(front_pos <= rear_pos)

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");

printf("Enter your choice : ");

scanf("%d",&choice);

switch(choice)

case 1 :

printf("Input the element for insertion in queue : ");

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.

// A complete working C++ program to

// demonstrate deletion in singly

// linked list with class

#include <bits/stdc++.h>

using namespace std;

// A linked list node

class Node{

public:

int data;

Node* next;

};

// Given a reference (pointer to pointer)

// to the head of a list and an int,

// inserts a new node on the front of the

// list.

void push(Node** head_ref, int new_data){

Node* new_node = new Node();


new_node->data = new_data;

new_node->next = (*head_ref);

(*head_ref) = new_node;

// Given a reference (pointer to pointer)

// to the head of a list and a key, deletes

// the first occurrence of key in linked list

void deleteNode(Node** head_ref, int key){

// Store head node

Node* temp = *head_ref;

Node* prev = NULL;

// If head node itself holds

// the key to be deleted

if (temp != NULL && temp->data == key) {

*head_ref = temp->next; // Changed head

delete temp; // free old head

return;

// Else Search for the key to be deleted,

// keep track of the previous node as we

// need to change 'prev->next' */

else {

while (temp != NULL && temp->data != key) {

prev = temp;
temp = temp->next;

// If key was not present in linked list

if (temp == NULL)

return;

// Unlink the node from linked list

prev->next = temp->next;

// Free memory

delete temp;

}}

// This function prints contents of

// linked list starting from the

// given node

void printList(Node* node){

while (node != NULL){

cout << node->data << " ";

node = node->next;

}}

// Driver code

int main(){

// Start with the empty list

Node* head = NULL;

// Add elements in linked list

push(&head, 7);
push(&head, 1);

push(&head, 3);

push(&head, 2);

puts("Created Linked List: ");

printList(head);

deleteNode(&head, 1);

puts("\nLinked List after Deletion of 1: ");

printList(head);

return 0;

}
5. Write a program to perform preorder traversal in a binary tree.
#include <stdio.h>

#include <stdlib.h>

// Basic struct of Tree

struct node {

int val;

struct node *left, *right;

};

// Function to create a new Node

struct node* newNode(int item) {

struct node* temp = (struct node *)malloc(sizeof(struct node));

temp->val = item;

temp->left = temp->right = NULL;

return temp;

// Function print the node in preorder format, when insertion is complete

void preorder(struct node* root) {

if (root != NULL) {

printf("%d ", root->val);

preorder(root->left);

preorder(root->right);

}}

// Here we are finding where to insert the new node so BST is followed

struct node* insert(struct node* node, int val) {


/* If the tree(subtree) is empty, return a new node by calling newNode func.*/

if (node == NULL) return newNode(val);

/* Else, we will do recursion down the tree to further subtrees */

if (val < node->val)

node->left = insert(node->left, val);

else if (val > node->val)

node->right = insert(node->right, val);

/* (Safety) return the node's pointer which is unchanged */

return node;

int main() {

/* Our BST will look like this

200

/ \

120 280

/ \ / \

80 160 240 320 */

struct node* root = NULL;

root = insert(root, 200);

insert(root, 120);

insert(root, 80);

insert(root, 160);

insert(root, 280);

insert(root, 240);
insert(root, 320);

// Finally printing the tree using preorder

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,

return the new node to the calling function.

ii) if root=>data < key

call the insert function with root=>right and assign the return value in root=>right.

root->right = insert(root=>right,key)

iii) if root=>data > key

call the insert function with root->left and assign the return value in root=>left.

root=>left = insert(root=>left,key)

3. Finally, return the original root pointer to the calling function.

struct node *insert(struct node *root, int val){

/*

* It will handle two cases,

* 1. if the tree is empty, return new node in the root

* 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

*/

if(root->key < val)

root->right = insert(root->right,val);

/*

* if given val is smallar than root->key,

* we should find the correct place in the left subtree and insert the new node

*/

else if(root->key > val)

root->left = insert(root->left,val);

/*

* It will handle two cases

* (Prevent the duplicate nodes in the tree)

* 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>

using namespace std;

// Function to print an array

void printArray(int array[], int size) {

for (int i = 0; i < size; i++) {

cout << array[i] << " ";

}cout << endl;

void insertionSort(int array[], int size) {

for (int step = 1; step < size; step++) {

int key = array[step];

int j = step - 1;

// Compare key with each element on the left of it until an element smaller than

// it is found.

// For descending order, change key<array[j] to key>array[j].

while (key < array[j] && j >= 0) {

array[j + 1] = array[j];
--j;

} array[j + 1] = key;

}}

// Driver code

int main() {

int data[] = {9, 5, 1, 4, 3};

int size = sizeof(data) / sizeof(data[0]);

insertionSort(data, size);

cout << "Sorted array in ascending order:\n";

printArray(data, size);

b. Bubble sort:
#include<stdio.h>

void print(int a[], int n){

//function to print array elements

int i;

for(i = 0; i < n; i++) {

printf("%d ",a[i]);

}}

void bubble(int a[], int n){

// function to implement bubble sort

int i, j, temp;

for(i = 0; i < n; i++) {

for(j = i+1; j < n; j++) {


if(a[j] < a[i]) {

temp = a[i];

a[i] = a[j];

a[j] = temp;

}}}}

void main (){

int i, j,temp;

int a[5] = { 10, 35, 32, 13, 26};

int n = sizeof(a)/sizeof(a[0]);

printf("Before sorting array elements are - \n");

print(a, n);

bubble(a, n);

printf("\nAfter sorting array elements are - \n");

print(a, n);

}
8. Write a program to perform Quick sort on elements of array.
#include<stdio.h>

void quicksort(int number[25],int first,int last){

int i, j, pivot, temp;

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

printf("Enter some elements (Max. - 25): ");

scanf("%d",&count);

printf("Enter %d elements: ", count);

for(i=0;i<count;i++)

scanf("%d",&number[i]);

quicksort(number,0,count-1);

printf("The Sorted Order is: ");

for(i=0;i<count;i++)

printf(" %d",number[i]);

return 0;

You might also like