Data Structures & Algorithms

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

Chapter 17 Linked Lists.
Linked Lists Chapter 4.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Data Structures Using Java1 Chapter 4 Linked Lists.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Chapter 5 Linked Lists II
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CSCS-200 Data Structure and Algorithms Lecture
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
Linked Lists and Generics Written by J.J. Shepherd.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Memory Management.
Chapter 16: Linked Lists.
CSCE 210 Data Structures and Algorithms
Lecture No.04 Data Structures Dr. Sohail Aslam
C++ Programming:. Program Design Including
Pointers and Linked Lists
Pointers and Linked Lists
Copy Constructor / Destructors Stacks and Queues
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
CS2006- Data Structures I Chapter 5 Linked Lists I.
Lists CS 3358.
List ADT & Linked Lists.
Lecture No.03 Data Structures Dr. Sohail Aslam
UNIT-3 LINKED LIST.
CISC181 Introduction to Computer Science Dr
Stack and Queue APURBO DATTA.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCI 3333 Data Structures Linked Lists.
Chapter 20: Binary Trees.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 21: Binary Trees.
Linked Lists.
Chapter 18: Linked Lists.
Linked List (Part I) Data structure.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Linked Lists.
Introduction to Algorithms and Data Structures
Data Structures & Algorithms
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Lecture No.02 Data Structures Dr. Sohail Aslam
General List.
Programming II (CS300) Chapter 07: Linked Lists
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Lists CMSC 202, Version 4/02.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Getting queues right … finally (?)
Data Structures (CS301) Linked List and its implementation
Linked Lists Chapter 5 (continued)
Presentation transcript:

Data Structures & Algorithms Instructor Name: Lecture-07

Today’s Lecture Linked List (Continue) Linked List Inside the Memory Linked List Operations Linked List using C++ Example Program 2

Linked List Linked List (Continue) A linked-list is a sequence of data structures which are connected together via links. 3

Linked List Linked List Inside Computer Memory 4

Node * newNode = new Node(9); Linked List Linked List Operations Linked List provides operations to work on the nodes inside the list Create a New Node Node * newNode = new Node(9); The first part of the statement that is on the left of the assignment is declaring a variable pointer of type Node. It may also be written as Node * newNode. On the right of this statement, the new operator is used to create a new Node object as new Node(9). What this whole statement tells us “Call the constructor of the Node class and pass it ‘9’ as a parameter. After constructing the object in memory, give me the starting memory address of the object. That address will be stored in the pointer variable newNode.” 5

Linked List Fitting Node into List 6

Linked List Fitting Node into List 7

Linked List Fitting Node into List Steps The first step is to point next pointer of the new node (with data element as 9) to the node with data element as 7. The second step is to point the next pointer of the node with data element 8 to the node the new node with data element 9. The third step is to change the current pointer to point to the new node. 8

Linked List Node Class Code class Node { public: int get() { return object; }; void set(int object) { this->object = object; }; Node * getNext() { return nextNode; }; void setNext(Node * nextNode) { this->nextNode = nextNode; private: int object; Node * nextNode; 9

Linked List OOP in C++ - Conventions Class begins with the word class followed by the class-name Public and Private variables and methods are defined explicitly under the label public and private Default constructor and destructor works if no constructor written In C++, normally, we make two files (.h and .cpp) for a class. The .h file contains the declarations of public and private members of that class. The public methods are essentially the interface of the class to be employed by the users of this class. The .cpp file contains the implementation for the class methods that has the actual code. 10

Linked List List Class Code #include <stdlib.h> #include "Node.h" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; } }; 11

Linked List Understanding Constructor Code Inside Constructor a Node object is created and its reference is assigned to headNode data member of List. setNext() method of the Node class is called for the object pointed to by the headNode pointer. This call is to set the nextNode data member to NULL, i.e. Node’s object pointed to by the headNode pointer is not pointing to any further Node. Setting currentNode pointer to NULL initialize the currentNode pointer to NULL that is not pointing to any Node object. The Final statement is to initialize the size data member to 0 indicating that there is no node present in the list. 12

Linked List Adding a node to List – add Method void add (int addObject) { Node * newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ) { newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; size ++; 13

Linked List Understanding add() Method Method takes object to be added as parameter Firstly, it will make a new node by calling Node class constructor. Insert the value e.g. 2. of the node into the node by calling the set method. Now if the list already exists (has some elements inside or its size is non-zero), it will insert the node after the current position. If the list does not already exist, this node is added as the first element inside the list. After checking the node state , size is incremented by 1 See the pictorial representation on next slide 14

Linked List Understanding add() Method 15

Linked List Understanding add() Method 16

Linked List get() Method int get() { if (currentNode != NULL) return currentNode->get(); } get()returns the value at the address of the node pointed to by the currentNode pointer. 17

Linked List next() Method bool next() { if (currentNode == NULL) return false; lastCurrentNode = currentNode; currentNode = currentNode->getNext(); return true; }; This is next() method, used to advance the currentNode pointer to the next node inside the linked list. 18

Linked List start() Method void start() { lastCurrentNode = headNode; // position currentNode and lastCurrentNode at first element void start() { lastCurrentNode = headNode; currentNode = headNode; }; 19

Linked List remove() Method void remove() { if( currentNode != NULL && currentNode != headNode) { (step 1) lastCurrentNode->setNext(currentNode->getNext()); (step 2) delete currentNode; (step 3) currentNode= lastCurrentNode; (step 4) size--; } }; 20

Linked List remove() Method 21

Linked List remove() Method 22

Linked List Analysis of Linked List add() Method is one step operation as compared to add in Array remove() method is one step operation as compared to remove in Array find() method – worst case is that we have to traverse entire list, similar to searching an Array Back() – back operation can not be performed straight forward. We first need to goto to head and then traverse array in forward direction 23

Assignment No. 1 Assignment No. 1 Write a program that maintains library shop stock using link list. Provide below facility in program  1) Insert book details  (at specific location) 2) Delete Book Detail (from specific location) 3) Update Book Detail 4) Facility to search book  6) Display all books details  also take below information for particular book  1) Book Author  2) Book Title  3) 4) Book Price   Due Date: 07 – 02 - 2016 24

25