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.

Slides:



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

Linked Lists.
Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
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.
CHP-5 LinkedList.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Ceng-112 Data Structures I Chapter 5 Queues.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
 Balancing Symbols 3. Applications
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
CS Data Structures Chapter 3 Stacks and Queues.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Data Structures Using C++ 2E
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Information and Computer Sciences University of Hawaii, Manoa
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 CSE 1342 Programming Concepts Trees. 2 Basic Terminology Trees are made up of nodes and edges. A tree has a single node known as a root. –The root is.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data structures Abstract data types Java classes for Data structures and ADTs.
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.
Data Structure & Algorithm
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
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.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
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.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
LINKED LISTS.
List data structure This is a new data structure. The List data structure is among the most generic of data structures. In daily life, we use shopping.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Chapter 16: Linked Lists.
CSE 1342 Programming Concepts
Cpt S 122 – Data Structures Abstract Data Types
Data Structure By Amee Trivedi.
Queues Chapter 4.
Chapter 12 – Data Structures
Top 50 Data Structures Interview Questions
Lectures linked lists Chapter 6 of textbook
Data Structure Interview Question and Answers
12 C Data Structures.
UNIT-3 LINKED LIST.
Data Structures and Algorithms
Stacks and Queues.
Data Structures Interview / VIVA Questions and Answers
Stack and Queue APURBO DATTA.
Data Structures: A Pseudocode Approach with C
Data Structures and Algorithms
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
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 Graphically a linked-list may be represented as: 4789 * 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 Recursive Lookup on an Unordered List int lookup(int x, Cell * L) { if (L = = NULL) return 0; // item not found else if (x = = L->element) return 1; // item found else return lookup(x, L->nextCell); }

9 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) != NULL) if (x = = ((*pL)->element) (*pL) = (*pL)->nextCell; else delete(x, &((*pL)->nextCell); }

10 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) = = NULL) { (*pL) = new Cell; (*pL)->element = x; (*pL)->nextCell = NULL; } else if (x != ((*pL)->element) insert(x, &((*pL)->nextCell); }

11 Recursive Lookup on an Sorted List int lookup(int x, Cell * L) { if (L = = NULL) return 0; // item not found else if (x > L->element) return lookup(x, L->nextCell); else if (x = = L->element) return 1; // item found else // x element, x cannot be in list return 0; // item not found }

12 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).

13 Doubly Linked-List Data Structure Graphically a doubly linked-list is represented as: 4 Head 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. 56 ** Tail (optional)

14 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.

15 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 != NULL) p->nextCell->previousCell = p->previousCell; if (p->previousCell = = NULL) //p points to first cell (*pL) = p->nextCell; else p->previousCell->nextCell = p->nextCell; }

16 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.

17 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.

18 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.

19 Array Implementation of Stacks

20 Array Implementation of Stacks

21 Linked-List Implementation of Stacks

22 Linked-List Implementation of Stacks

23 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.

24 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.

25 Linked-List Implementation of a Queue