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.

Slides:



Advertisements
Similar presentations
Singly linked lists Doubly linked lists
Advertisements

Stacks, Queues, and Linked Lists
DATA STRUCTURES USING C++ Chapter 5
CSE Lecture 12 – Linked Lists …
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
TREES Chapter 6. Trees - Introduction  All previous data organizations we've studied are linear—each element can have only one predecessor and successor.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
They’re not just binary anymore!
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP 110 Introduction to Programming Mr. Joshua Stough.
Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques
Binary Search Trees Chapter 6.
Binary Trees Chapter 6.
CS 206 Introduction to Computer Science II 09 / 30 / 2009 Instructor: Michael Eckmann.
Trees Chapter 8. 2 Tree Terminology A tree consists of a collection of elements or nodes, organized hierarchically. The node at the top of a tree is called.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
1 Data Structures Lists and Trees. 2 Real-Life Computational Problems All about organizing data! –What shape the data should have to solve your problem.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Pointers and Dynamic Data Structures Problem Solving, Abstraction,
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
DATA STRUCTURES AND ALGORITHMS Lecture Notes 5 Prepared by İnanç TAHRALI.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
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.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
ACM/JETT Workshop - August 4-5, 2005 Using Visualization Tools To Teach Data Structures and Algorithms Java applets by Dr. R. Mukundan, University of Canterbury,
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Binary Search Trees Binary Search Trees (BST)  the tree from the previous slide is a special kind of binary tree called a binary.
Starting at Binary Trees
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.
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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
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.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
CMSC 202, Version 5/02 1 Trees. CMSC 202, Version 5/02 2 Tree Basics 1.A tree is a set of nodes. 2.A tree may be empty (i.e., contain no nodes). 3.If.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
DATA STRUCURES II CSC QUIZ 1. What is Data Structure ? 2. Mention the classifications of data structure giving example of each. 3. Briefly explain.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
CSE 1342 Programming Concepts
Cpt S 122 – Data Structures Abstract Data Types
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Problems with Linked List (as we’ve seen so far…)
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Pointers and Linked Lists
Linked Lists.
Lesson Objectives Aims
Find in a linked list? first last 7  4  3  8 NULL
Chapter 8: Data Abstractions
Pointers & Dynamic Data Structures
Lists.
Stacks, Queues, and Deques
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Tree (new ADT) Terminology: A tree is a collection of elements (nodes)
Presentation transcript:

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 we want to do at the ADT level is describe what it is and what it should do  We don’t worry about HOW it does it  There’s no definite rule for what operations must be supported for each type  Use what makes sense.

Lists:  Things we know about lists:  The items have an order  One comes after another - this doesn’t mean they’re “ordered” in any purposeful way, but there’s a built in order to the elements in a list  The list has a size (n elements in the list)  Data in a list can be duplicated  All elements in the list are of the same data type

List operations we might want:  push(x)-add to end of list  insert(x,k) adds item x to list at kth position  remove (Node *node) – removes a node from the list  removekth(int kth) – removes the node at the kth position from the list  pop() – removes the last element from the list  size() - gives you number of elements in list  isEmpty() – returns true iff the list is empty  find(x) – return the position of x in the list (usually -1 if not in list)  findkth(k) – return the item at the kth position in the list  printList() – you figure it out … I’m sure there are other things you’d want to be able to do with a list.

Linked List (versus Array):  Every element in a linked list consists of at a minimum 2 parts:  The data  A pointer to (aka the address of ) the element coming next in the list  No fixed size (no limit on the number of elements in the list)  No “wasted space”  No empty spaces in between elements in the list  Sequential access (as opposed to random access, like an array) 3  7  4  2 NULL Nodes in a linked list

