Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
M180: Data Structures & Algorithms in Java
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
Data Structures: A Pseudocode Approach with C
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Summary of lectures (1 to 11)
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Data Structures & Algorithms
Linked Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
CHP-3 STACKS.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Linked Lists A formal data structure. Linked Lists Collections of data items “lined up in a row” Inserts and deletes can be done anywhere in the list.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
STACKS & QUEUES for CLASS XII ( C++).
Data Structures Using C++ 2E
C++ Programming:. Program Design Including
Data Structures Using C, 2e
Queues.
Stacks and Queues Chapter 4.
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
MEMORY REPRESENTATION OF STACKS
Stacks.
Linked lists.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stack and Queue APURBO DATTA.
Queues Mohammad Asad Abbasi Lecture 5
Pointers and Linked Lists
Stacks.
DATA STRUCTURE QUEUE.
UNIT-I Topics to be covere d 1.Introduction to data structures.
EENG 212 ALGORITHMS And DATA STRUCTURES
Abstract Data Type Abstract Data Type as a design tool
Stacks Data structure Elements added, removed from one end only
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Linked lists.
LINEAR DATA STRUCTURES
Presentation transcript:

Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out) or LCFS(Last Come First Served)

A stack is referenced via a pointer to the top element of the stack referred as top pointer. The top pointer keeps track of the top element in the stack. Initially, when the stack is empty, the top pointer has a value zero and when the stack contains a singly element, the top pointer has a value one and so on.

Implementation of Stack Array implementation of stack: Stacks can be represented using arrays when the size is nominal and elements are predefined Top Max stack A Pointer called Top which contains the location of top element. The variable Max stack gives the maximum number of elements held by the stack

Array Implementation of PUSH Operation Process of adding a new element. Each time top pointer is incremented. Top=-1 Top=0 Top= 1 Top=2 Stack is empty Insert element 20 Insert element 34 Insert element top

Algorithm StackPUSH(stack, maxsize) Step 1:[check for stack overflow] If Top>maxsize-1 Then print ”stack is full: and else Step 2:[Increment Top] set Top=Top+1 Step 3:[Insert new element in Top position] set stack[Top]=element END StackPUSH

Pseudocode Void push() { int element; if(Top==maxsize-1) { printf(“the stack is full”); getch(); exit(0); } else { printf(“Enter the element to be inserted”); scanf(“%d”,&element); Top=Top+1; stack[Top]=element; }

Array Implementation of POP Operation Process of deleting an existing element from the top of the stack. Each time top pointer is decremented. Top=2 Top=1 Top=0 Top=-1 Initial stack After deletion of 50 After deletion of 34 After deletion of 20 Stack empty top

Algorithm StackPop(stack) Step 1:[check for stack underflow] If Top<0 Then print “stack is empty” and exit else remove the item from top Step 2:[decrement Top] set Top=Top-1 Step 3:[delete an element in Top position] set item=stack[Top] END StackPop

Psuedocode int Pop() { int lement; if(Top==-1) { printf(“The stack is empty”); getch(); exit(0); } else { element=stack[Top]; Top=Top-1; } return(element); }

Linked List Implementation of stack Stacks can be represented using linked list to overcome the drawbacks of array that are memory wastage and shortage. Top Linked List implementation of stack provides us convenient and flexibility to increase or decrease stack size as necessary during execution. Only Drawback is extra memory to store the address of links. A * B * CNULL

Linked List implementation of Push Operation While inserting a new element into the linked stack, first we need to create a new stack node and then store new element into the data field of the new node. Top Stack empty Push element 20 Push element 13 NULL 20NULL NULL 1000

Algorithm linkedstackpush(element) Step 1:[allocate memory for new stack node] node=(stack*)malloc(sizeof(stack) if(Top==NULL) stack empty and insert newnode as only node Top==newnode else insert node at top of stack Step 2:[set node data] newnode->element=element

Step 3:[insert node into as top of stack] current Top=Top current Top->Link=newnode Top=newnode END linkedstackpush;

Pseudocode void pushstacklinked() { stack *node; int data; node=(stack*)malloc(sizeof(stack)) printf(“enter the elements”); Scanf(“%d”,&data); Newnode->element=data; currentTop=Top; currentTop->Link=newnode; Top=newnode; }

Linked list implementation of Pop Operation For deleting an element from linked stack, first we need to make sure that the stack is not empty. For this first Verify the Top pointer. Top Pop element 13 Pop element 20 stack empty NULL NULL 2050 NULL

Algorithm linkedstackpop(element) Step 1:[check whether stack is empty] if(Top==NULL) Then print ”stack is empty” stop else Step 2:[retrieve node value] data=node->element Step 3:[delete node] Top=node->link deallocate memory END stackpush;

Pseudocode void popstacklinked() { stack *node; int data; if(top==NULL) { printf(“stack is empty and deletion cannot be done”); getch(); exit(); } Else { node=Top; data=node->element; Top=node->link; free(node); } retrun(data); }

Thank u