Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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 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; };

12 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; }

13 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

14 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; }

15 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;

16 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); }

17 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); }


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

Similar presentations


Ads by Google