Data Structure
Lecture 2
Linked Lists
Dr. Mohamed Eassa 1
Contents of the lecture
• Linked Lists
•Linked Lists Vs. Arrays
•Linked Lists Structure
•Linked Lists Implementation
Lecture 2 DR. Mohamed Eassa
2
Linked Lists
• A linked list is a list of items, called nodes, in which the order of the nodes is
determined by the address connected by pointer links hence, called a link,
stored in each node.
• A linked list is accessed via a pointer to the first node of the list.
• Each subsequent node is accessed via the link-pointer member stored in the
previous node.
• By convention, the link pointer in the last node of a list is set to null (0) to
mark the end of the list.
Lecture 2 DR. Mohamed Eassa
3
Linked Lists
•Data is stored in a linked list dynamically each node is created as
necessary.
•A node can contain data of any type, including objects of other classes.
•Linked list nodes are normally not stored contiguously in memory.
Lecture 2 DR. Mohamed Eassa
4
Linked Lists: Some Properties
Collection of components (nodes)
• Every node (except last) contains address of the next node
Node components
• Data: stores relevant information
• Next(Link): stores address
Lecture 2 DR. Mohamed Eassa
5
Linked Lists
Head (first)
Address of the first node in the list
Arrow points to node address
Stored in node
Down arrow in last node indicates NULL link field
Lecture 2 DR. Mohamed Eassa
6
Linked Lists and Arrays
Lists of data can be stored in arrays, but linked lists provide several advantages:
1. A linked list is appropriate when the number elements is unpredictable. But
array, cannot be altered, because the array size is fixed at compile time .
2. Linked lists are dynamic, so the length of a list can increase or decrease as
necessary.
3. A linked list allows efficient insertion operations anywhere in the list. But array
consuming a lot of time because of element must be shifted appropriately.
4. The elements of an array are stored contiguously in memory. This allows
immediate access to any array element.
Lecture 2 DR. Mohamed Eassa
7
Linked Lists and Arrays
5. Linked lists don’t afford "direct access" to their elements. So accessing
individual elements in a linked list can be considerably more expensive
than accessing individual elements in an array.
6. Note that the selection of a data structure is typically based on the
performance of specific operations used by a program and the order in
which the data items are maintained in the data structure.
7. For example, it is typically more efficient to insert an item in
a sorted linked list than a sorted array.
Lecture 2 DR. Mohamed Eassa
8
Linked Lists and Arrays
5. Linked lists don’t afford "direct access" to their elements. So accessing
individual elements in a linked list can be considerably more expensive
than accessing individual elements in an array.
6. Note that the selection of a data structure is typically based on the
performance of specific operations used by a program and the order in
which the data items are maintained in the data structure.
7. For example, it is typically more efficient to insert an item in
a sorted linked list than a sorted array.
Lecture 2 DR. Mohamed Eassa
9
Linked Lists Implementation
The following program uses a List class to manipulate a list of integer values.
The program provides five options:
1) Insert a value at the beginning (first) of the list.
2) insert a value at the position (insert before) of the list.
3) insert a value at the end (Append) of the list.
4) delete a value from the beginning (first) of the list.
5) delete a value at a position of the list.
6) Search for a value from the list. 10
Lecture 2 DR. Mohamed Eassa
Linked Lists Implementation
Head Null
Data Next Data Data Data Data
Head Null
10 20 30 40 50 Null
Lecture 2 DR. Mohamed Eassa
11
The following program uses a List class to manipulate a list of integer values.
The program provides five options:
1) Insert a value at the beginning (first) of the list.
2) insert a value at the position (insert before) of the list.
3) insert a value at the end (Append) of the list.
4) delete a value from the beginning (first) of the list.
5) delete a value at a position of the list.
6) Search for a value from the list. 12
Lecture 2 DR. Mohamed Eassa
1- Insert a value at the Beginning (first) of the list
If the list is empty or filled
#include <iostream> void insertFirst (int val)
using namespace std; { Head
If (isempty())
class node {
{ node* newnode = new node();
10 / Null
public: newnode -> data = val;
int data; newnode -> next = Null;
node* next; head = newnode;
}; } Head
class linkedlist else
{ {
public: node* newnode = new node(); 10 / 20 /
node* head = NULL; newnode -> data = val;
newnode -> next = head; Head
bool isempty() head = newnode;
{ }
return (head==Null); } 10 / 20 /
} }
Lecture 2 DR. Mohamed Eassa
13
Traversing
Head Head Null
10 20 30 40 50 Null
Head = head -> next;
Head Temp Temp Temp Temp Temp
Null
10 20 30 40 50 Null
Node* temp = Head; Temp = Null
Temp = Temp-> next;
Lecture 2 DR. Mohamed Eassa
14
Display by traversing
void display ()
{
node* temp = head;
while(temp!= NULL)
{
cout << temp->data<<“ ”;
temp = temp -> next;
}
}
Lecture 2 DR. Mohamed Eassa
15
Insert a value at the Position of the list
(insert before)
Head Temp Null
10 20 30 40 50 Null
void InsertPosition(int item , int val) 2 1
{ 35
node* newnode= new node();
newnode -> data = val;
node* temp = head;
while (temp!= NULL && temp -> next ->data != item)
{
temp = temp-> next;
}
newnode -> next = temp-> next; 1
temp-> next = newnode; 2
}
Lecture 2 DR. Mohamed Eassa
16
Insert a value at the end (Last) of the list
(Append)
Head Temp
void insertLast(int val)
{ 10 20 30 40 50 Null
If (isempty())
insertFirst (val)
else
35 Null
{
node* temp = head;
while(temp -> next != NULL)
{
temp = temp -> next;
} while(temp!= NULL) خارج
node * newnode = new node ();
newnode-> data = val; while(temp -> next != NULL)
temp->next = newnode;
newnode->next = NULL;
}
} 17
Lecture 2 DR. Mohamed Eassa
Delete a value from the beginning (first) of the list
Head
10 20 30 40 50 Null
Temp
void deleteFirst()
{
if(isempty())
cout<<"Already Empty \n";
else
{
node* temp = head;
head = head-> next; (or) head = temp-> next;
delete temp;
}
}
Lecture 2 DR. Mohamed Eassa
18
Delete a value from the position of the list
Delete (30)
Head delptr prev delptr
prev next
Null 10 20 30 40 50 Null
else
void delete (int item) {
{ node* prev= NULL;
if(isempty()) node* delptr= head;
cout<<"Already Empty \n"; while (delptr->data != item)
If (head-> data== item) {
{ prev = delptr;
node* temp= head; delptr = delptr-> next;
head = head->next; //(or) head = temp-> next; }
delete temp; Prev->next = delptr->next;
} Delete delptr;
}
Lecture 2 DR. Mohamed Eassa } 19
Searching
Head Temp Temp Temp Temp Temp
Null
10 20 30 40 50 Null
bool search (int key)
{
bool found = false;
node* temp = head;
while(temp!= NULL)
{
If (temp -> data == key)
found = true;
temp = temp->next;
}
return found;
} 20
Lecture 2 DR. Mohamed Eassa
Main function
Int main()
{
linkedlist list;
if(list.isempty())
cout<< “the list is empty” \n;
int item;
cout << “enter item to in the list” \n;
cin >> item;
list. insertFirst(item);
list.display();
} 21
Lecture 2 DR. Mohamed Eassa