1 / 30

CSE 1342 Programming Concepts

CSE 1342 Programming Concepts. Lists. Basic Terminology. A list is a finite sequence of zero or more elements. For example, (1,3,5,7) is a list of the odd integers from 1 to 7 inclusive. The length of the list is the number of elements in the list.

ruthortiz
Download Presentation

CSE 1342 Programming Concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 1342 Programming Concepts Lists

  2. Basic Terminology • A list is a finite sequence of zero or more elements. • For example, (1,3,5,7) is a list of the odd integers from 1 to 7 inclusive. • The length of the list is the number of elements in the list. • The first element of the list is called the head. The remaining elements of the list are collectively called the tail. • A sublist is a series of consecutive elements in a list not necessarily starting at the head.

  3. Basic Terminology • A prefix is a sublist that starts at the head of a list. • A suffix is a sublist that terminates at the end of a list. • Each element in a list is associated with a position. • For example, (3, 5, 23, 100) is a list containing four elements with the 3 occupying position 1, the 5 occupying position 2, and so on. • The number of positions in a list equals the length of the list. • A particular value can appear at multiple positions in a list.

  4. Operations on Lists • A list must support the following operations: • Insertion of new elements. • Deletion of existing elements. • Verification that an element is/isn’t in the list • A data set upon which we can execute an insert, delete, and look-up is called a dictionary, no matter how the set is implemented or what it is used for. • A list is a dictionary.

  5. Operations on Lists • Other optional list operations: • Retrieval of a particular element in the list. • Calculate the length of the list. • Determine if the list is empty.

  6. The Linked-List Data Structure • Each cell (element) in the linked-list will have the following general format: struct Cell { int element; //data goes here Cell *nextCell; //ptr to next cell in the list }; • The data element may be simple or complex. • The list may be ordered or unordered.

  7. The Linked-List Data Structure 4 7 8 9 * Graphically a linked-list may be represented as: L • In this example • L is a pointer to the head of a four element list. • Each double box represents a cell. • The values 4, 7, 8, and 9 are the data values. • An arrow is a pointer to the next element in the list. • * is the symbol for a NULL pointer (end-of-list indicator).

  8. Iterative Lookup on an Unordered List boolean lookup(int x, Cell * L) { while (L != nullptr) { if (x = = L->element) return true; // item found L = L->nextCell; } return false; }

  9. Recursive Lookup on an Unordered List boolean lookup(int x, Cell * L) { if (L = = nullptr) return false; // item not found else if (x = = L->element) return true; // item found else return lookup(x, L->nextCell); }

  10. Iterative Insert on an Ordered List //This function adds a new cell at the appropriate location in //an ordered list. void insert(int x, Cell ** pL) { Cell* newCell = new Cell; newCell->element = x; while ((*pL ! = nullptr) && (x > (*pL)->element)) { pL = &((*pL)->nextCell); } newCell->nextCell = *pL; *pL = newCell; }

  11. Recursive Insert on an Unordered List //This function adds a new cell to the end of the list //if the value x is not already in the list. void insert(int x, Cell ** pL) { if (*pL = = nullptr) { *pL = new Cell; (*pL)->element = x; (*pL)->nextCell = NULL; } else if (x != ((*pL)->element) insert(x, &((*pL)->nextCell); }

  12. Iterative Lookup on an Sorted List boolean lookup(int x, Cell * L) { while (L ! = nullptr) { if (x = = L->element) return true; // item found if (x > L->element) L = L->nextCell); else // x < L->element, x cannot be in list return false; // item not found }//end while return false; }

  13. Recursive Lookup on an Sorted List boolean lookup(int x, Cell * L) { if (L = = nullptr) return false; // item not found else if (x > L->element) return lookup(x, L->nextCell); else if (x = = L->element) return true; // item found else // x < L->element, x cannot be in list return false; // item not found }

  14. Iterative Delete on an Unordered List //This function deletes the value x from the list. If the value x is not //present in the list the function does nothing. void delete(int x, Cell ** pL) { while ((*pL) != nullptr) { if (x = = ((*pL)->element) { Cell ** tempPtr = pL; *pL = (*pL)->nextCell; delete *pL; return; } else pL = &((*pL)->nextCell); } //end while }//end delete

  15. Iterative Delete on an Unordered List //This function deletes the value x from the list. If the value x is not //present in the list the function does nothing. Memory belonging to the //deleted cell is freed. void delete(int x, Cell ** pL) { Cell** temp; while (*pL != nullptr) { if (x = = ((*pL)->element) { *temp = *pL; *pL = (*pL)->nextCell; delete *temp; return; } pL = &((*pL)->nextCell); }//end while }//end delete

  16. Recursive Delete on an Unordered List //This function deletes the value x from the list. If the value x //is not present in the list the function does nothing. Memory //belonging to the deleted cell is not freed. void delete(int x, Cell ** pL) { if (*pL != nullptr) if (x = = ((*pL)->element) *pL = (*pL)->nextCell; else delete(x, &((*pL)->nextCell); }

  17. Analysis of List Algorithms • All insert, delete and lookup operations on a list are O(n). • The exception is an insert on an unsorted list that allows duplicate entries. In this case the running time is O(1). • Since the algorithm does not search for duplicate entries, all new entries are inserted in the front of the list (there is no looping involved).

  18. Doubly Linked-List Data Structure 4 5 6 * * Graphically a doubly linked-list is represented as: Head Tail (optional) • Doubly linked-lists … • Facilitate movement both forwards and backwards in the list. • Make deletion of a cell easier when the address of the cell to be deleted is already known.

  19. Doubly Linked-List Data Structure • Each cell (element) in the doubly linked-list will have the following general format: struct Cell { int element; //data goes here Cell *nextCell; //ptr to next cell in the list Cell *previousCell; //ptr to previous cell }; • The data element may be simple or complex. • The list may be ordered or unordered.

  20. Delete on a Doubly Linked-List //The calling function supplies the address of the list and //the address of the node to be deleted. void delete(Cell * p, Cell ** pL) { //p points to cell to delete if (p->next != nullptr) p->nextCell->previousCell = p->previousCell; if (p->previousCell = = nullptr) //p points to first cell (*pL) = p->nextCell; else p->previousCell->nextCell = p->nextCell; }

  21. Lists Implemented as Arrays • A list may also be implemented as an array. • Advantages to array implementation: • Binary search may be used for look-ups. • Linked-lists are limited to sequential searches. • Eliminates the need for pointers (except for the array name) and pointer notation. • Disadvantages to array implementation: • List size is limited to array size. • Maximum possible list size must be known at compile time. • Potential for wasted memory. • Insertion and deletion of list elements may require other list elements to be repositioned within the array.

  22. Stacks • A stack is an abstract data type based on the list model. • All stack operations are performed at one end of the list, known as the top. • A stack is a last-in-first-out (LIFO) ADT. • A stack may be implemented as an array or a linked-list.

  23. Stack Operations Assuming S is a stack of type ETYPE and x is an element of type ETYPE, the following is a set of operations common the the stack ADT.

  24. Array Implementation of Stacks

  25. Array Implementation of Stacks

  26. Linked-List Implementation of Stacks

  27. Linked-List Implementation of Stacks

  28. Queues • A queue is an abstract data type based on the list model. • All insertions into the queue are performed at the rear of the queue. • All deletions from the queue are performed at the front of the queue. • A queue is a first-in-first-out (FIFO) ADT. • A queue may be implemented as an array or a linked-list.

  29. Queue Operations • Assuming Q is a queue of type ETYPE and x is an element of type ETYPE, the following is a set of operations common the the stack ADT. • clear(Q) - Make the queue Q empty. • dequeue(Q, x) - If Q is empty return FALSE; else set x the the value at the front of Q, delete the front element of Q, and return TRUE. • enqueue(Q, x) - If Q is full return FALSE; else add x to the end of Q, and return TRUE. • isEmpty(Q) - Return TRUE is Q is empty; else return FALSE. • isFull(Q) - Return TRUE is Q is full; else return FALSE.

  30. Linked-List Implementation of a Queue

More Related