Stack C and Data Structures Baojian Hua

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue.
C and Data Structures Baojian Hua
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Data Structure Lecture-5
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structure & Abstract Data Type
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
C Module System C and Data Structures Baojian Hua
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Extensible Array C and Data Structures Baojian Hua
Exercise 6 : Stack 1.Stack is a data structure that supports LIFO (Last In First Out) operations. - In this exercise, you implement Stack data structures.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
1 Abstract Data Types (ADTs) Professor Jennifer Rexford COS 217.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Map, Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Queue C and Data Structures Baojian Hua
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks.
Binary Search Tree C and Data Structures Baojian Hua
Relation Discrete Mathematics and Its Applications Baojian Hua
Breath First Searching & Depth First Searching C and Data Structures Baojian Hua
String C and Data Structures Baojian Hua
Stack C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Hash Discrete Mathematics and Its Applications Baojian Hua
Queue C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Extensible Array C and Data Structures Baojian Hua
Linked List C and Data Structures Baojian Hua
Tree C and Data Structures Baojian Hua
Set, Map & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Functional List C and Data Structures Baojian Hua
String C and Data Structures Baojian Hua
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Graph Discrete Mathematics and Its Applications Baojian Hua
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Hash C and Data Structure Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
C ++ Programming Languages Omid Jafarinezhad Lecturer: Omid Jafarinezhad Fall 2013 Lecture 2 C ++ -overview Department of Computer Engineering 1.
Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks top push (8)push (2)
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.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –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.
Hash C and Data Structure Baojian Hua
Extensible Tree Discrete Mathematics and Its Applications Baojian Hua
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
STACK Data Structure
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
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 and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
Data Structures and Algorithms
Stack and Queue APURBO DATTA.
Some examples.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Discrete Mathematics and
Data Structures and Algorithms
UNIT-II.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Structures and List Processing
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Stack C and Data Structures Baojian Hua

Linear List Recall that a linear list takes this form: The delete and insert operations may operate on an arbitrary element e_i If the delete and insert operations are restricted at (any) one end, we get a stack

Example: Stack of Char ‘a’‘a’ insert ‘b’‘b’ ‘a’‘a’ ‘b’‘b’ ‘a’‘a’ ‘c’‘c’ delete ‘b’‘b’ ‘a’‘a’

Abstract Data Types in C: Interface // in file “stack.h” #ifndef STACK_H #define STACK_H typedef struct Stack_t *Stack_t; Stack_t Stack_new (); int Stack_size (Stack_t stk); int Stack_isEmpty (Stack_t stk); void Stack_push (Stack_t stk, poly x); poly Stack_pop (Stack_t stk); poly Stack_getTop (Stack_t stk); #endif

Implementation Using Extensible Array // in file “arrayStack.c” #include “array.h” struct Stack_t { Array_t l; }; // Recall the “box” strategy: l stk

Operations: “ new ” Stack_t Stack_new () { Stack_t stk = malloc (sizeof (*stk)); stk->l = ArrayList_new (); return stk; } 0 n-1 array max tail l stk

Operations: “ size ” int Stack_size (Stack_t stk) { return Array_length (stk->l); } 0 n-1 array max tail l stk

Operations: “ size ”, “ isEmpty ” int Stack_isEmpty (Stack_t stk) { return Array_isEmpty (stk->l); } 0 n-1 array max tail l stk

Operations: “ push ” void Stack_push (Stack_t stk, poly x) { Array_insertLast (stk->l, x); return; } 0 n-1 array max tail l stk

Operations: “ pop ” poly Stack_pop (Stack_t stk) { if (Array_isEmpty (stk->l)) error (“empty stack”); return Array_deleteLast (stk->l); } 0 n-1 array max tail l stk

Implementation Using Linked List // in file “linkedStack.c” #include “linkedList.h” struct Stack_t { List_t l; }; l stk

Operations: “ new ” Stack_t Stack_new () { Stack_t stk = malloc (sizeof (*stk)); stk->l = LinkedList_new (); return stk; } l stk /\

Operations: “ size ” int Stack_size (Stack_t stk) { return LinkedList_length (stk->l); } l stk data next data next data next …

Operations: “ isEmpty ” int Stack_isEmpty (Stack_t stk) { return LinkedList_isEmpty (stk->l); } l stk data next data next data next …

Operations: “ push ” void Stack_push (Stack_t stk, poly x) { // note the difference with extensible array LinkedList_insertFirst (stk->l, x); return; } l stk data next data next data next …

Operations: “ pop ” poly Stack_pop (Stack_t stk) { if (LinkedList_isEmpty (stk->l)) error (“empty stack”); return LinkedList_deleteFirst (stk->l); } l stk data next data next data next …