1 CS 201 Dynamic Data Structures (2) Debzani Deb.

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.
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
CSCI2100B Linked List Jeffrey
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
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.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Linked Lists. Outline Why linked lists? Linked lists basics Implementation Basic primitives ­Searching ­Inserting ­Deleting.
C Programming : Elementary Data Structures 2009/04/22 Jaemin
1 CS 201 Dynamic Data Structures (3) Debzani Deb.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
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 CS 201 Dynamic Data Structures and Function Pointers Debzani Deb.
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.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
1 CS 201 Dynamic Data Structures Debzani Deb. 2 Run time memory layout When a program is loaded into memory, it is organized into four areas of memory.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Summary of lectures (1 to 11)
Chapter 14 Dynamic Data Structures Instructor: Alkar & Demirer.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
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.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Introduction to Data Structures Systems Programming.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
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’
CSC2100B Tutorial 2 List and stack implementation.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Data Structures: Advanced Damian Gordon. Advanced Data Structure We’ll look at: – Linked Lists – Trees – Stacks – Queues.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
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.
Stacks and Queues. Announcements USACO Open competition results are out o Congrats to Johnny for scoring 2nd in the US USACO Finalists are also announced.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
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.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
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.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
 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.
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++).
Pointers and Linked Lists
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
UNIT – I Linked Lists.
Data Structure Interview Question and Answers
Data Structures and Algorithms
Stack and Queue APURBO DATTA.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Review & Lab assignments
Data Structures and Algorithms
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Stacks and Queues Prof. Michael Tsai 2017/02/21.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Presentation transcript:

1 CS 201 Dynamic Data Structures (2) Debzani Deb

2 Overview Linked list basics List Searching Insertion Deletion Stack Queue

3 Linked List basics A linked list is a sequence of nodes in which each node but the last contains the address of the next node. typedef struct list_node_s { int digit; struct list_node_s *restp; } list_node_t; list_node_t *n1_p, *n2_p; n1_p = (list_node_t *) malloc (sizeof(list_node_t)); n2_p = (list_node_t *) malloc (sizeof(list_node_t)); n1_p -> digit = 5; n2_p -> digit = 7; n1_p -> restp = n2_p; n2_p -> restp = NULL; 5 n1_p digit restp 7 n2_p digit restp X

4 Link List Operation (Searching) Find First occurrence of target in the list. 1.What if I put cur_nodep++ instead of cur_nodep -> restp ? Could that work? When? 2.What if the order of the following tests are reversed? (cur_nodep != NULL) && (cur_nodep -> digit != target)

5 Link List Operation (Insertion at Head) Insert at list’s head (i.e. at the front of the list) list_node_t * insertH (list_node_t *pHead, int v) { list_node_t *newp; newp = (list_node_t *) malloc(sizeof(list_node_t)); newp->digit = v; newp->restp = pHead; return newp; /* return pointer to the new head of the list */ } typedef struct list_node_s { int digit; struct list_node_s *restp; } list_node_t; int main(void){ list_node_t * pHead = NULL; pHead = insertH(pHead, 3); pHead = insertH(pHead, 5); pHead = insertH(pHead, 7); pHead = insertH(pHead, 9); }

6 Link List Operation (Deletion at Head) After the insertion at last slide, your list now looks like 9 pHead digitrestp 7 digitrestp 5 digitrestp 3 digit X restp list_node_t * deleteH (list_node_t * pHead){ list_node_t *newp = pHead -> restp; /* newp is now pointing to the 2nd element of the list */ free(pHead); return newp; /* return pointer to the new head of the list */ } 7 pHead digitrestp 5 digitrestp 3 digitrestp After the following call pHead = deleteH (pHead);

7 Lists are arrays Quite often the lists are treated as arrays, that can change their size dynamically. data *next a data *next b data *next c data NULL d Assumptions  Indexing of list starts at 0 (as in arrays).  Every index value is unique.  Indices are in growing order (incremented by 1). Index

