0% found this document useful (0 votes)
18 views10 pages

Data Structure

Uploaded by

murshida.hzb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

Data Structure

Uploaded by

murshida.hzb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DATA STRUCTURE: STACK

Data structure is a way of storing organizing information in the computer.


NEED OF DATA STRUCTURE
1. Specific data structure are essential ingredients of many efficient
algorithms and make possible the management of huge amount of data,
such as a large integrated collection of database.
TYPES OF DATA STRUCTURE
1. PRIMITIVE DATA STRUCTURE:
Data structures that are directly operated by machine level instructions
are known as primitive data structures.

Eg: integer, real, character, pointer and reference

2. NON PRIMITIVE DATA STRUCTURE:


Data structure that are derived from primitive data structures. It
contains formation of set of homogeneous and heterogeneous data
elements.
Non primitive data structure is divided into 2 parts:
 Linear data structure These are single level data structure having
elements in sequence.
Eg: array, stack, queue and linked list

A B C D E F G H I
I= 0 1 2 3 4 5 6 7 8

 Non linear data structure These are multilevel data structure


Eg: tree and graph
A

B C

D E F
Fig: binary tree

OPERATION ON DATA STRUCTURE


1. INSERTION:
It means addition of a new data elements
2. DELETION:
It means removal of element
3. SEARCHING:
It means search of specific elements
4. TRAVERSAL:
It means processing all the data elements
5. SORTING:
Arranging data elements in ascending or descending order
6. MERGING:
Combining elements of two similar data structure to form
new data structure
STACK
A stack is a list where insertion and deletion take place only at one
end called top.
It works on the principal of Last In First Out (LIFO).

Eg:
40  TOP
30
20
10
1

Some example of stack :


a. Stack of coins
b. Plates placed above one another
c. Pile of books
d. Pile of cloths
e. Vertical Pile of chairs
BASIC OPERATIONS AND IMPLEMENTATION OF STACK
1. PUSH OPERATION:
It means adding/ inserting elements from the top

Eg:
C D TOP
TOP 
PUSH C
B B
D A
A

STACK BEFORE STACK AFTER

ALGORITHM FOR PUSH


ITEMS  INSERTING ELEMENTS
MAX TOP ELEMENT OF THE STACK
TOP TOPMOST ELEMENT OF STACK
if top == (max -1):
“overflow”
else:
top=top+1
data [top] = item
stop

Code:

def push(list,element):
list.append(element)
top=len(list)-1

2. POP OPERATION
To remove an element from the top of the stack

Eg: 15 12 10 5

TOP  15
12 TOP  12
10 10 TOP  10
5 5 5 TOP  5

STACK INITIALLY POP 15 POP 12 POP 10 POP 5

ALGORITHM FOR PUSH


if top== -1:
“stack is empty”
else:
item=data [top]
top=top-1
CODE:
def pop(stack_pointer):
if stack_pointer== -1
“underflow”
else:
print(“poped element:”,list.pop())
stack_pointer = stack_pointer -1
print(“stack pointer:”, stack_pointer)

3. PEEK
It inspect the element of list without removing it.

4. OVERFLOW
Attempt to insert new element in already full stack.

5. UNDERFLOW
Attempt to delete an element from already empty stack.

CODE IMPLIMENTATION

def empty(stack):
if stack==[ ]:
return True
else:
return False
def push(stack,element):
stack.append(element)
top=len(stack) -1
def pop(stack):
if empty(stack):
return “underflow”
else:
element=stack.pop()
if len(stack)= = 0:
top=None
else:
top=len(stack) -1
return element
def display(stack):
if empty(stack):
print(“stack is empty”)
else:
top=len(stack)
for i in range(top-1.-1.-1):
print(stack[i])
#main program----------------------------------------
stk = [ ]
top=None
a=”y”
while a= =”y” or a= = “Y”:
print(“1.PUSH”)
print(“1.POP”)
print(“1.DISPLAY”)
print(“1.EXIT”)
choice= input(“Enter Your Choice:\n”)
if choice= =1:
element=int(input(“Enter the Element”))
push(stk,element)
elif choice= = 2:
element=pop(stk)
if element= = “underflow”:
print(“stack is Empty”)
else:
print(“poped element is:\n”,element)
elif choice= =3:
print(“stack\n”)
show(stk)
elif choice= =4:
break
else:
print(“wrong input”)

OPERATION ON STACKS
There are 3 different operations
1. INFIX: operators are written in between the operands
Eg:
A*(B+C)/D
2. PREFIX: operators are written before the operands
Eg:
/*A+BCD
3. POSTFIX: operators are written in between the operands
Eg:
ABC+*D/
ALGORITHM TO CONVERT INFIX TO POSTFIX
1. Enclose the expression in parenthesis
2. Read next symbol of expression
3. If the symbol is operand then add it to output
4. If the symbol is “(“ then push in the stack
5. If the symbol is operator then:
i) Repeat while (priority of TOP >= priority of operator) pop
operator from stack and add operator to output
ii) Push operator into output
6. If symbol is “)”then
i) Repeat while (TOP (stack)!=’(‘ ) POP from stack and add
to operator.
ii) Remove the “(“.
Eg:
Evaluate the postfix expression and show the contents of the
stack after execution of each operation
( ( true && false)|| !(false || true))
scan Stack Output
( (
( ((
True (( True
&& ((&& True
False ((&& True false
) ( True false &&
|| (|| True false &&
! (||! True false &&
( (||!( True false &&
False (||!( True false && false
|| (||!(|| True false && false
True (||!(|| True false && false true
) (||! True false && false true||
) Empty stack True false && false true||!||

Output: True false && false true||!||


Evaluate the postfix expression and show the contents of the stack
after execution of each operation
50, 40, +, 18, 14, -, 4, *, +
scan Stack Output
50 50
40 50,40
+ 90 50+40=90
18 90,18
14 90, 18, 14
- 90,4 18-14=4
4 90, 4, 4
* 90, 16 4*4=16
+ 106 90+16=106

OUTPUT: 106

You might also like