3. Stack
Stack is an abstract data type with a bounded(predefined) capacity. It is
a simple data structure that allows adding and removing elements in a
particular order. Every time an element is added, it goes on the top of
the stack, the only element that can be removed is the element that was
at the top of the stack.
4. Stack
Basic features of Stack
Stack is an ordered list of similar data type.
Stack is a LIFO structure. (Last in First out).
push() function is used to insert new elements into the Stack and pop()
is used to delete an element from the stack. Both insertion and deletion
are allowed at only one end of Stack called Top.
Stack is said to be in Overflow state when it is completely full and is
said to be in Underflow state if it is completely empty.
Applications of Stack
The simplest application of a stack is to reverse a word. You push a
given word to stack - letter by letter - and then pop letters from the
stack.
There are other uses also like : Parsing, Expression Conversion(Infix
to Postfix, Postfix to Prefix etc) and many more.
5. Stack Operation
Algorithm for Stacks:
Push operation.
If(TOS= Maxsize-1)
Then print Insertion is not possible
Else
Set TOS=TOS+1
Set S[Tos]=data
Exit
Pop Operation.
If (TOS==-1)
Then print deletion is not possible
Else
Data=S[TOS]
Set TOS=TOS-1
Exit
6. Linked List Representation of Stack:
#include<stdio.h>
#include<conio.h>
struct stu *new(int)
void addnode( );
void traverse( );
void delfirst();
struct stu
{
Int info;
Struct stu * next;
}
*head=NULL, *temp,*p;
Void main( )
{
int a, ch=1,choice, data;
while( ch)
{
Printf(“Enter the choice”);
Scanf(“%d”,&choice);
Switch(choice)
{
Case:1
Printf(“enter the data to be inserted”);
Scanf(“%d”,&a);
P=new(a);
addnode(p);
traverse();
break;
case 2:
delfirst( );
traverse();
break;
case 3:
exit( );
}
}
}
Struct stu *new ( int a)
{
p=((struct stu *) malloc (sizeof(struct
stu)));
p – info=a;
p – next=NULL;
return p;
}
Void addnode( )
{
Temp=head;
If (head==NULL)
{
Head=p;
}
Else
{
P –> next=temp;
Head=p;
}
Void traverse
{
Temp=head;
While(temp!=NULL)
{
Printf(“%d”, temp- info);
Temp=temp – next;
}
}
Void delfirst( )
{
Temp=head;
If(head==NULL)
{
Printf(“ Stack is empty”);
}
Else
{
Head=temp->next;
Free(temp);
}
}
7. Infix, Postfix and Prefix
Infix, Postfix and Prefix notations are three different but equivalent ways
of writing expressions. It is easiest to demonstrate the differences by
looking at examples of operators that take two operands.
Infix notation: X + Y
Postfix notation (also known as "Reverse Polish notation"): X Y +
Prefix notation (also known as "Polish notation"): + X Y
Converting between these notations
The most straightforward method is to start by inserting all the implicit
brackets that show the order of evaluation e.g.:
Infix Postfix Prefix
( (A * B) + (C / D) ) ( (A B *) (C D /) +) (+ (* A B) (/ C D) )
((A * (B + C) ) / D) ( (A (B C +) *) D /) (/ (* A (+ B C) ) D)
8. Queue Data Structures
Queue is also an abstract data type or a linear data structure, in which the first
element is inserted from one end called REAR(also called tail), and the
deletion of exisiting element takes place from the other end called as
FRONT(also called head). This makes queue as FIFO data structure, which
means that element inserted first will also be removed first.
The process to add an element into queue is called Enqueue and the process of
removal of an element from queue is called Dequeue.
9. Queue
Basic features of Queue
• Like Stack, Queue is also an ordered list of elements of similar
data types.
• Queue is a FIFO( First in First Out ) structure.
• Once a new element is inserted into the Queue, all the
elements inserted before the new element in the queue must be
removed, to remove the new element.
• peek( ) function is oftenly used to return the value of first
element without dequeuing it.
10. Implementation of queue
Initially the head(FRONT) and the tail(REAR) of the queue points at the first
index of the array (starting the index of array from 0). As we add elements to
the queue, the tail keeps on moving ahead, always pointing to the position
where the next element will be inserted, while the head remains at the first
index.
11. Queue
Queue is a linear data structure in which insertion and deletion is performed at two ends:
Rear end: Insertion takes place
Front end: Deletion takes place
Algorithms for Queue:
Insertion:
If ( front=0 && rear=size-1)
Then print insertion not possible
Elseif ( rear=-1 && front=-1)
Then set rear=0 and front=0
Else
Set rear=rear+1
Set Q[rear]=data
Exit
Deletion:
If(front=-1 or front>rear)
Then print deletion not possible
Set data=Q[font];
Front=front+1
Exit
12. Linked List Representation of Queue:
#include<stdio.h>
#include<conio.h>
struct stu *new(int)
void addnode( );
void traverse( );
void delfirst();
struct stu
{
Int info;
Struct stu * next;
}
*head=NULL, *temp,*p;
Void main( )
{
int a, ch=1,choice, data;
while( ch)
{
Printf(“Enter the choice”);
Scanf(“%d”,&choice);
Switch(choice)
{
Case:1
Printf(“enter the data to be inserted”);
Scanf(“%d”,&a);
P=new(a);
addnode(p);
traverse();
break;
case 2:
delfirst( );
traverse();
break;
case 3:
exit( );
}
}
}
Struct stu *new ( int a)
{
p=((struct stu *) malloc (sizeof(struct stu)));
p – info=a;
p – next=NULL;
return p;
}
Void addnode( )
{
Temp=head;
If (head==NULL)
{
Head=p;
}
Else
{
While(temp->next!=NULL)
{
Temp=temp->next;
}
Temp->next=p;
}
}
Void traverse
{
Temp=head;
While(temp!=NULL)
{
Printf(“%d”, temp- info);
Temp=temp – next;
}
}
Void delfirst( )
{
Temp=head;
If(head==NULL)
{
Printf(“ Stack is empty”);
}
Else
{
Head=temp->next;
Free(temp);
}
}
13. Circular Queue
when the queue becomes full or rear=maxsize-1, then no more data can be inserted.But, if there is space at the
front end after deleting the elements, then the space can be utilized using circular queues
In circular queue, the first index comes after the right index
Operations on Queues:
Insertion
If front=0 and rear=maxsize-1
Then print insertion not possible
Else if front=-1 and rear=-1
Then set front=0 and rear=0
Else if front!=0 and rear=maxsize-1
Then set rear=0
Else set rear=rear+1
Set Q[rear]=data
Exit
Deletion
If front=-1
Then print deletion not possible
Set data=Q[front];
If front=rear
Then set front =front+1
Else if front=maxsize-1
Then set front=0
Else set front=front+1
Exit
14. Program on Array Representation of Circular Queue
#include<stdio.h>
#include<conio.h>
int S[10];
int rear=-1,front=-1,size=10;
void insert();
int delete();
void traverse();
void main()
{
int ch=1,choice,data;
while(ch)
{
printf("Enter the choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be inserted");
scanf("%d",&data);
insert(data);
traverse();
break;
case 2:
data=delete();
printf("The element deleted is %d",data);
traverse();
break;
case 0:
exit();
}
}
}
void insert(int data)
{
if((front==0) && (rear==size-1))
{
printf("Insertion not possible");
}
else if((front==-1)&&(rear==-1))
{
rear=front=0;
}
else if((front!=0) && (rear==size-1))
{
rear=0;
}
else
{
rear=rear+1;
}
S[rear]=data;
}
int delete()
{
int data;
if(front==-1)
{
printf("deletion not possible");
}
data=S[front];
if(front==rear)
{
front=front+1;
}
else if(front==size-1)
{
front=0;
}
else
{
front=front+1;
}
return data;
}
void traverse()
{
int i;
for(i=front;i<=rear;i++)
{
printf("The list is %d",S[i]);
}
}
15. Dequeue
Dequeue
It is called Circular Double Ended Queue
Operations of insertion and deletion can be performed at both the ends
These are of two types:
Input Restricted Queue: Insertion is performed at one end while
deletion is performed at both ends
Output restricted Queue: Deletion is performed at single end while
insertion is performed at both ends
16. Algorithm for Deques:
Insert-Right
If front=0 and rear=masize-1 or front=rear+1
Then print insertion not possible
Else if front=-1
Then set front=0 and rear=0
Else if rear=maxsize-1
Then set rear=0
Else
Set rear=rear+1
Q[rear]=data
Exit
Insert-Left
If front =0 and rear=maxsize-1 or left=right+1
Then print insertion not possible
Else If front=-1
Then set front=0 and rear=0
Else if front=0 and rear!=maxsize-1
Then set front=maxsize-1
Else set front=front-1
Set data=Q[front]
Exit
Delete-Right
If right=-1
Then print deletion not possible
Data=Q[front]
If front=rear
Then set front=0 and rear=0
Else set rear=rear-1
Exit
Delete-left
If front= -1
Then print deletion not possible
Data=Q[front]
If front =rear=0
Then set front=-1 and rear=-1
Else
Set front=front +1
Exit
17. Priority Queue
Priority Queue
A priority queue is a queue in which each element is assigned a priority.
The priority of each element determines the order in which each
elements will be processed.
Rules for creating priority queue:
An element with higher priority is processed before an element with
lower priority
Two elements with the same priority are processed on First come- First
serve basis (FCFS)
18. Tower of Hanoi
It is one of the main applications of recursion
The problem is stated as:
• There are 3 disks mounted on a pole A. the problem is to move all
these rings from pole A to pole C using the intermediary pole B.
The rules which govern the problem are:
• Only one disk can be moved at one time
• The smaller disk must always come above the larger disk
Algorithm:
If n=1, move the ring directly from A to C
Else move n-1 rings from A to B
Move nth ring from A to C
Move n-1 rings from B to C