Presentation is loading. Please wait.

Presentation is loading. Please wait.

E.G.M. Petrakislists, stacks, queues1 Lists List: finite sequence of data elements –Ordered: each element has a position in list –All elements has the.

Similar presentations


Presentation on theme: "E.G.M. Petrakislists, stacks, queues1 Lists List: finite sequence of data elements –Ordered: each element has a position in list –All elements has the."— Presentation transcript:

1 E.G.M. Petrakislists, stacks, queues1 Lists List: finite sequence of data elements –Ordered: each element has a position in list –All elements has the same data type –The operations depend on the type of the list and not on the data type List: the most general type Stacks, Queues: restricted versions of lists –Not all operations are allowed

2 E.G.M. Petrakislists, stacks, queues2 list info next list New element Insertions current

3 E.G.M. Petrakislists, stacks, queues3 list Deleted element Deletions current

4 E.G.M. Petrakislists, stacks, queues4 Terminology: –Empty: contains no elements –Length: number of elements in list –Head, or list: pointer to the beginning of the list –Tail: pointer to the last element –Current: pointer to current element –Ordered: elements in ascending/descending order

5 E.G.M. Petrakislists, stacks, queues5 Operations on lists: –setFirst: set “current” to “Head” –setPos(i): sets current to the i-th element –currValue: returns value of “current” element –next/prev: “current” points to next/prev element –clear: delete all elements –insert: inserts an element in current position –append: inserts an element at “tail” –remove: delete the “current” element –find(k): set current to the first occurrence of “k” –isInList: true/false if current position is in list –isEmpty: true/false if list is empty