Node implementation: class Node { friend class LL; // allows another class to access privage members of the Node class int data; Node *next; public: Node(int x); ~Node(); }; //Node Node::Node(int x){ data = x; next = NULL; } //Constructor Node::~Node() { if (next != NULL) { cout << “deleting may cause memory leak.” << endl; } //if } //destuctor

A list class (A list of nodes): #include "Node.hpp“ class LL { Node *first; int currsize; public: LL(); ~LL(); void push(); void insert(x,k); void remove (int pos); int pop(); int size(); bool isEmpty(); int find(x); Node * findkth(k); };

Push: //adds node to end of list  If the list is empty?  Create the new node  Set the first pointer to point to the new node  Set the currsize to 1 void LL::push() { int x = rand()%10; if (first == NULL) { first = new Node(x); } //if currsize=1; } //push class LL { Node *first; int currsize; public: … void push(); … }; 7 NULL first

Push: //adds node to end of list  If List is not empty? class LL { Node *first; int currsize; public: … void push(); … }; first NULL temp NULL 8 new Node(8) Node *temp = first; while (temp->next != NULL) { //Why isn’t the condition while (temp != NULL) { ? temp = temp->next; } //while temp->next = new Node(x); currsize++; How many steps to push onto the list? Is there a more efficient way? (Hint: yes, there is!!!)

Adding a last node pointer: When we start list: last = first; When we’re pushing onto the end of an existing list: last->next = new Node(8); last = last->next; Now what is the count for push()? 7 NULL first NULL first NULL last class LL { Node *first; Node *last; int currsize; public: … void push(); … };

7  4  3  8 NULL 9  void insert(x,k); Inserting node with data, x= 9 at k = 2; last first  How do we get to the kth position in the list?  Loop!  Do we loop to the kth position or the k-1th position? Why?  Is there a better way? What if we did this with an array? Node *n = new Node(9); n->next = node4->next; node4->next = n;

insert(x,k) bool LLC::insert(int x, int k) { if (k > currsize) { //illegal insert return false; } //if else { // inserting a new node NodeC *n = new NodeC(x); if (k == 0) { //inserting at front of list if (first == NULL) { //empty list last = n; } //if else { n->next = first; } //else first = n; }// if else if (k != 0){ NodeC *temp = first; for (int j = 0; j < k-1; j++) { temp = temp->next; } //for n->next = temp->next; temp->next = n; if (k == currsize) { //inserting at end of list last = temp->next; } //if } // else if } //else currsize++; return true; } //insert

Problems with Linked List (as we’ve seen so far…)  We can insert a node only after a node we have a pointer to.  We can remove a node only if we have a pointer to its predecessor node  We can traverse the list in only the forward direction Solution?

Stack  Subset of list  Only 2 operations: push() and pop()  Stick something on the end of the stack, or take something off the end of the stack  Last-in, first-out (LIFO)  Think of a pile of … almost anything at all …  you can’t take anything off the BOTTOM of the pile, only off the top. Equally, you can’t add to the pile at the bottom, only at the top.  Applications:  Reversing a word  The “undo” option  Backgracking in a maze  Memory for functions  (the easiest way to visualize this is recursion)  Linked list or array??  Hint: oh please…

Pop()  How do we implement pop?  How many steps?  Now pop again…  Where is the last pointer still pointing?  Is there a better way?  What about arrays – will that work better? first NULL 8 last

Doubly-linked list  Now what do we need to do? Node *temp = last; last = last->prev; delete temp; last->next = NULL;  Now how many steps? class Node { friend class LL; int data; Node *next; Node *prev; public: Node(int x); ~Node(); }; //Node first NULL 8 last NULL temp NULL

Doubly-linked list:  Disadvantages:  A bit more memory (now we’ve got that prev pointer space for each node)  Must manage more pointers when performing operations on the linked list (e.g., insert, remove, etc.)  Advantages:  Makes pop() easier (O(1))  Makes traversing the list in reverse order easier  Reversing the list is easy now  Can go backwards and forwards from a node in a list  We may need surrounding nodes  We may need data that occurred “close to” a node with certain data What about inserting into an ordered list?

Inserting 6 into the ordered list: Node *temp = first; while (temp->data < 6) { // left out check for end of list temp = temp->next; } Node *n = new Node(6); n->prev = temp->prev; temp->prev->next = n; n->next = temp; Easier with a doubly-linked list? first NULL 8 last NULL 6 temp n

Circular List  Can be singly or doubly linked list  Make the “last” node’s next pointer point to the first node.  No NULL pointers  Don’t have to worry about “falling off the end” of the list  Can hit all items in the list, even if we start looking in the middle tail->next = head; head->prev = tail; // if doubly-linked  Do we still need a head and tail pointer? first last

Circular linked lists:  Works nicely with queues:  FIFO (or LILO)  First in, first out  Think of the check-out line in a store  To-do lists  The printing jobs coming into a printer  Now to insert new job:  Node *n = new Node(20);  n->next = last->next;  last->next = n;  To remove a job:  Node *n = last->next;  last->next = last->next->next;  delete n;  What if there’s only one node in the list?  What about searching for a particular node in the list? What if the node isn’t in the list?

How about Find in a linked list?  Find 3 in the list  Find 6 in the list  O(n)  Is there a better way?  Binary search tree! 7  4  3  8 NULL last first

23 Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a list have?  Each node has exactly one predecessor (called the parent)  Except the starting node, called the root  Links from node to its successors are called branches  Nodes with same parent are siblings  Nodes with no children are called leaves

24 Tree  We also use words like ancestor and descendent Pets is the parent of Dogs and Cats Poodle and Beagle are the children of Dogs Poodle, Beagle, Persian, and Siamese are descendents of Pets, Pets is the ancestor of Poodle, Beagle, Persian, and Siamese

25 Tree Terminology  Subtree of a node: A tree whose root is a child of that node  Depth of a node: A measure of its distance from the root: Depth of the root = 0 Depth of other nodes = 1 + depth of parent