8 Link List Operation (Deletion at Index) Delete element at some index of the list list_node_t * deleteIndex (list_node_t * pHead, int index) { int i; list_node_t * newp, *p, * tmpp; if (index == 0) { // deleting head newp = pHead -> restp; free(pHead); return newp; }

9 Link List Operation (Deletion at Index) else { // deleting element other than the head for (p = pHead, i = 1; (i restp != NULL); i++) /* searching for the element that has a pointer to the one to be deleted */ p = p -> restp; tmpp = p -> restp; /* tmpp now points at the element to be deleted */ if(tmpp != NULL) p -> restp = tmpp -> restp; /* p now points at the element after the one to be deleted */ else /* we are dealing with the last element of the list */ p -> restp = NULL; free(tmpp); return pHead; }

10 Representing a stack with a linked list Like a push down stack of books. Push (insert) at top (pointed by Head) Pop (remove) from the top (pointed by Head) Last in first out (LIFO) architecture. restp 7 Head restp 5 3 X Push Pop

11 Push on Stack typedef struct list_node_s { int digit; struct list_node_s *restp; } list_node_t; Push on top of the stack list_node_t * push (list_node_t * sHead, int v) { list_node_t * p = (list_node_t *) malloc(sizeof(list_node_t)); p -> digit = v; p -> restp = sHead; return p; } Pointer to the current head of the stack Return pointer to the new head of the stack Return the new node as the head of the stack int main(void) { list_node_t * sHead = NULL; /* Function call for push*/ sHead = push(sHead, 3); sHead = push(sHead, 5); return 0; }

12 Pop from Stack Pop from the top of the stack list_node_t * pop (list_node_t * sHead, int * v) { list_node_t * p; *v = sHead ->digit; p = sHead -> restp; free (sHead); return p; } Pointer to the current head of the stack Return pointer to the new head of the stack Return the new node as the head of the stack int main(void) { list_node_t * sHead = NULL; int val; sHead = push(sHead, 3); sHead = push(sHead, 5); /* Function call for pop */ sHead = pop (sHead, &val); printf(“Popped : %d”, val); return 0; } Popped digit as output parameter

13 Stack (Summary) In stack only top element can be accessed. You could make a stack with an array.  Linked list is just one way. Common design for function invocation. Both push and pop are constant time operations on stack.

14 Representing a Queue with a linked list Like a queue of people waiting Push at the Head (i.e at the end of the list). Pop from the Bottom (i.e from the front of the list) First In First Out (FIFO) restp 7 Head restp 5 3 X Front End Push Pop

15 Push on Queue typedef struct list_node_s { int digit; struct list_node_s *restp; } list_node_t; Push is same as stack (at Head) list_node_t * push (list_node_t * qHead, int v) { list_node_t * p = (list_node_t *) malloc(sizeof(list_node_t)); p -> digit = v; p -> restp = qHead; return p; } Pointer to the current head of the queue Return pointer to the new head of the queue Return the new node as the head of the queue int main(void) { list_node_t * qHead = NULL; /* Function call for push*/ qHead = push(qHead, 3); qHead = push(qHead, 5); return 0; }

16 Pop from Queue (from the bottom) list_node_t * pop (list_node_t * qHead, int * v) { list_node_t * qEnd, * qFront = NULL; if (qHead -> restp = NULL) { // Queue has only one element *v = qHead ->digit; free (qHead); return NULL; } for (qEnd = qHead; qEnd ->restp != NULL; qEnd = qEnd -> restp) qFront = qEnd; *v = qEnd -> digit; qFront -> restp = NULL; free(qEnd); return qHead; } Can we write this more efficiently?

17 Queue (Summary) You could implement it as an array too. You could make a hybrid of stack/queue to access at either end. Common design for process scheduling, event processing, buffering, input/output etc. In our design push is constant time, but pop is O(n) linear time (where n is the number of elements in the queue). If we record two pointers (front and end) instead of only one pointer pointing to the head of the list – both push and pop would have constant time.  See the implementation in your textbook.

18 Circular and double linked list Circular linked list pHead -> restp -> restp -> restp -> restp = pHead; Double linked list struct dblLink { int digit; struct dblLink * pNext, pPrev; } 9 pHead digitrestp 7 digitrestp 5 digitrestp 3 digitrestp 9 pHead digitpNext X pPrev 7 digitpNextpPrev 5 digit X pNextpPrev

19 Important ! Lab 6 grades are posted. Remember Exam 2 next Wednesday. Covers  Array  String  Recursion  Structure & Union  Dynamic data storage (upto queue) All rules of exam 1 will be applicable.