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.

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.
Data Structure HKOI training /4/2010 So Pak Yeung.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
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 17 Linked List Saurav Karmakar Spring 2007.
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.
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
1 Abstract Data Types. Objectives To appreciate the concept and purpose of abstract data types, or ADTs To understand both the abstract behavior and the.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CS Data Structures II Review COSC 2006 April 14, 2017
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
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.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
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.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
EASTERN MEDITERRANEAN UNIVERSITY Stacks EENG212 –Algorithms and Data Structures.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
 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.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
STACKS & QUEUES for CLASS XII ( C++).
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
CC 215 Data Structures Queue ADT
Chapter 15 Lists Objectives
Data Structures and Algorithms
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Queue data structure.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Data Structures and Algorithms
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks and Queues.
Linked Lists.
CSCS-200 Data Structure and Algorithms
Presentation transcript:

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 how to do things using stacks, queues, lists etc

2 Stack

The Stack Metaphor A stack is a data structure in which the elements are accessible only in a last- in/first-out order. The fundamental operations on a stack are push, which adds a new value to the top of the stack, and pop, which removes and returns the top value. One of the most common metaphors for the stack concept is a spring-loaded storage tray for dishes. Adding a new dish to the stack pushes any previous dishes downward. Taking the top dish away allows the dishes to pop back up.

Implementation of Stack #define elements int #define stacksize 100 typedef struct { int top; elements items[stacksize]; } stack; 4

Implementation of Stack int empty ( stack s ); /* postcondition: empty(s) == 1 if s is empty, == 0 otherwise */ int full ( stack s ); /* postcondition: full(s) == 1 if s is full, == 0 otherwise */ elements pop ( stack *s ); /* precondition: not empty(*s); postcondition: push(*s, pop(*s)) == *s */ void push ( stack *s, elements e ); /* precondition: full(*s) == 0; postcondition: push(*s,e); pop(*s) == e */ 5

Implementation of Stack II #define elements int typedef struct { int top; int size; elements *items; } stack; 6

Implementation of Stack II stack create ( int n ); /* postcondition: create(n) is a stack which can hold n items */ int empty ( stack s ); /* postcondition: empty(s) == 1 if s is empty, == 0 otherwise */ int full ( stack s ); /* postcondition: full(s) == 1 if s is full, == 0 otherwise */ int pop ( stack *s, elements *e ); /* precondition: not empty(*s); postcondition: pop(*s,*e) == 0 if not empty(*s), == 1 otherwise, check = push(*s,*e) restores the stack */ 7

8 Queues and Lists

QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out (LIFO) discipline A queue adopts a first-in/first-out (FIFO) model that more closely resembles a waiting line. 9

Queue ADT abstract typedef > queue; /* a queue is a sequence of elements, FIFO = First In First Out */ abstract int empty ( queue q ); postcondition: empty(s) == 1 if queue is empty, == 0 otherwise; abstract int full ( queue q ); postcondition: full(s) == 1 if queue is full, == 0 otherwise; 10

Queue ADT 11 abstract elements delete ( queue q ); /* dequeue */ precondition: empty(q) == 0; postcondition: first element is removed from queue q; abstract void insert ( queue q, elements e ); /* enqueue */ precondition: full(q) == 0; postcondition: element e is added to the end of queue q;

Implementation of Queue 12 #define elements int typedef struct { int size,front,rear; elements *items; } queue;

Implementation of Queue FrontRear B C FrontRear C D E FrontRear C FrontRear

14 Linked List Group of nodes connected by pointers A node consists of Data Pointer to next node 6538 HeadNull

15 Declaration of a node struct node { int info; struct node *next; }; typedef struct node node; 6538 HeadNull

16 Linked List Structure 6538 Head 6538 Null Some address In memory Points back to Internal node

17 Dynamic allocation of a node node *ptr; ptr = (node *)malloc(sizeof(node)); ? ptr free(ptr)

18 Inserting at the Head 1. Allocate a new node 2. Insert new element 3. Make new node point to old head 4. Update head to point to new node 6538 HeadNull Head

19 Inserting at the Head node *inserthead(node *head, int a) { node *ptr; ptr = (node*)malloc(sizeof(node)); ptr->info = a; ptr->next = head; return(ptr); } 6538 Head Null Head head = inserthead(head,2);

20 Removing at the Head 1. Update head to point to next node in the list 2. Allow garbage collector to reclaim the former first node 6538 Head Null 538 Head Null

21 Inserting at the Tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null 4. Have old last node point to new node 6538 Head Null 6538 Head Null 1

22 Print a linked list void printlist (node *head) { while (head !=NULL) { printf("%d ",head->info); head = head->next; } printf("\n"); } 6538 HeadNull printlist(head);

23 Find length of a linked list int length(node *head) { if (head == NULL) return 0; else return 1 + length(head->next); } 6538 HeadNull x=length(head);

Linked List Implementation of Queue typedef struct { int size,front,rear; elements *items; } queue; HeadNull

Linked List Implementation of Queue struct node { elements info; struct node *next; }; typedef struct node node; front Null struct queue { node *front; node *rear; } ; typedef struct queue queue rear

Linked List Implementation of Queue front Null int empty ( queue q ); /* postcondition: empty(q) == 1 if queue q is empty, * == 0 otherwise; */ elements delete ( queue *q ); /* precondition: empty(*q) == 0; * postcondition: first element is removed from queue *q; */ void insert ( queue *q, elements e ); /* precondition: full(*q) == 0; * postcondition: element e is inserted to end of queue *q; */ rear

Linked List Implementation of Queue Null queue *q; q frontrear struct

28 Doubly Linked List Group of nodes connected by pointers A node consists of Data Pointer to next node Pointer to previous node 6538 Head Null

29 Doubly Linked List struct cnode { int info; struct cnode *next; struct cnode *previous; }; typedef struct cnode cnode; 6538 Head Null

Circular Lists Linked lists have some limitations Given a pointer p, we can not reach preceding nodes In circular lists, next field of last node points to first node A node consists of Data Pointer to next node 6538 HeadNull 6538 Head

Circular Lists Keep a pointer to the last node How can we add or remove an element from either the front or the rear of the list? How can we check if the list is empty? 6538 list First node Last node