Sie sind auf Seite 1von 62

Linked Lists

Outline
Introduction
Insertion Description
Deletion Description
Basic Node Implementation
Conclusion
Outline
Introduction
Insertion Description
Deletion Description
Basic Node Implementation
Conclusion
Introduction
Definitions
Lists and arrays
Nodes and pointers
Single Linked Lists
Double Linked Lists
Circular Lists
Advantages
Introduction
Definitions
Lists and arrays
Nodes and pointers
Single Linked Lists
Double Linked Lists
Circular Lists
Advantages
Lists and arrays
Lists:
Lists and arrays
Arrays:
{Size of the following array is = 4}

Index 0 1 2 3
Value 44 5 96 3
Whats wrong with Array and Why lists?

Disadvantages of arrays as storage data


structures:
slow searching in unordered array
slow insertion in ordered array
Fixed size
Linked lists solve some of these problems
Linked lists are general purpose storage
data structures and are versatile.
Linked Lists
Each data item is embedded in a link.
Each Link object contains a reference to
the next link in the list of items.
In an array items have a particular
position, identified by its index.
In a list the only way to access an item is
to traverse the list
Is LL an ADT?
Introduction
Definitions
Lists and arrays
Nodes and pointers
Single Linked Lists
Double Linked Lists
Circular Lists
Advantages
Nodes and pointers
Introduction
Definitions
Lists and arrays
Nodes and pointers
Single Linked Lists
Double Linked Lists
Circular Lists
Advantages
Single linked lists
Abstract Data Types
Focus on what the data structure does
Ignore how it does.
ADT is a class considered without regard
to its implementation.
Examples: Stacks and Queues
They can also be implemented using
linked lists as opposed to array in the
previous chapter.
ADT Lists
Also called linear list.
Group of items arranged in a linear order.
Operations supported are : insertion, deletion
and read an item.
List is defined by its interface; the specific
methods used to interact with it.
This can be implemented using arrays or linked
lists.
Sorted Lists (ProrityQ)
As the name suggests data is stored in order.
Find and delete methods are used.
Advantage of sorted list over sorted array is
speed of insertion and its ability to expand to fill
available memory.
Efficiency:
-- Insertion and deletion of arbitrary items require
O(n) comparisons.
The List ADT
A sequence of zero or more elements
A1, A2, A3, AN
N: length of the list
A1: first element
AN: last element
Ai: position i
If N=0, then empty list
Linearly ordered
Ai precedes Ai+1
Ai follows Ai-1
Operations
printList: print the list
makeEmpty: create an empty list
find: locate the position of an object in a list
list: 34,12, 52, 16, 12
find(52) 3
insert: insert an object to a list
insert(x,3) 34, 12, 52, x, 16, 12
remove: delete an element from the list
remove(52) 34, 12, x, 16, 12
findKth: retrieve the element at a certain position
Introduction
Definitions
Lists and arrays
Nodes and pointers
Single Linked Lists
Double Linked Lists
Circular Lists
Advantages
Double Linked Lists
Introduction
Definitions
Lists and arrays
Nodes and pointers
Single Linked Lists
Double Linked Lists
Circular Lists
Advantages
Circular Lists
Introduction
Definitions
Lists and arrays
Nodes and pointers
Single Linked Lists
Double Linked Lists
Circular Lists
Advantages
Advantages
The Linked List advantages are collected because of the
array disadvantages, array disadvantages are:
1. Array Size
2. Memory allocation
3. Insertion and Deletion
Outline
Introduction
Insertion Description
Deletion Description
Basic Node Implementation
Conclusion
Insertion Description
Insertion at the top of the list
Insertion at the end of the list
Insertion in the middle of the list
Insertion Description
Insertion at the top of the list
Insertion at the end of the list
Insertion in the middle of the list
Insertion at the top
Steps:
Create a Node
Set the node data Values
Connect the pointers
Insertion Description
head 48 17 142 //

Follow the previous steps and we get


Step 1 Step 2

Step 3

head 93
Insertion Description
Insertion at the top of the list
Insertion at the end of the list
Insertion in the middle of the list
Insertion at the end
Steps:
Create a Node
Set the node data Values
Connect the pointers
Insertion Description
head 48 17 142 //

Follow the previous steps and we get


Step 1 Step 2

Step 3
Insertion Description
Insertion at the top of the list
Insertion at the end of the list
Insertion in the middle of the list
Insertion in the middle
Steps:
Create a Node
Set the node data Values
Break pointer connection
Re-connect the pointers
Insertion Description

Step 1 Step 2

Step 3

Step 4
Outline
Introduction
Insertion Description
Deletion Description
Basic Node Implementation
Conclusion
Deletion Description
Deleting from the top of the list
Deleting from the end of the list
Deleting from the middle of the list
Deletion Description
Deleting from the top of the list
Deleting from the end of the list
Deleting from the middle of the list
Deleting from the top
Steps
Break the pointer connection
Re-connect the nodes
Delete the node
Deletion Description
head

6 4 17 42

head

6 4 17 42
head

4 17 42
Deletion Description
Deleting from the top of the list
Deleting from the end of the list
Deleting from the middle of the list
Deleting from the end
Steps
Break the pointer connection
Set previous node pointer to NULL
Delete the node
Deletion Description
head

