Circular Queue Using Linked List
Circular Queue Using Linked List
CLL is a SLL where last node points to first node in the list
It does not contain null pointers like SLL
We can traverse the list in only one direction
Its advantage is that when we want to go from last node to first node, it directly points to
first node
Figure: CLL
CIRCULAR QUEUE USING LINKED LIST:
/* C Program to implement queue using circular linked list*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*rear=NULL;
int main()
{
int choice,item;
while(1)
{
printf("\n1.Insert\n");
printf("2.Delete\n");
printf("3.Peek\n");
printf("4.Display\n");
printf("5.Quit\n");
printf("\nEnter your choice : ");
scanf("%d",&choice);
DEPT OF INFORMATION TECHNOLOGY, MJCET XX
switch(choice)
{
case 1:
printf("\nEnter the element for insertion : ");
scanf("%d",&item);
insert(item);
break;
case 2:
printf("\nDeleted element is %d\n",del());
break;
case 3:
printf("\nItem at the front of queue is %d\n",peek());
break;
case 4:
display();
break;
case 5:
exit(1);
default:
printf("\nWrong choice\n");
}
}
}
if( isEmpty() )
{
rear=tmp;
tmp->link=rear;
}
else
{
tmp->link=rear->link;
rear->link=tmp;
rear=tmp;
}
del()
{
int item;
struct node *tmp;
if( isEmpty() )
{
printf("\nQueue underflow\n");
exit(1);
}
if(rear->link==rear)
{
tmp=rear;
rear=NULL;
}
else
{
tmp=rear->link;
rear->link=rear->link->link;
}
item=tmp->info;
free(tmp);
return item;
}
int peek()
{
if( isEmpty() )
{
printf("\nQueue underflow\n");
exit(1);
}
return rear->link->info;
}
int isEmpty()
{
if( rear == NULL )
return 1;
else
return 0;
}
void display()
{
OUTPUT:
VIVA QUESTIONS
1. What is data structure
2. How the memory is allocated dynamically
3. What is linked list
4. What is node
5. What are the types of linked list
Binary Trees: A binary tree is a special kind of tree in which each node can have at most two children:
they are distinguished as a left child and a right child. The subtree rooted at the left child of a node is
called its left subtree and the subtree rooted at the right child of a node is called its right subtree.
Level of a node refers to the distance of a node from the root. The level of the root is 1. Children of the
root are at level 2, ‖grandchildren‖ or children of the children of the root are at level 3, etc. The height
of a tree is the largest level of any node in the tree. In this case the term depth is used to indicate the
largest level.
Maximum number of nodes on a level i of a binary tree is 2i-1. Also the maximum number of nodes in a
binary tree of depth k is 2k – 1, k>0.
Tree traversal is the process of visiting each node in the tree exactly once. Visiting each node in a graph
should be done in a systematic manner. If search result in a visit to all the vertices, it is called a traversal.
There are basically three traversal techniques for a binary tree that are,
1. Preorder traversal
2. Inorder traversal
3. Postorder traversal
1) Preorder traversal
To traverse a binary tree in inorder traversal, following operations are carried out:
3) Postorder traversal
To traverse a binary tree in postorder traversal, following operations are carried out:
struct node {
int data;
struct node* left;
struct node* right;
};
return newNode;
}
int main(){
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
OUTPUT:
Figure 13: Binary Search Tree on Numbers Figure 14: Binary Search Tree on Strings
Searching a Binary Search Tree: Suppose we wish to search for an element with key x. We
being at root. If the root is 0, then the search tree contains no elements and the search terminates
unsuccessfully. Otherwise, we compare x with key in root. If x equals key in root, then search
terminates successfully. If x is less than key in root, then no element in right sub tree can have
key value x, and only left subtree is to be searched. If x is larger than key in root, the no element
in left subtree can have the key x, and only right subtree is to be searched. The subtrees can be
searched recursively.
Insertion into a Binary Search Tree: To insert an element x, we must first verify that its key is
different from those of existing elements. To do this, a search is carried out. If search is
unsuccessful, then element is inserted at point where the search terminated.
Figure 15: Deleting a leaf node Figure 16: Deleting a non leaf node.
struct node
{
int data;
struct node *right_child;
struct node *left_child;
};
return p;
}
if(root==NULL)
return new_node(x);
else if(x>root->data)
root->right_child = insert(root->right_child, x);
else
root->left_child = insert(root->left_child,x);
return root;
}
if(root==NULL)
return NULL;
if (x>root->data)
root->right_child = delete(root->right_child, x);
else if(x<root->data)
root->left_child = delete(root->left_child, x);
else
{
else
{
struct node *temp = find_minimum(root->right_child);
root->data = temp->data;
root->right_child = delete(root->right_child, temp->data);
}
}
return root;
}
int main()
{
/*
20
/ \
/ \
5 30
/ \ /\
/ \ / \
1 15 25 40
/ \
/ \
9 45
inorder(root);
printf("\n");
return 0;
}
OUTPUT: