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

Stacks, Queues, and Linked Lists
C and Data Structures Baojian Hua
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
The Stack Data Structure. Classic structure What is a Stack? An abstract data type in which accesses are made at only one end Last In First Out (LIFO)
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.
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
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.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
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
Stack C and Data Structures Baojian Hua
Dynamically Extensible Data Structures Discrete Mathematics and Its Applications Baojian Hua
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
Graph C and Data Structures Baojian Hua
C and Data Structures Baojian Hua
Graph C and Data Structures Baojian Hua
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
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
Graph Traversal Discrete Mathematics and Its Applications Baojian Hua
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
Set & Bit-Vector Discrete Mathematics and Its Applications Baojian Hua
Graph Discrete Mathematics and Its Applications Baojian Hua
Hash C and Data Structure Baojian Hua
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.
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Binary Search Tree C and Data Structures Baojian Hua
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Induction & Recursion Discrete Mathematics and Its Applications Baojian Hua
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Hash C and Data Structure Baojian Hua
Polymorphism Discrete Mathematics and Its Applications Baojian Hua
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
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
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington Last updated: 2/17/
Data Structures and Algorithms
Abstract Data Types and Stacks
CSC215 Homework Homework 11 Due date: Dec 19, 2016.
Discrete Mathematics and
Data Structures and Algorithms
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
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
Abstract Data Types and Stacks
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 stackStruct *stack; stack newStack (); int size (stack stk); int isEmpty (stack stk); void push (stack stk, poly x); poly pop (stack stk); poly getTop (stack stk); #endif

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

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

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

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

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

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

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

Operations: “ new ” stack newStack () { stack stk = (stack)malloc (sizeof (*stk)); stk->l = newLinkedList (); return stk; } l stk /\

Operations: “ size ” int size (stack stk) { return linkedListLength (stk->l); } l stk data next data next data next …

Operations: “ isEmpty ” int isEmpty (stack stk) { return linkedListIsEmpty (stk->l); } l stk data next data next data next …

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

Operations: “ pop ” poly pop (stack stk) { if (linkedListIsEmpty (stk->l)) error (“empty stack”); return linkedListDeleteFirst (stk->l); } l stk data next data next data next …