Академический Документы
Профессиональный Документы
Культура Документы
2: Stack
STACK
Introduction to linked list, stack, array implementation of stack, linked list implemenatation of stack,
coverting INFIX to POSTFIX notation, Evaluation of a POSTFIX expression
Introduction:
The term list means a linear collection of elements. Array is an example of linear lists. A
linked list is a collection of data elements, called nodes pointing to the next nodes by means of
pointers. Each node is divided into two parts DATA (info) and LINK (next). It is a dynamic data
structure as it can grow or shrink.
Stack:
It is a list in which insertion or deletion of an element is allowed only at one end. The rule
used for a stack is to always remove the item that has been in the collection the least amount of
time. This policy is known as last-in first-out or LIFO. The most accessible element denotes the top
and least accessible element denotes the bottom of the stack. An insertion in a stack is called
pushing and a deletion from a stack is called popping.
void main()
{int stack[size],item, top=-1, res;
char ch=‟y‟;
clrscr();
while(ch==‟y‟ || ch==‟Y‟)
{cout<<”\n Enter the ITEM for insertion:”;
cin>>item;
PUSH(stack,item,top);
cout<<”\n The stack is :\n”;
DISPLAY(stack,top);
cout<<”\n Want to insert more elements?
(y/n)”; cin>>ch;
}
cout<<”\n Deletion of elements:\n”;
ch=‟y‟;
while(ch==‟y‟ || ch==‟Y‟)
{
res=POP(stack,top);
cout<<”\n Deleted element is:”<< res;
cout<<”\n The stack is :\n”;
DISPLAY(stack,top);
cout<<”\n Want to delete more elements?
(y/n)”; cin>>ch;
}
getch();
}
Linked List Implementation of Stack:
A linked list is a dynamic data structure. So the space requirement is not predetermined.
The linked-stack is created after getting a node for the ITEM to be inserted. TOP points to the
newly inserted node. The dynamic allocation of memory is done by using new operator as shown
below:
Struct node
{ int info;
Node *next;
}*newptr;
Now, the following statement creates a node dynamically
TO
P
Kendriya
Sangathan
Vidyalaya
Popping also requires modification of TOP. Top is made to point to the next node in the sequence.
Deallocation of memory is done by the operate delete.
delete ptr;
Example: Convert the expression (TRUE && FALSE) || !(FALSE||TRUE) into postfix notation
from showing stack status after every step.
Answer:
Step Input Action Stack Status Output
1 ( Push (
2 TRUE Print ( TRUE
3 && Push (&& TRUE
4 FALSE Print (&& TRUE FALSE
5 ) Pop Empty TRUE FALSE &&
6 || Push || TRUE FALSE &&
7 ! Push || ! TRUE FALSE &&
8 ( Push || ! ( TRUE FALSE &&
9 FALSE Print || ! ( TRUE FALSE && FALSE
10 || Push || ! ( || TRUE FALSE && FALSE
11 TRUE Print || ! ( || TRUE FALSE && FALSE TRUE
12 ) Pop || ! TRUE FALSE && FALSE TRUE ||
13 Pop || TRUE FALSE && FALSE TRUE || !
14 Pop Empty TRUE FALSE && FALSE TRUE || ! ||
Remark: Here TRUE , FALSE are operands and !, ||, && are operators.
Initially the Stack is empty. Now, the first three characters scanned are 1,2 and 3, which are
operands. Thus they will be pushed into the stack in that order. Next character scanned is
"*", which is an operator. Thus, we pop the top two elements from the stack and perform
the "*" operation with the two operands. The second operand will be the first element that
is popped.
Expression
Expression
Stack
The value of the expression(1+6) that has been evaluated(7) is pushed into the stack.
Expression
Stack
Next character scanned is "4", which is added to the stack.
Expression
Stack
Next character scanned is "-", which is an operator. Thus, we pop the top two elements from the
stack and perform the "-" operation with the two operands. The second operand will be the first
element that is popped.
Expression
Stack
The value of the expression(7-4) that has been evaluated(3) is pushed into the stack.
Expression
Stack
Example: Evaluate the following postfix notation of expression (show status of stack after
execution of each operation).
TURE, FALSE, TRUE, FALSE, NOT, OR, TRUE, OR, OR, AND
Answer:
Step Input Action Stack Status
1 TRUE Push TRUE
2 FALSE Push TRUE, FALSE
3 TRUE Push TRUE, FALSE, TRUE
4 FALSE Push TRUE, FALSE, TRUE, FALSE
5 NOT Pop(False) TRUE, FALSE, TRUE
Push (Not FALSE=TRUE) TRUE, FALSE, TRUE, TRUE
6 OR Pop(TRUE) TRUE, FALSE, TRUE
Pop (TRUE) TRUE, FALSE
push (TRUE or TRUE=TRUE) TRUE, FALSE, TRUE
7 TRUE push TRUE, FALSE, TRUE, TRUE
8 OR pop (TRUE) TRUE, FALSE, TRUE
pop(TRUE) TRUE, FALSE,
push (TRUE or TRUE=TRUE) TRUE, FALSE, TRUE
9 OR pop (TRUE) TRUE, FALSE,
pop(FALSE) TRUE
push (TRUE or FALSE=TRUE) TRUE, TRUE
10 AND pop (TRUE) TRUE,
Introduction:
A queue is a subclass of lists in which insertion and deletion take place at specific
ends i.e. REAR and FRONT respectively. It is a FIFO (First In First Out) data structure. By
convention, we name the queue insert operation enqueue and the remove operation dequeue.
void queue::remove()
{
if (front != -1)
{
cout << data[front] << "
deleted "; if (front == rear)
front = rear - 1;
else
f
ront = }
else
cout << "Queue empty ! Underflow
error!!\n"; }
void main()
{ clrscr();
queue Q;
Q.add();
Q.remove();
}
switch (choice)
{
case 1:
do
{
cout << "Enter the value to be added in the
queue "; cin >> val;
rear = add_Q(rear, val);
if (front == NULL)
front = rear;
cout << "\nDo you want to add more element
<Y/N>? "; cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{
front = del_Q(front, val);
if (front == NULL)
rear = front;
if (val != -1)
cout << "Value deleted from Queue is " << val;
cout << "\nDo you want to delete more element
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Queue";
cout << "\n\t2. Deletion from Queue";
cout << "\n\t3. Traverse of Queue";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter Your choice from above ";
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter the character string : ";
gets(data);
add_Q(queue, front, data, rear);
cout << "Do you want to add more element <Y/N> ?
"; cin >> opt;
} while (toupper(opt) == 'Y');
break;