Download presentation
Presentation is loading. Please wait.
1
Data Structures & Algorithms
Instructor Name: Lecture-07
2
Today’s Lecture Linked List (Continue) Linked List Inside the Memory
Linked List Operations Linked List using C++ Example Program 2
3
Linked List Linked List (Continue)
A linked-list is a sequence of data structures which are connected together via links. 3
4
Linked List Linked List Inside Computer Memory 4
5
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
6
Linked List Fitting Node into List 6
7
Linked List Fitting Node into List 7
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
Linked List Understanding add() Method 15
16
Linked List Understanding add() Method 16
17
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
18
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
19
Linked List start() Method void start() { lastCurrentNode = headNode;
// position currentNode and lastCurrentNode at first element void start() { lastCurrentNode = headNode; currentNode = headNode; }; 19
20
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
21
Linked List remove() Method 21
22
Linked List remove() Method 22
23
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
24
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 – 24
25
25
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.