CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.

Slides:



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

Linked Lists Geletaw S..
Lists CS 3358.
COMP171 Fall 2005 Lists.
Stacks, Queues, and Linked Lists
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Data Structures ADT List
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.
CSCI2100B Linked List Jeffrey
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
CS 201 Data Structures and Algorithms Chapter 4: Trees (BST) Text: Read Weiss, §4.3 1Izmir University of Economics.
1 Chapter 6 Priority Queues (Heaps) General ideas of priority queues (Insert & DeleteMin) Efficient implementation of priority queue Uses of priority queues.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Lists, Stacks, and Queues Abstract Data Types (ADTs) The List ADT The Stack ADT The Queue ADT.
Data Structures and Algorithm Analysis Lecturer: Jing Liu Homepage:
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Linked List by Chapter 5 Linked List by
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.
CE 221 Data Structures and Algorithms
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Data Structure and Algorithm Analysis 03: Lists, Stacks and Queues Hongfei Yan School of EECS, Peking University.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Unit – I Lists.
Cpt S 122 – Data Structures Abstract Data Types
CS 201 Data Structures and Algorithms
Lectures linked lists Chapter 6 of textbook
CE 221 Data Structures and Algorithms
Lists CS 3358.
Chapter 4 Linked Lists.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
LINKED LISTS CSCD Linked Lists.
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Lists CSE 373 Data Structures.
Lists CSE 373 Data Structures.
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Chapter 3 Lists, Stacks, and Queues
Presentation transcript:

CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics

Introduction We will discuss three of the most simple and basic data structures. Virtually every significant program will use at least one of these structures explicitly, and a stack is always implicitly used in a program, whether or not you declare one. Izmir University of Economics2

Abstract Data Types (ADTs) An abstract data type (ADT) is a set of objects. ADTs are mathematical abstractions; implementation is not considered. Objects such as lists, sets, and graphs along with their operations can be viewed as abstract data types just as integers, reals, and booleans are data types. There is no rule telling which operations must be supported for each ADT; this is a design issue. Izmir University of Economics3

The List ADT - I A general list of the form: A 0, A 1, A 2,..., A N-1 The size of this list is N. Special list of size 0 is an empty list. For any list except empty list, A i follows or succeeds A i-1 (i 0). The predecessor of A 0 and the successor of A N-1 are not defined. First element of the list is A 0 and the last element is A N-1. The position of element A i is i. Izmir University of Economics4

The List ADT - II Associated with these definitions are some popular operations on the list ADT: printList, makeEmpty, find (returns the position of the first occurence of an item), insert and remove ( generally insert and remove from some position), findKth (returns the element in some position). Example: 34, 12, 52, 16, 12. find (52)  2; insert (x, 2)  34, 12, x, 52, 16, 12. Izmir University of Economics5

Simple Array Implementation of Lists Use arrays for the underlying implementation. Arrays are created with a fixed capacity; grow it when needed by doubling its capacity. With this implementation mechanism; printList takes O(N) time; findKth takes O(1) time. Insertions and deletions are expensive. In the worst case, inserting into position 0 (at the front of the list) requires pushing the entire array down one spot to make room, and deleting the first requires shifting all up by one: O(N). On the average half the list is moved: still O(N). Best case occurs when they are performed at the higher end: O(1). Izmir University of Economics6

Simple Linked Lists - I In order to avoid the linear cost of insertion and deletion, we need to ensure that the list is not stored contiguously, since otherwise entire parts of the list will need to be moved. The linked list consists of a series of nodes. Each node contains the element and a link (next) to the successor node. To execute printList() or find(x), start at the first node and then traverse the list by following the next links: O(N) findKth(i) takes O(i) (but you can sort the calls by i) Izmir University of Economics7

Simple Linked Lists - II The remove operation can be executed in one next pointer change. The insert command requires obtaining a new node from the system by using a malloc call (more on this later) and then executing two pointer maneuvers. Izmir University of Economics8

If we know where a change is to be made, inserting or removing an item from a linked list involves only a constant number of changes to node links. The special case of adding to the front or removing the first item is thus a constant-time operation if there exists a link to the front of the list. The same holds when adding at the end as long as another link is provided to the last node. Removing the last item is trickier! Why? We need to find the next-to-last item and change its next link to NULL and then update the pointer to the last node. A third link to next-to-last node does not work (it too needs to be updated.) Solution: each node maintain a link to its previous node: Doubly linked list Izmir University of Economics9 Simple Linked Lists - III

