ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.

Slides:



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

DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
DATA STRUCTURES USING C++ Chapter 5
CSE Lecture 12 – Linked Lists …
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
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.
CMSC 341 Lists 3. 2 Doubly-Linked Lists Option: add pointer to previous node Issues –doubles number of pointers –allows immediate (O(1)) access to previous.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
C++ Classes and Data Structures Jeffrey S. Childs
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 6 – September 6 th, 2001.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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,
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 9 – September 18, 2001.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 10 – September 20, 2001.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 5 – September 4 th, 2001.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
IKI 10100I: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Data.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture1.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
UNIT-3 LINKED LIST.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
LinkedList Class.
Linked List Yumei Huo Department of Computer Science
Chapter 16-2 Linked Structures
Linked Lists.
Object Oriented Programming COP3330 / CGS5409
Chapter 18: Linked Lists.
Lists List: finite sequence of data elements
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
CMSC 341.
Linked Lists.
Doubly Linked List Implementation
Chapter 16 Linked Structures
CMSC 341.
CMSC 341.
CSI 1340 Introduction to Computer Science II
CMSC 341 List 2.
General List.
Doubly Linked List Implementation
Linked List Improvements
Chapter 9 Linked Lists.
Presentation transcript:

ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department

ICOM 4035Dr. Manuel Rodriguez Martinez2 Readings Chapter 17 of textbook: Linked Lists

ICOM 4035Dr. Manuel Rodriguez Martinez3 Linked Lists A linked list is a container used to store elements sequentially. Unlike the array (and the vector) the linked list does not need to have a fixed size. –Instead the linked list can be grown as much as necessary during run time. But unlike arrays, the memory allocated for array elements is not contiguous, so the cost of accessing in the linked list is larger.

ICOM 4035Dr. Manuel Rodriguez Martinez4 Basics of linked list The basic form the linked list is called the singly linked-list It stores elements of given type Object in a “node”. Each node points to the next elements in the list, except for the last element which points to the value NULL. The list has a special element called the header that stores no data. The header points to the first element in the list, or to the value NULL if the list is empty.

ICOM 4035Dr. Manuel Rodriguez Martinez5 Singly linked list node // Make object be a string typedef Object string; // define a node for the linked list struct Node { // data field Object data; // pointer to the next node in the list Node *next; }; data next

ICOM 4035Dr. Manuel Rodriguez Martinez6 Sample linked lists  header Empty singly Linked list  BobbyAnaBecky Singly linked list with three elements header

ICOM 4035Dr. Manuel Rodriguez Martinez7 Linked list interface class linkedlist{ public: linkedlist(); linkedlist(cons linkedlist &L); ~linkedlist(); const linkedlist& operator= (const linkedlist &L); bool is_empty() const; void insert(const Object& obj); bool erase(const Object& obj); Node *find(const Object& obj) const; Node *first() const; void make_empty() const; int size(); private: Node *header; int list_size; };

ICOM 4035Dr. Manuel Rodriguez Martinez8 Constructor Makes an empty list. linkedlist::linkedlist(){ header = new Node; header->next = NULL; list_size = 0; }  header

ICOM 4035Dr. Manuel Rodriguez Martinez9 Copy constructor Makes a deep copy (element by element) of a list L1 into a new list L2.  BobbyAnaBecky header L1  BobbyAnaBecky header L1  BobbyAnaBecky header L2 L2(L1)

ICOM 4035Dr. Manuel Rodriguez Martinez10 Copy constructor linkedlist::linkedlist(const linkedlist& L){ Node *temp1=NULL, *temp2=NULL, *new_node = NULL; header = new Node; header->next = NULL; for (temp1 = header, temp2 = L.first(); temp2 != NULL; temp1 = temp1->next, temp2 = temp2->next) { new_node = new Node; new_node->data = temp2->data; new_node->next = NULL; temp1->next = new_node; ++list_size; }

ICOM 4035Dr. Manuel Rodriguez Martinez11 Copy assignment Pretty much like copy constructor const linkedlist& linkedlist::linkedlist(const linkedlist& L){ Node *temp1=NULL, *temp2=NULL, *new_node = NULL; if (this != &L){ make_empty(); for (temp1 = header, temp2 = L.first(); temp2 != NULL; temp1 = temp1->next, temp2 = temp2->next) { new_node = new Node; new_node->data = temp2->data; new_node->next = NULL; temp1->next = new_node; ++list_size; } return *this; }

ICOM 4035Dr. Manuel Rodriguez Martinez12 Destructor Removes all elements from the list and then deletes the header.    Bob Tom 

ICOM 4035Dr. Manuel Rodriguez Martinez13 Destructor linkedlist::~linkedlist(){ // remove all the stuff from the list make_empty(); // remove the header delete header; }

ICOM 4035Dr. Manuel Rodriguez Martinez14 Insert operation Adds a new element after the header of the list  Bob Tom  Bob Tom Will header

ICOM 4035Dr. Manuel Rodriguez Martinez15 Insert operation void linkedlist::insert(const Object& obj){ temp = new Node; // creates the new node temp->data = obj; // adds the data to it temp->next = header->next; // adds it to the list header->next = temp; // make header point to it ++list_size; // increase list size }

ICOM 4035Dr. Manuel Rodriguez Martinez16 Insert after a position Adds a new element after a position in the list (a pointer to a node) –Obtained from either find() or first() void linkedlist::insert(const Object& obj, Node *pos){ assert(pos != NULL); temp = new Node; // creates the new node temp->data = obj; // adds the data to it temp->next = pos->next; // adds it to the list pos->next = temp; // make pos point to it ++list_size; }

ICOM 4035Dr. Manuel Rodriguez Martinez17 Erase operation Removes the first occurrence of an object from the list. Returns true if the element is deleted, false if not found.  Bob Tom Bill erase(“Bill”);  Bob Tom Bill header

ICOM 4035Dr. Manuel Rodriguez Martinez18 Erase operation bool linkedlist::erase(const Object& obj){ Node *temp1 = NULL, *temp2 = NULL; for ( temp1 = header->next, temp2 = header; temp1 != NULL; temp1 = temp1->next, temp2 = temp2->next){ if (temp1->data == obj){ // remove from the list temp2->next = temp1->next; temp1->next = NULL; delete temp1; --list_size; return true; } return false; }

ICOM 4035Dr. Manuel Rodriguez Martinez19 Find operation Returns a pointer to the position at which an object obj is located. Returns NULL if the element is not in the list.  Bob Tom Bill find(“Bill”); header  Bob Tom Bill header temp Return temp

ICOM 4035Dr. Manuel Rodriguez Martinez20 Find operation Node* linkedlist::find(const Object& obj) const{ Node *temp=NULL; for (temp = header->next; temp != next; temp = temp->next){ if (temp->data == obj){ return temp; } // not found return NULL; }

ICOM 4035Dr. Manuel Rodriguez Martinez21 First operation Returns position (pointer) to the first element in the list. Node* linkedlist::first() const{ return header->next; }

ICOM 4035Dr. Manuel Rodriguez Martinez22 Is Empty operation Determines if the list is empty –If header->next is null bool linkedlist::is_empty() const { return header->next == null; }

ICOM 4035Dr. Manuel Rodriguez Martinez23 Make Null operation Erase all elements from the list until is becomes an empty list.    Bob Tom

ICOM 4035Dr. Manuel Rodriguez Martinez24 Make Null operation void linkedlist::make_empty() { while (!is_empty()){ erase(header->next); }

ICOM 4035Dr. Manuel Rodriguez Martinez25 Size operation Returns the size of the list int linkedlist::size() const { return list_size; }