6 4 17 42
head

6 4 17 42

head

6 4 17
Deletion Description
Deleting from the top of the list
Deleting from the end of the list
Deleting from the middle of the list
Deleting from the Middle
Steps
Set previous Node pointer to next node
Break Node pointer connection
Delete the node
Deletion Description
head

4 17 42

head

4 17 42
head

4 42
Outline
Introduction
Insertion Description
Deletion Description
Basic Node Implementation
Conclusion
Basic Node Implementation
The following code is written in C++:

Struct Node
{
int data; //any type of data could be another struct
Node *next; //this is an important piece of code pointer
};
A Simple Linked List Class
We use two classes: Node and List
Declare Node class for the nodes
data: double-type data in this example
next: a pointer to the next node in the list

class Node {
public:
double data; // data
Node* next; // pointer to next
};
A Simple Linked List Class
Declare List, which contains
head: a pointer to the first node in the list.
Since the list is empty initially, head is set to NULL
Operations on List
class List {
public:
List(void) { head = NULL; } // constructor
~List(void); // destructor

bool IsEmpty() { return head == NULL; }


Node* InsertNode(int index, double x);
int FindNode(double x);
int DeleteNode(double x);
void DisplayList(void);
private:
Node* head;
};
A Simple Linked List Class
Operations of List
IsEmpty: determine whether or not the list is
empty
InsertNode: insert a new node at a
particular position
FindNode: find a node with a given value
DeleteNode: delete a node with a given
value
DisplayList: print all the nodes in the list
Inserting a new node
Node* List::InsertNode(int index, double x) { Try to locate indexth
if (index < 0) return NULL; node. If it doesnt
exist, return NULL.
int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;

Node* newNode = new Node;


newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
Inserting a new node
Node* List::InsertNode(int index, double x) {
if (index < 0) return NULL;

int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;

Node* newNode = new Node;


newNode->data = x;
if (index == 0) {
newNode->next = head; Create a new node
head = newNode;
}
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
}
Inserting a new node
Node* List::InsertNode(int index, double x) {
if (index < 0) return NULL;

int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;

Node* newNode = new Node; Insert as first element


newNode->data = x;
if (index == 0) { head
newNode->next = head;
head = newNode;
}
else {
newNode->next = currNode->next; newNode
currNode->next = newNode;
}
return newNode;
}
Inserting a new node
Node* List::InsertNode(int index, double x) {
if (index < 0) return NULL;

int currIndex = 1;
Node* currNode = head;
while (currNode && index > currIndex) {
currNode = currNode->next;
currIndex++;
}
if (index > 0 && currNode == NULL) return NULL;

Node* newNode = new Node;


newNode->data = x;
if (index == 0) {
newNode->next = head;
head = newNode; Insert after currNode
}
currNode
else {
newNode->next = currNode->next;
currNode->next = newNode;
}
return newNode;
} newNode
Finding a node
int FindNode(double x)
Search for a node with the value equal to x in the list.
If such a node is found, return its position. Otherwise, return 0.

int List::FindNode(double x) {
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
currNode = currNode->next;
currIndex++;
}
if (currNode) return currIndex;
return 0;
}
Deleting a node
int List::DeleteNode(double x) {
Try to find the node with its
Node* prevNode = NULL;
value equal to x
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
Deleting a node
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++; prevNode currNode
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
}
return 0;
}
Deleting a node
int List::DeleteNode(double x) {
Node* prevNode = NULL;
Node* currNode = head;
int currIndex = 1;
while (currNode && currNode->data != x) {
prevNode = currNode;
currNode = currNode->next;
currIndex++;
}
if (currNode) {
if (prevNode) {
prevNode->next = currNode->next;
delete currNode;
}
else {
head = currNode->next;
delete currNode;
}
return currIndex;
} head currNode
return 0;
}
Printing all the elements
void DisplayList(void)
Print the data of all the elements
Print the number of the nodes in the list
void List::DisplayList()
{
int num = 0;
Node* currNode = head;
while (currNode != NULL){
cout << currNode->data << endl;
currNode = currNode->next;
num++;
}
cout << "Number of nodes in the list: " << num << endl;
}
Destroying the list
~List(void)
Use the destructor to release all the memory used by the list.
Step through the list and delete each node one by one.

List::~List(void) {
Node* currNode = head, *nextNode = NULL;
while (currNode != NULL)
{
nextNode = currNode->next;
// destroy the current node
delete currNode;
currNode = nextNode;
}
}
6 result

Using List
7
5
Number of nodes in the list: 3
5.0 found
4.5 not found
6
5
int main(void) Number of nodes in the list: 2
{
List list;
list.InsertNode(0, 7.0); // successful
list.InsertNode(1, 5.0); // successful
list.InsertNode(-1, 5.0); // unsuccessful
list.InsertNode(0, 6.0); // successful
list.InsertNode(8, 4.0); // unsuccessful
// print all the elements
list.DisplayList();
if(list.FindNode(5.0) > 0) cout << "5.0 found" << endl;
else cout << "5.0 not found" << endl;
if(list.FindNode(4.5) > 0) cout << "4.5 found" << endl;
else cout << "4.5 not found" << endl;
list.DeleteNode(7.0);
list.DisplayList();
return 0;
}

Das könnte Ihnen auch gefallen