The List will,this time, be implemented as a doubly linked list. We will also maintain pointers to both ends of the list. The two extra nodes created at the front and at the back of the list are header node and tail node respectively (sentinels nodes). Linked List Implementation - I 10Izmir University of Economics

11 Linked List Implementation - II typedef int ElemType; struct Node { ElemType data; Node * prev; Node * next; }; void initializeDLL(List *l) { l->theSize = 0; /* assume l is not NULL */ l->head = (struct Node *)malloc(sizeof(struct Node)); l->tail = (struct Node *)malloc(sizeof(struct Node)); l->head->next = l->tail; l->tail->prev = l->head; l->head->prev = NULL; l->tail->next = NULL; l->current = NULL; } struct List { Node * head; Node * tail; int theSize; Node * current; };

Izmir University of Economics12 Linked List Implementation - III struct Node* insert(struct List *l, struct Node *x,int pos){ if(pos > l->theSize){ l->tail->prev->next = x; /* 3 */ x->prev = l->tail->prev; /* 1 */ x->next = l->tail; /* 2 */ l->tail->prev = x; /* 4 */ } else{ Iterator(l,pos); l->current->prev->next = x; /* 3 */ x->prev = l->current->prev; /* 1 */ x->next = l->current; /* 2 */ l->current->prev = x; /* 4 */ } l->current = x; l->theSize++; return x; }

Linked List Implementation - IV void remove(struct List *l, int pos){ /*remove the element at position pos in the List */ Node *p = Iterator(l, pos); p->next->prev = p->prev; p->prev->next = p->next; free(p); l->theSize--; } 13Izmir University of Economics

Linked List Implementation - V Izmir University of Economics14 Struct Node* Iterator(struct List *l, int pos){ if(pos > l->theSize) return l->current=l->tail->prev; else{ l->current = l->head->next; for(int i = 1; i < pos; i++) l->current = l->current->next; return l->current; } ElemType findKth(struct List *l, int pos){ if(pos > l->theSize){ printf(“ position is out of bound!"); return NULL; } else return Iterator(l, pos)->data; }

Vector: Array Implementation - I Vector implementation: Advantage: indexable in constant time; Disadvantage: insertion and removal of items at locations other than the end are expensive. List implementation: Advantage: Insertions and removals are cheap; Disadvantage: is not easily indexable. #define FatalError(Str) fprintf(stderr,"%s\n",Str), exit(1) Izmir University of Economics15 #include #include "fatal.h“ #define SPARE_CAPACITY 16 typedef int ElementType; struct ListHeader { int theSize; int theCapacity; ElementType* elements; }; typedef struct ListHeader* List;

Vector: Array Implementation - II Izmir University of Economics16 List InitializeList(List L){ if(L != NULL) DeleteList(L); L = malloc(sizeof(struct ListHeader)); if(L == NULL) FatalError( "Out of memory!”); L->theSize = 0; L->theCapacity = SPARE_CAPACITY; L->elements = malloc((L->theCapacity)*sizeof(ElementType)); if(L->elements == NULL) FatalError( "Out of memory!”); return L; } int IsEmpty(List L){ return L->theSize == 0; } int Size(List L){ return L->theSize; } int Capacity(List L){ return L->theCapacity; } void DeleteList(List L){ free(L->elements); free(L); }

Izmir University of Economics17 Vector: Array Implementation - III void InsertToBack(List L, ElementType X){ if(L->theSize == L->theCapacity) ReserveList(L,2*L->theCapacity+1); L->elements[L->theSize++] = X; } ElementType DeleteFromBack(List L){ if(L->theSize == 0) return -1; return L->elements[L->theSize--]; } void ReserveList(List L, int newCapacity){ int i; if(newCapacity theSize) return; ElementType* oldElements = L->elements; L->elements = malloc(newCapacity*sizeof(ElementType)); if(L->elements == NULL) FatalError( "Out of memory!”); for (i=0; i theSize; i++) L->elements[i] = oldElements[i]; L->theCapacity = newCapacity; free(oldElements); }