Cs212: Data Structures Computer Science Department Lab 7: Stacks.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Stacks. COMP104 Slide 2 Stacks * A stack, S, is a data structure that supports: n push(x) make x the top element in stack S n Pop Remove the top item.
Data Structures & Algorithms
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.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
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 CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
Objectives of these slides:
Chapter 7 Stacks II CS Data Structures I COSC 2006
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Chapter 7 Stacks Dr. Youssef Harrath
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
1 CS 132 Spring 2008 Chapter 7 Stacks Read p Problems 1-7.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Using C++1 Chapter 7 Stacks. Data Structures Using C++2 Chapter Objectives Learn about stacks Examine various stack operations Learn how.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
Data Structures Using Java1 Chapter 6 Stacks. Data Structures Using Java2 Chapter Objectives Learn about stacks Examine various stack operations Learn.
CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
STACK Data Structure
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Stacks Chapter 3 Objectives Upon completion you will be able to
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Stacks II David Lillis School of Computer Science and Informatics
CS505 Data Structures and Algorithms
Stacks and Queues Chapter 4.
Chapter 15 Lists Objectives
Homework 4 questions???.
Stacks.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
Stack.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Stacks: Implemented using Linked Lists
COMPUTER 2430 Object Oriented Programming and Data Structures I
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Stacks with Dynamic Memory
Jordi Cortadella and Jordi Petit Department of Computer Science
Stacks CS-240 Dick Steflik.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
LAB#3 Stacks Nora Albabtin nora albabtin.
Stacks.
Presentation transcript:

Cs212: Data Structures Computer Science Department Lab 7: Stacks

Stacks What is Stack ? A stack is a linear list in which all additions and deletions are restricted to one end, called the top. It is a Last in-First out (LIFO) data structure. In Stacks we DO NOT Search Add in arbitrary positions Sort Access anything beyond the top element. 28-Nov-18 Computer Science Department

Basic Stack Operation Remove data from the top of the stack. Push Place data on the top of the stack. Pop Remove data from the top of the stack. Stack Top Returns the top element of the stack. 28-Nov-18 Computer Science Department

Stack Linked List Implementation count top Head Data nodes Top (a) Conceptual (b) Physical 28-Nov-18 Computer Science Department

Stack LL Implementation (cont.) Java Node Class – same Node class in the Linked List Stack Class class Stack{ // Declare Stack class private Node top; // Reference to the first Node in the list private int count; // count the number of nodes in the list public Stack(){ // constructor count=0; // top by default initialized to null } public int StackCount(){ // return the count of nodes in the list return count;} public boolean emptyStack(){// check is it empty stack or not return (count==0); 28-Nov-18 Computer Science Department

Stack LL Implementation (cont.) Java Stack Class – pushStack method public void pushStack( int data){ Node NewNode = new Node(data); NewNode.next = top; top = NewNode; count ++; }

Stack LL Implementation (cont.) Java Stack Class – popStack method public boolean popStack( Node dataout){ if (!emptyStack()){ dataout.Data = top.Data; top = top.next; count --; return true; } else return false;

Stack LL Implementation (cont.) Java Stack Class – StackTop method public boolean StackTop( Node dataout){ if (!emptyStack()){ dataout.Data = top.Data; return true; } else return false;

Stack LL Implementation (cont.) C++ Node Class – same Node class in the Linked List Stack Class class Stack{ // Declare Stack class private : Node *top; // pointer to the first Node in the list int count; // count the number of nodes in the list public : Stack(){ // constructor count=0; top =NULL; } int StackCount(){ // return the count of nodes in the list return count;} bool emptyStack(){// check is it empty stack or not return (count==0);} 28-Nov-18 Computer Science Department

Stack LL Implementation (cont.) C++ Stack Class – pushStack function void pushStack( int data){ Node *NewNode = new Node(data); NewNode->next = top; top = NewNode; count ++; }

Stack LL Implementation (cont.) C++ Stack Class – popStack function bool popStack( int & dataout){ if (!emptyStack()){ Node *pLoc = top; dataout = top->Data; top = top->next; count --; delete pLoc; ;//freed memory return true; } else return false;

Stack LL Implementation (cont.) C++ Stack Class – StackTop function bool StackTop( int & dataout){ if (!emptyStack()){ dataout = top->Data; return true; } else return false;

Example 1 Write a program that read a list of integers from the user then print them in reverse. 28-Nov-18 Computer Science Department

Example 2 (C++) void ReverseList () { Stack s ; int number[15] , size ; int dataout; cout << "Enter the size of your list :"<<endl; cin >> size; cout << "Enter the element of the list: "<<endl; for ( int i = 0; i < size ; i++){ cin>>number[i]; s.pushStack(number[i]); } cout << endl << "The reverse list is: "<<endl; while ( ! s.emptyStack()) s.popStack(dataout); cout << dataout << " " ; 28-Nov-18 Computer Science Department

Example 2 ( Java ) public static void ReverseList () { Stack s = new Stack(); Node dataout=new Node(0); Scanner input = new Scanner (System.in); System.out.println( "Enter the size of your list :" ); int size = input.nextInt(); int number [] = new int [size]; System.out.println( "Enter the element of the list: " ); for ( int i = 0; i < size ; i++){ number[i]=input.nextInt(); s.pushStack(number[i]); } System.out.println( "The reverse list is: " ); while ( ! s.emptyStack()){ s.popStack(dataout); System.out.print(dataout.Data + " " ) ; 28-Nov-18 Computer Science Department

Example 2 Java & C++ PostFix expression 28-Nov-18 Computer Science Department