Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Stacks, Queues, and Linked Lists
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
1 Stack and Queue. 2 Stack In Out ABCCB Data structure with Last-In First-Out (LIFO) behavior.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO 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.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
Objectives of these slides:
DS.L.1 Lists, Stacks, and Queues (Review) Chapter 3 Overview Abstract Data Types Linked Lists, Headers, Circular Links Cursor (Array) Implementation Stacks.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Dale Roberts, Lecturer
Reference: Vinu V Das, Principles of Data Structures using C and C++
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
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.
CSC2100B Tutorial 2 List and stack implementation.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
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.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
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 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
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.
Queue and Tree in C Yang Zhengwei CSCI2100B Data Structures Tutorial 5 1.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
STACKS & QUEUES for CLASS XII ( C++).
Lists, Stacks and Queues in C
Chapter 12 – Data Structures
12 C Data Structures.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stack and Queue.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 16-2 Linked Structures
Review & Lab assignments
Stacks and Queues.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
CSCS-200 Data Structure and Algorithms
Presentation transcript:

Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4

Outline Structure Linked List – Overview – Implementation Stack – Overview – Implementation Queue – Overview – Implementation 2

Structure A collection of values (members) – Like a class in java or c++, but without methods and access controls (private, protected, public.) 3 struct time { int hh; int mm; int ss; }; … struct time t1; t1.hh = 20; t1.mm=12; t1.ss=30;

Structure We can also use pointer to structure Pointer to structure is very common. The above is equivalent to: struct time* t1; t1->hh = 20; // same as (*t1).hh=20; 4 struct time { int hh; int mm; int ss; }; struct time* t1; (*t1).hh=20;

Some words about typedef Allow us to define alias for a data type: – typedef int My_integer_type; – My_interger_type x =3; Tpedef can be used for structures: 5 typedef struct time { int hh; int mm; int ss; }Time_type; Time_type t1; t1.hh = 12;

Dynamic Memory Allocations We can allocate memory at run time using malloc – malloc can be used to allocate a piece of memory of the specified size, and returns a pointer to it. Example: Time_type *t1; t1 = (Time_type*)malloc(sizeof(Time_type)); – Allocate enough memory for storing a Time_type variable (which is a structure). – Return a pointer to it. – Cast it to a pointer to Time_type, and assign it to t1. 6

Dynamic Memory Allocations Use free to de-allocate the memory when it is no longer needed. This is important because there is no garbage collection in C. So you will run out of memory if you keep allocating without de-allocating. (“Memory Leak”) 7 Time_type *t1; t1 = (Time_type*)malloc(sizeof(Time_type)); … t1->hh = 12; … free(t1); //de-allocate when we no longer need it.

Linked List Overview A list of structures (nodes) Each structure contains – Element (to store data) – Pointer to next structure Insert() Delete() Print() 8

Linked List Implementation struct node_s { int data; struct node_s *next; }; typedef struct node_s node; //To create a node variable node anode; //static, allocate in compile time //or dynamic allocation node *anode = (node*)malloc(sizeof(node)); 9

Linked List Implementation Link two nodes together 10 node a,b; a.next = &b; b.next = NULL; //use pointer node* a, *b; a = (node*)malloc(sizeof(node)); b = (node*)malloc(sizeof(node)); b->next = NULL; a->next = b;

Linked List Implementation Insert a node to a list 11 node a,b,c; c.next = NULL; //originally, only a and c a.next = &c; //insert b between a and c b.next = &c; a.next = &b;

Linked List Implementation Delete a node from a list 12 node a,b,c; c.next = NULL; //orginial a.next = &b; b.next = &c; //remove b from the list a.next = &c;

13

Stack books on your table? 14

Stack Overview Last In First Out (LIFO) Push() Pop() Top() is_empty() 15

Stack Implementation Can be implemented by linked list or array Create an empty stack 16 node *create_stack() { node* emptyStack; emptyStack = (node*)malloc(sizeof(node)); emptyStack->next = NULL; return emptyStack; }

void Push(int inputdata, node *stack) { node* newnode = (node*)malloc(sizeof(node)); newnode->data = inputdata; newnode->next = stack->next; //should be first stack->next = newnode; //how about change the above 2 lines? } Stack Implementation Push an entry into the stack 17

int Pop(node* stack) { int temp; node* toBePopped; if(stack->next!=NULL) { temp = stack->next->data; toBePopped = stack->next; stack->next = stack->next->next; free(toBepopped); return temp; } else return 0; //error code, you can define according to the demand } Stack Implementation Pop an entry from the stack 18

Stack Implementation Return the top element in the stack 19 int top(node* stack) { if(stack->next!=NULL) return stack->next->data; else return 0; } //Determine if the stack is empty int is_empty(node *stack) { return (stack->next==NULL); }

If you are implementing a large project, usually we can separate the declaration and implementation to.h and.c files. 20

Stack Implementation 21

Stack Implementation using Array Implement a stack using array 22

Stack Implementation using Array createStack, makeEmpty 23

Stack Implementation using Array isEmpty, isFull 24

25

Queue ticket office of the Ocean park? 26

Queue Overview First In First Out (FIFO) Enqueue Dequeue 27

Queue Implementation A queue may be implemented using linked-list or array Implement a queue using array 28

Queue Implementation Implementing a queue using circular array 29

Queue Implementation createQueue 30

Queue Implementation enqueue 31

Queue Implementation dequeue 32

Queue Implementation isEmpty, isFull 33

Queue Implementation front, makeEmpty 34