* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download CE221_week_3_Chapter3_ListStackQueuePart1
Survey
Document related concepts
Transcript
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 Izmir University of Economics 1 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 Economics 2 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 Economics 3 The List ADT - I • A general list of the form: A0, A1, A2, ..., AN-1 • The size of this list is N. Special list of size 0 is an empty list. • For any list except empty list, Ai follows or succeeds Ai-1 (i<N); Ai-1 precedes Ai,(i>0). The predecessor of A0 and the successor of AN-1 are not defined. • First element of the list is A0 and the last element is AN-1. The position of element Ai is i. Izmir University of Economics 4 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 Economics 5 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 Economics 6 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 Economics 7 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. 8 Izmir University of Economics Simple Linked Lists - III • 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 Economics 9 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) #include <stdlib.h> #include "fatal.h“ #define SPARE_CAPACITY 16 typedef int ElementType; struct ListHeader { int theSize; int theCapacity; ElementType* elements; }; typedef struct ListHeader* List; Izmir University of Economics 10 Vector: Array Implementation - II 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){ void DeleteList(List L){ return L->theSize; free(L->elements); } free(L); int Capacity(List L){ } return L->theCapacity; } Izmir University of Economics 11 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 < L->theSize) return; ElementType* oldElements = L->elements; L->elements = malloc(newCapacity*sizeof(ElementType)); if(L->elements == NULL) FatalError( "Out of memory!”); for (i=0; i<L->theSize; i++) L->elements[i] = oldElements[i]; L->theCapacity = newCapacity; free(oldElements); } Izmir University of Economics 12 Linked List Implementation - I • 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). Izmir University of Economics 13 Linked List Implementation - II typedef int ElemType; struct Node { ElemType data; Node * prev; Node * next; }; struct List { Node * head; Node * tail; int theSize; Node * current; }; 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; } Izmir University of Economics 14 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; } Izmir University of Economics 15 Linked List Implementation - IV void remove(struct List *l, int pos){ /*remove the element at position pos in the List */ if (l->theSize <>0) { Node *p = Iterator(l, pos); p->next->prev = p->prev; p->prev->next = p->next; free(p); l->theSize--; } } Izmir University of Economics 16 Linked List Implementation - V 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 = 0; 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; } Izmir University of Economics 17 Homework Assignments • 3.2, 3.4, 3.5, 3.15, 3.29, 3.34.a, 3.34.b, 3.36, 3.37 • You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics 18