6 E.G.M. Petrakislists, stacks, queues6 class list {// List class ADT in C++ public: list(const int = LIST_SIZE); //constructor ~list( );//destructor void clear ( );// remove all elements void insert(const ELEM &); // inset at current void append(const ELEM&); // insert at tail ELEM remove ( ); // remove current void setFirst ( ); // set current to HEAD void prev ( ) ;// set current to previous void next ( );// set current to next int length ( ) const;// return size of list void setPos(const int);// set current to new pos void setValue(const ELEM&) // set current’s values bool isEmpty( ) const;// true/false on empty bool isInList( ) const;// true/false if current is in list bool find(const ELEM&);// find value of current in list };

7 E.G.M. Petrakislists, stacks, queues7 Iterate through the whole list: MyList for (MyList.first( ); MyList.isInList(); MyList.next( ) DoSomething(MyList.currValue( )); If MyList: (12 32 15) and current points to 32 then MyList.insert(90) changes the list to be (12 32 90 15)

8 E.G.M. Petrakislists, stacks, queues8 Implementation, two approaches: –Array-based The elements are stored in array Fixed size Fast implementation Actual number or element less than size allocated –Linked list Dynamic: allocates memory for new elements No restriction on number of elements except physical memory size Slower but more efficient

9 E.G.M. Petrakislists, stacks, queues9 Array implementation (1) : –Stores elements in continuous array positions –Head of list at pos 0 –Insertion/deletion causes shifting of elements

10 E.G.M. Petrakislists, stacks, queues10 class list{//Array based list class private: int msize //Maximum size of list int numinlist//Actual number of ELEMs int curr;//Position of “current” ELEM* listarray;//Array holding ELEMs Public: list (const int=LIST_SIZE); // Constructor ~list();// Destructor void clear();// Remove all ELEMs from list void insert(const ELEM&);// Insert ELEM at current position void append(const ELEM&);// Insert ELEM at tail of list ELEM remove();// Remove and return current ELEM void setFirst();// Set curr to first position; void prev();// Move curr to previous position; void next();// Move curr to next position; int length() const;// Return current length of list void setPos(const int);// Set curr to specified position void setValue(const ELEM&);// Set current ELEM's value ELEM currValue() const;// Return current ELEM's value bool isEmpty() const;// Return TRUE if list is empty bool isInList() const;// TRUE if curr is within list bool find(const ELEM&);// Find value (from current position) };

11 E.G.M. Petrakislists, stacks, queues11 List::List(int sz) // Constructor: initialize { msize = sz; numinlist = 0; curr = 0; listarray = new Elem[sz]; } List::~List() // Destructor: return array space { delete [] listarray; } void List::clear() // Remove all Elems from list { numinlist = 0; curr = 0; } // Simply reinitialize values void List::insert(const Elem item) { // Insert Elem at current position // Array must not be full and curr must be a legal position assert((numinlist =0) && (curr <= numinlist)); for(int i = numinlist; i > curr; i--) // Shift Elems up to make room listarray[i] = listarray[i - 1]; listarray[curr] = item; numinlist++; // Increment current list size } void List::append(const Elem item) { // Insert Elem at tail of list assert(numinlist < msize); // List must not be full listarray[numinlist++] = item; // Increment list size }

12 E.G.M. Petrakislists, stacks, queues12 Elem List::remove( ) { // Remove and return current Elem assert( !isEmpty() && isInList() ); // Must be an Elem to remove Elem temp = listarray[curr]; // Store removed Elem for (int i=curr; i < numlist-1; i++) // shift elements down listarray[i] = listarray[i+1]; numinlist--;// Decrement current list size return temp; } void List::setFirst( ) // Set curr to first position { curr = 0; } void List::prev( ) // Move curr to previous position { curr--; } void List::next( ) // Move curr to next position { curr++; } int List::length( ) const // Return current length of list { return numinlist; } void List::setPos(int pos) // Set curr to specified position {curr = pos; }

13 E.G.M. Petrakislists, stacks, queues13 void List::setValue(const Elem val) { // Set current Elem's value assert(isInList( )); // Curr must be at valid position listarray[curr] = val; } Elem List::currValue( ) const { // Return current Elem's value assert(isInList( )); // Must be at a valid position return listarray[curr]; } bool List::isEmpty( ) const // Return TRUE if list is empty { return numinlist == 0; } bool List::isInList( ) const // TRUE if curr is within list { return (curr >= 0) && (curr < numinlist); } bool List::find(int val) { // Find value (starting at curr) while (isInList( )) // Stop if reach end if (key(currValue( )) == val) return TRUE; // Found it else next( ); return FALSE; // Not found }

14 E.G.M. Petrakislists, stacks, queues14 Array implementation(2): pointers to elems 1 11526 list 111 12 510 260 8 9 10 11 12 13 nodes NULL

15 E.G.M. Petrakislists, stacks, queues15 31313 14 376 5 12 List 1 1726 List 2 311932 List 3 1181311415 List 4

16 E.G.M. Petrakislists, stacks, queues16 1260 21110 3516 4125 5171 6132 7 819 91413 10422 11 12318 1363 14 15 163724 17312 18 19320 20 2179 22150 23 24120 25186 List 4 List 2 List 3 List 1

17 E.G.M. Petrakislists, stacks, queues17 class list {//Array based list class private: int msize //Maximum size of list int numinlist//Actual number of ELEMs int curr;//Position of “current” int next;// next element int avail;// next available position ELEM* listarray;//Array holding ELEMs plus pointers to next ELEMs int get_node( );//get position of available node void free_node( );//return node in array void insert(p, x);//insert node after the node pointed by p void delete(p, x);//delete node after the node pointed by p public: list (const int=LIST_SIZE); // Constructor ~list( );// Destructor void clear( );// Remove all ELEMs from list void insert(const ELEM&);// Insert ELEM at current position void append(const ELEM&);// Insert ELEM at tail of list ELEM remove( );// Remove and return current ELEM void setFirst( );// Set curr to first position; void prev( );// Move curr to previous position; void next( );// Move curr to next position; int length( ) const;// Return current length of list ……….// More member functions };

18 E.G.M. Petrakislists, stacks, queues18 list::list(int sz) {// Constructor: initialize msize = sz; numinlist = 0; curr = 0; listarray = new Elem[sz]; // put all (available) elements in a stack avail = 0;// the first available element for (i=0; i < msize; i++) listarray[i].next = i+1;// each elements points to its successor listarray[msize-1].next = nothing;// the last elem has no next } list::~list( ) // Destructor: return array space { delete [] listarray; }

19 E.G.M. Petrakislists, stacks, queues19 int list::get_node( ) { if (avail == nothing) error(‘list overflow’) else { int pos = avail; avail = listarray[avail].next; return pos; } void list::free_node (int p) { node[p].next = avail; avail =p; }

20 E.G.M. Petrakislists, stacks, queues20 void insert::list (int p, int x) { if (p == null) error (‘void insertion’) else { int q = get_node( ); node[q].info = x; node[q].next = node[p].next; node[p].next=q; } void delete::list (int p; int x) { if ( p == 0) error (‘void deletion’); else { int q = node[p].next; x = node[q].info; node[p].next = node[q].next; free_node(q); }

21 E.G.M. Petrakislists, stacks, queues21 Linked List implementation: makes use of pointers to elements –Allocates memory for new elements as needed –Each node is a distinct object The node class Class link { // A linked-list node public: ELEM element// node value link *next;// pointer to next node link(const ELEM& val, link *nextval = NULL); {element = val; next = nextval;} link(link *nextval = NULL) {next = nextval;} ~link( ) { } }

22 E.G.M. Petrakislists, stacks, queues22 class List { // Linked list class private: Link* head; // Pointer to list header Link* tail; // Pointer to last Elem in list Link* curr; // Position of "current" Elem public: List( ); // Constructor ~List( ); // Destructor void clear( ); // Remove all Elems from list void insert(const Elem); // Insert Elem at current position void append(const Elem); // Insert Elem at tail of list Elem remove( ); // Remove and return current Elem void setFirst( ); // Set curr to first position void prev( ); // Move curr to previous position void next( ); // Move curr to next position int length( ) const; // Return current length of list void setPos(int); // Set curr to specified position void setValue(const Elem); // Set current Elem's value Elem currValue( ) const; // Return current Elem's value bool isEmpty( ) const; // Return TRUE if list is empty bool isInList( ) const; // TRUE if curr is within list bool find(int); // Find value (from current position) };

23 E.G.M. Petrakislists, stacks, queues23 List::List( ) // Constructor { head = new Link; tail = head; curr = head; } // Initialize List::~List( ) { // Destructor while(head != NULL) {// Return link nodes to free store curr = head; head = head  next; delete curr; } void List::clear( ) { // Remove all Elems from list while (head  next != NULL) { // Return link nodes to free store curr = head  next; // keeps header node !! head  next = curr  next; // otherwise, similar to ~List( ) delete curr; } curr = head; tail = head; // Reinitialize }

24 E.G.M. Petrakislists, stacks, queues24 void List::insert(const Elem item)// Insert Elem at current position { assert(curr != NULL); // Must be pointing to list Elem curr  next = new Link(item, curr  next); if (tail == curr) // Appended new Elem tail = curr  next; } void List::append(const Elem item) // Insert Elem at tail of list { tail = tail  next = new Link(item, NULL); } Elem List::remove( ) { // Remove and return current Elem assert(isInList( )); // Must be valid position in list Elem temp = curr  next  element; // Remember value Link* ltemp = curr  next; // Remember link node curr  next = ltemp  next; // Remove from list if (tail == ltemp) tail = curr; // Removed last Elem: set tail delete ltemp; // Send link to free store return temp; // Return value removed } void List::setFirst( ) // Set curr to first position { curr = head; }

25 E.G.M. Petrakislists, stacks, queues25 void List::next( ) // Move curr to next position { if (curr != NULL) curr = curr  next; } void List::prev( ) { // Move curr to previous position Link* temp = head; if ((curr == NULL) || (curr == head)) // No previous Elem { curr = NULL; return; } // so just return while ((temp!=NULL) && (temp  next != curr)) temp=temp  next; curr = temp; } int List::length( ) const { // Return current length of list int cnt = 0; for (Link* temp = head  next; temp != NULL; temp = temp  next) cnt++; // Count Elems and return cnt; } void List::setPos(int pos) { // Set curr to specified position curr = head; for (int i = 0; (curr != NULL) && (i < pos) i++) curr = curr  next; }

26 E.G.M. Petrakislists, stacks, queues26 void List::setValue(const Elem val) {// Set current Elem's value assert(isInList()); curr  next->element = val; } Elem List::currValue() const // Return value of current Elem { assert(isInList( )); return curr  next  element; } bool List::isEmpty() const // Return TRUE if list is empty { return head  next == NULL; } bool List::isInList() const // TRUE if curr is within list { return (curr != NULL) && (curr  next != NULL); } bool List::find(int val) { // Find value (starting at curr) while (isInList( )) if (key(curr  next->element) == val) return TRUE; else curr = curr  next; return FALSE; // Not found }

27 E.G.M. Petrakislists, stacks, queues27 Comparison of implementations –Array-Based Lists: Insertion and deletion are  (n) instead of  (1) Prev and direct access are  (1) instead of  (n) Array must be allocated in advance No overhead if all array positions are full Faster in some cases –Linked Lists: Insertion and deletion are  (1) Prev and direct access are  (n) Space grows with number of elements Every element requires overhead

28 E.G.M. Petrakislists, stacks, queues28 Doubly linked list: allows direct access to both next and previous elements of the current pointer –insert (delete) operations update both next/prev pointers –Easy implementation curr  next = new (item, curr  next, curr); if (curr  next  != NULL) curr  next  prev = curr  next

29 E.G.M. Petrakislists, stacks, queues29 current Insertion in doubly linked list

30 E.G.M. Petrakislists, stacks, queues30 current Deletion from doubly linked list

31 E.G.M. Petrakislists, stacks, queues31 Circular linked lists: the next pointer of the last element points to the first element –The tail points is no longer needed –Implementation: left as an exercise


Download ppt "E.G.M. Petrakislists, stacks, queues1 Lists List: finite sequence of data elements –Ordered: each element has a position in list –All elements has the."

Similar presentations


Ads by Google