- Click below to download Unit 4 programs in pdf file: -
Unit 4
/* 1. Write a program to implement Simple Queue operations
using Array and Linked List.
• ENQUEUE
• DEQUEUE
• Traversal (display)
*/
/** Using Array **/
#include<stdio.h>
#define max 3
int queue[max];
int front=-1;
int rear=-1;
void addque()
{
int val;
printf("enter the value :");
scanf("%d",&val);
if(rear==max-1)
{
printf("\nQueue is overflow.");
return;
}
rear++;
queue[rear]=val;
if(front==-1)
front++;
}
int delque()
{
int temp;
if(front==-1)
{
printf("\nQueue is underflow.");
return 0;
}
temp=queue[front];
if(front==rear)
{
front=rear=-1;
}
else
front++;
return temp;
}
void display()
{
for(int i=front;i<=rear;i++)
printf("%d ", queue[i]);
}
int main()
{
int ch,val;
do
{
printf("\n1.Add Queue:");
printf("\n2.Delete Queue:");
printf("\n3.Display Queue:");
printf("\n4.Exit:");
printf("\nEnter your choice:");
scanf("%d",&ch);
if(ch==1)
{
addque();
}
else if(ch==2)
{
printf("%d is removed element.",delque());
}
else if(ch==3)
{
display();
}
else if(ch==4)
{
printf("\nGood bye");
}
else
printf("\ninvalid input:");
}while(ch!=4);
}
/** Using Linked List **/
/*
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void addqueue()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value : ");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}
}
}
void dequeue()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}
void main ()
{
int choice;
while(choice != 4)
{
printf("\n1.Add Queue:");
printf("\n2.Delete Queue:");
printf("\n3.Display Queue:");
printf("\n4.Exit:");
printf("\nEnter your choice : ");
scanf("%d",& choice);
switch(choice)
{
case 1:
addqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("\nGood bye.....");
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
*/
/* 2. Write a program to implement Circular Queue operations
Using Array.
• ENQUEUE
• DQUEUE
• Traversal (display)
*/
#include<stdio.h>
#define MAX 3
int queue[MAX];
int front=-1;
int rear=-1;
void add(int val)
{
if((front == 0 && rear == MAX-1)||(rear+1==front))
{
printf("\nQueue Is Full.");
return;
}
if(rear == MAX-1)
rear=0;
else
rear++;
if(front == -1)
front++;
queue[rear]=val;
}
int del()
{
int tmp;
if(front == -1)
{
printf("\nQueue Is Empty.");
return 0;
}
tmp=queue[front];
if(front == rear)
front=rear=-1;
else if(front==MAX-1)
front=0;
else
front++;
return tmp;
}
void display()
{
int i=0;
while(i<=MAX-1)
{
printf("%d ",queue[i]);
i++;
}
}
int main()
{
int val,ch;
do{
printf("\n1.Add Queue:");
printf("\n2.Delete Queue:");
printf("\n3.Display Queue:");
printf("\n4.Exit:");
printf("\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter The Value.. ");
scanf("%d",&val);
add(val);
break;
case 2:val=del();
printf("| %d | Is Deleted value\n",val);
break;
case 3:printf("\nEnter Elements Are: ");
display();
break;
case 4:printf("\nGood Bye.........");
break;
default : printf("\nInvalid Choice.");
}
}while(ch != 4);
}
/* 3. Write a program to implement following operations on
Binary Search Tree using Linked List.
• Creation
• Insertion
• Traversal( In-order, Pre-order, Post-order)
*/
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
void inorderTraversal(struct node* root)
{
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
void preorderTraversal(struct node* root)
{
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
struct node* createNode(value)
{
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}
int main()
{
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
printf("Inorder traversal \n");
inorderTraversal(root);
printf("\nPreorder traversal \n");
preorderTraversal(root);
printf("\nPostorder traversal \n");
postorderTraversal(root);
}
0 Comments