1 CSE 2341 Object Oriented Programming with C++ Note Set #18.

Slides:



Advertisements
Similar presentations
Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.
Advertisements

Chapter 17 Linked Lists.
Linked Lists.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Doubly-linked list library.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Linked List
Linked Lists
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Linked Lists A linked list is a series of connected nodes Each node contains at least –A piece of data (any type) –Pointer to the next node in the list.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
1 CSC 211 Data Structures Lecture 10 Dr. Iftikhar Azim Niaz 1.
C++ is Fun – Part 14 at Turbine/Warner Bros.! Russell Hanson.
Starting Out with C++, 3 rd Edition 1 Chapter 17 Linked Lists.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
1 CSE 2341 Object Oriented Programming with C++ Note Set #21.
Algorithms and Data Structures
Binary Search Tree. Tree  A nonlinear data structure consisting of nodes, each of which contains data and pointers to other nodes.  Each node has only.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Linked Lists. Array List Issues Painful insert/remove at start/middle.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
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.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
Binary Search Trees. 2 Overview Recursive linked list ops Binary Trees.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
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.
Programming Circular Linked List.
UNIT – I Linked Lists.
Lists CS 3358.
List ADT & Linked Lists.
Linked lists.
classes and objects review
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
CSCI 3333 Data Structures Linked Lists.
Data Structures and Algorithms IT12112
Chapter 18: Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
COSC 1030 Section 8 List.
Chapter 17: Linked Lists.
Lecture 14 Linked Lists CSE /26/2018.
CS148 Introduction to Programming II
Linked lists.
Linked List Insert After
Linked Lists.
Sequences 08/30/17 08/30/17 Unit III. Linked Lists 1.
Presentation transcript:

1 CSE 2341 Object Oriented Programming with C++ Note Set #18

2 Overview Dynamic Data structures with linked lists

3 Linked List Series of connected nodes Can grow/shrink at runtime –dynamic allocation of nodes allows for this Data Null Head

4 Linked List Node Data struct ListNode { double data; ListNode* next; }; ListNode is a self referential data structure.

5 Basic Linked List Operations Basic Operations of LL –appending a node –traversing the list –inserting a node –deleting a node –destroying the list

6 Class NumberList class NumberList { private: struct ListNode { double value; ListNode* next; }; ListNode* head; private: NumberList() {head = NULL;} ~NumberList(); void appendNode(double); void insertNode(double); void deleteNode(double); void displayList(); };

7 AppendNode void NumberList::appendNode(double num) { ListNode* newNode, *nodePtr; newNode = new ListNode; newNode -> value = num; newNode -> next = NULL; if(!head) //If there aren’t any nodes in list head = newNode; else { nodePtr = head; while(nodePtr->next) nodePtr = nodePtr->next; nodePtr -> next = newNode; }

8 Append 5 34Null Head newNode NodePtr

9 Append 5 34Null Head newNode NodePtr

10 Append 5 34 Null Head newNode NodePtr

11 Display List void NumberList::DisplayList() { ListNode* nodePtr; nodePtr = head; while(nodePtr) { cout value << endl; nodePtr = nodePtr->next; }

12 Display 534 Null Head NodePtr 3

13 Display 534 Null Head NodePtr 3434

14 Display 534 Null Head NodePtr

15 Display 534 Null Head NodePtr

16 Insert A Node void NumberList::insertNode(double num) { ListNode *newNode, *nodePtr, *previousNode; newNode = new ListNode; newNode -> value = num; if(!head) { head = newNode; newNode->next = NULL; } else { nodePtr = head; previousNode = NULL;

17 Insert A Node //Continued from previous slide while(nodePtr != NULL && nodePtr->value < num) { previousNode = nodePtr; nodePtr = nodePtr->next; } if(previousNode == NULL) //if insert as first { head = newNode; newNode -> next = nodePtr; } else { previousNode -> next = newNode; newNode -> next = nodePtr; }

18 Insert 534 Null Head NodePtr 4.5 newNode PreviousNode Null

19 Insert 534 Null Head NodePtr 4.5 newNode PreviousNode

20 Insert 534 Null Head NodePtr 4.5 newNode PreviousNode

21 Insert 534 Null Head NodePtr 4.5 newNode PreviousNode

22 Insert 534 Null Head NodePtr 4.5 newNode PreviousNode

23 Insert 534 Null Head NodePtr 4.5 newNode PreviousNode

24 Insert 534 Null Head NodePtr 4.5 newNode PreviousNode

25 Deleting A Node void NumberList::deleteNode(double num) { ListNode *nodePtr, *previousNode; if(!head) return; if(head->value == num) { nodePtr = head->next; delete head; head = nodePtr; } else { nodePtr = head; while(nodePtr != NULL && nodePtr->value != num) { previousNode = nodePtr; nodePtr = nodePtr->next; } if(nodePtr) { previousNode->next = nodePtr -> next; delete nodePtr; }

26 Delete 534 Null Head NodePtr PreviousNode Null num 4

27 Delete 534 Null Head NodePtr num 4 PreviousNode

28 Delete 534 Null Head NodePtr num 4 PreviousNode

29 Delete 534 Null Head NodePtr num 4 PreviousNode

30 Fini ?