0% found this document useful (0 votes)
18 views62 pages

Lecture 8

The document provides an overview of linked lists, detailing their structure, operations, and implementation in C++. It explains the properties of linked lists, how to create and insert nodes, and includes C++ code for a Node class and a List class. The document emphasizes the methods for managing nodes within the linked list, such as adding new nodes and maintaining pointers.

Uploaded by

Rana Modified
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views62 pages

Lecture 8

The document provides an overview of linked lists, detailing their structure, operations, and implementation in C++. It explains the properties of linked lists, how to create and insert nodes, and includes C++ code for a Node class and a List class. The document emphasizes the methods for managing nodes within the linked list, such as adding new nodes and maintaining pointers.

Uploaded by

Rana Modified
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 62

Dr.

Ghulam Farooque
Assistant Professor
Department: CS & IT
The University of Lahore
Email: [email protected]
Linked List
 Actual picture in memory:
1051 6
current 1052 1063
1053 1063
1054 2
head 1055 1051
1056
2 6 8 7 1 1057 7
1058 1060
current 1059
1060 1
1061 0
head 1062 1054
1063 8
1064 1057
1065
Linked List
Linked Lists: some Properties

head 2000 2800 1500 3600

2000 17 2800 92 1500 63 3600 45 /

info link info link info link info

This linked list has four nodes. The address of the first node is stored
in the pointer head.

Each node has two components: info to store the info, and link, to
store the address of the next node.
Linked List Operations
The linked list data structure provides operations to work
on the nodes inside the list.
The first operation we are going to discuss here is to create
a new node in the memory.
The Add(9) is used to create a new node in the memory at
the current position to hold ‘9’.
 add(9): Create a new node in memory to hold ‘9’
Node* newNode = new Node(9);

newNode 9

“Call the constructor of the Node class and pass it ‘9’ as a


parameter.
Linked List Operations

After constructing the object in memory, it will give the


starting memory address of the object. That address will be
stored in the pointer variable newNode.

To create an object of int type in the same manner, we can


write as:

int * i = new int ;


Linked List Operations
 Now after the node has been created, how the node is fit into the chain of the
linked list.
 Node* newNode = new Node(9);

newNode 9
 Insertion of new Node into the linked list

head

2 6 8 7 1 size=5 6

current 2
1

3
9

newNode
Linked List Operations
This insertion operation is performed in a step-by-step
fashion.

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.
C++ Code for Linked List
The Node class

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;
};
C++ Code for Linked List
The Node class

 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;
};
C++ Code for Linked List
The Node class

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;
};
C++ Code for Linked List
The Node class

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;
};
C++ Code for Linked List
The Node class

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;
};
C++ Code for Linked List
The Node class

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;
};
C++ Code for Linked List
The Node class

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;
};
C++ Code for Linked List
The Node class

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;
};
C++ Code for Linked List
The Node class

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;
};
C++ Code for Linked List
The Node class

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;
};
Linked List Operations
 When we write class 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. This is usually the way that we write two files for one
class. But this is not mandatory.

 In the code given above, we have only one file .cpp, instead of
separating into two files. As the class methods are very small,
so their code is written within the body of the class.
Linked List Operations

 The very first public function given at the top is get(). We


have written its code within the class Node. It returns back
the value of the variable object i.e. of the type of int.

 The second method in the above-mentioned class is set() that


accepts a parameter of type int while returning back nothing.
The accepted parameter is assigned to the internal data
member object.
Linked List Operations
 The next method is getNext() which returns a pointer to an
object of type Node lying somewhere in the memory. It
returns nextNode i.e. a pointer to an object of type Node. As
discussed above, nextNode contains the address of next node
in the linked list.

 The last method of the class is setNext() that accepts a pointer


of type Node, further assigned to nextNode data member of
the object. This method is used to connect the next node of the
linked list with the current object. It is passed an address of
the next node in the linked list.
C++ Code for Linked List

#include "Node.cpp"

class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
C++ Code for Linked List

 #include "Node.cpp"

class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
C++ Code for Linked List

#include "Node.cpp"

class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
C++ Code for Linked List

#include "Node.cpp"

class List {
 public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
C++ Code for Linked List

#include "Node.cpp"

class List {
public:
 // Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
C++ Code for Linked List

#include "Node.cpp"

class List {
public:
// Constructor
List() {
 headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
C++ Code for Linked List

#include "Node.cpp"

class List {
public:
// Constructor
List() {
headNode = new Node();
 headNode->setNext(NULL);
currentNode = NULL;
size = 0;
};
C++ Code for Linked List

#include "Node.cpp"

class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
 currentNode = NULL;
size = 0;
};
C++ Code for Linked List

#include "Node.cpp"

class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
 size = 0;
};
C++ Code for Linked List

#include "Node.cpp"

class List {
public:
// Constructor
List() {
headNode = new Node();
headNode->setNext(NULL);
currentNode = NULL;
size = 0;
 };
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
 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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
 currentNode = newNode;
}
size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
 size++;
};
C++ Code for Linked List
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;
currentNode = newNode;
}
size++;

};
Building a Linked List
List list; headNode size=0
Building a Linked List
List list; headNode size=0

currentNode

headNode 2 size=1
list.add(2);
lastcurrentNode
Building a Linked List
List list; headNode size=0

currentNode

headNode 2 size=1
list.add(2);
lastcurrentNode

currentNode

list.add(6); headNode 2 6 size=2

lastcurrentNode
Building a Linked List

List.add(8); list.add(7); list.add(1);

currentNode

headNode 2 6 8 7 1 size=5

lastcurrentNode
C++ Code for Linked List

int get() {
if (currentNode != NULL)
return currentNode->get();
};
C++ Code for Linked List
bool next() {
if (currentNode == NULL) return false;

lastCurrentNode = currentNode;
currentNode = currentNode->getNext();
if (currentNode == NULL || size == 0)
return false;
else
return true;
};
C++ Code for Linked List
// position current before the first
// list element
void start() {
lastCurrentNode = headNode;
currentNode = headNode;
};
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
lastCurrentNode->setNext(currentNode-
>getNext());
delete currentNode;
currentNode = lastCurrentNode->getNext();
size--;
}
}; currentNode
headNode

2 6 8 7 1 size=5

lastcurrentNode
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
1 lastCurrentNode->setNext(currentNode-
>getNext());
delete currentNode;
currentNode = lastCurrentNode->getNext();
size--;
}
}; currentNode
headNode
1

2 6 8 7 1 size=5

lastcurrentNode
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
1 lastCurrentNode->setNext(currentNode-
>getNext());
2
delete currentNode;
currentNode = lastCurrentNode->getNext();
size--;
}
}; currentNode
headNode
1

2 8 7 1 size=5
2
lastcurrentNode
C++ Code for Linked List
void remove() {
if( currentNode != NULL &&
currentNode != headNode) {
1 lastCurrentNode->setNext(currentNode-
>getNext());
2
3
delete currentNode;
4
currentNode = lastCurrentNode->getNext();
size--;
} 3
}; currentNode
headNode
1 4
2 8 7 1 size=4
2
lastcurrentNode
C++ Code for Linked List
int length()
{
return size;
};

private:
int size;
Node *headNode;
Node *currentNode, *lastCurrentNode;
Example of List Usage
#include <iostream>
#include "List.cpp"

int main()
{
List list;

list.add(5); list.add(13); list.add(4);


list.add(8); list.add(24); list.add(48);
list.add(12);
list.start();
while (list.next())
cout << "List Element: "<<
list.get()<<endl;
}
Analysis of Linked List
 add
• we simply insert the new node after the current
node. So add is a one-step operation.
 remove
 remove is also a one-step operation
 find
 worst-case: may have to search the entire list
 back
 moving the current pointer back one node
requires traversing the list from the start until
the node whose next pointer points to current
node.
Advantages of Linked List

1. Insertions and Deletions can be done easily.


2. It does not need the movement of elements for insertion
and deletion.
3. Space is not wasted as we can get space according to our
requirements.
4. Its size is not fixed.
5. It can be extended or reduced according to requirements.
6. Elements may or may not be stored in consecutive
memory available, even then we can store the data in
computer.
AL 61
Disadvantages of Linked List

1. It requires more space as pointers are also stored with


information.
2. A different amount of time is required to access each
element.
3. If we have to go to a particular element, we have to go
through all the elements that come before it.
4. we can not traverse it from the last & only from the
beginning.
5. It is not easy to sort the elements stored in the linear
linked list.
AL 62

You might also like