Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Arab Academy for Science , Technology & Maritime Transport

College of Computing and Information Technology

Data Structures
and Algorithms (CS212)
Section 02 :
Sept
Linked Lists
2016
Eng. Mahmoud Osama Radwan
mhmoudko@msn.com – AAST CCIT
Lists
 What is a List ?
A Collection of Elements
 How can we implement Lists ?

1- Arrays
int x[10];
float y[20];

2- Linked Lists
Linked Lists
 So, What is a Linked List ?
Collection of elements connected with each other using
pointers , it can be accessed using the “head” pointer
Linked Lists: Implementation
struct Node {
int data;
Node *next;
};
Linked Lists: How to create ?
 First , Define the Structure of the nodes
struct Node {
int data;
Node *next;
};
Linked Lists: How to create ?
 Secondly , Initialize the list
struct Node {
head
int data;
Node *next;
};

Node *head = NULL;


Linked Lists: How to create ?
 Then , Create Nodes … head
struct Node {
int data; temp
Node *next;
}; 10

Node *head = NULL;


Node *temp = new Node();
temp->data = 10;
temp->next = NULL;
Linked Lists: How to create ?
 Point the head to the newly created node
struct Node {
int data; head
Node *next;
}; 10

Node *head = NULL;


Node *temp = new Node();
temp->data = 10;
temp->next = NULL;
head = temp;
Method to Insert at Head of the List
 First , create the function header: head
void insert(Node *&head ,int x){

} 10
Method to Insert at Head of the List
 Create a Node head
void insert(Node *&head ,int x){
Node *temp = new Node();
temp->data = x; 10
temp->next = NULL;
}

temp

20
Method to Insert at Head of the List
 Adjust the links
void insert(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = head;
}

head
temp

10
20
Method to Insert at Head of the List
 Also adjust the head
void insert(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = head;
head = temp;
}

head

10
temp 20
Method to Insert at End of the List

void insertEnd(Node *&head ,int x){

head

20 10
Method to Insert at End of the List
 Create a Node
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = NULL; temp 30
}

head

20 10
Method to Insert at End of the List
 Find the last element using a pointer p
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = NULL; temp 30
Node *p = head;
}

head p

20 10
Method to Insert at End of the List
 Find the last element using a pointer p
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = NULL; temp 30
Node *p = head;
while(p->next != NULL){
p=p->next;
}
}
head p

20 10
Method to Insert at End of the List
 Now , adjust the last element’s link to point to the new node
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
Node *p = head;
while(p->next != NULL){
p=p->next;
}
p->next = temp;
}
p temp
head

20 10 30
Method to Insert at End of the List
 Is there’s any special cases we should handle ?
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
Node *p = head;
while(p->next != NULL){
p=p->next;
}
p->next = temp;
}
head

20 10 30
Method to Insert at End of the List
 Is there’s any special cases we should handle ?
void insertEnd(Node *&head ,int x){ If list is empty
Node *temp = new Node();
temp->data = x; head
temp->next = NULL;
Node *p = head;
while(p->next != NULL){
p=p->next;
}
p->next = temp; temp 30
}
Method to Insert at End of the List
 If list is empty , just point the head to the new node
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
If(head == NULL) {
head = temp;
return; // stop head
}
Node *p = head; temp 30
while(p->next != NULL){
p=p->next;
}
p->next = temp;
}
Method to Insert at nth position
 First , create the node
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
temp->data = x; temp 40
temp->next = NULL;
}

head

20 10 50 30
Method to Insert at nth position
 Go to before the position you want
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
temp->data = x; temp 40
temp->next = NULL;
Node *p = head;
}

p
head

20 10 50 30
Method to Insert at nth position
 Go to before the position you want
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
temp->data = x; temp 40
temp->next = NULL;
Node *p = head;
for(int i=1; i<n-1 ;i++){
p=p->next;
}
}

p n=3
head

20 10 50 30
Method to Insert at nth position
 Save the nth node’s address using another pointer q
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
temp->data = x; temp 40
temp->next = NULL;
Node *p = head;
for(int i=1; i<n-1 ;i++){
p=p->next;
}
Node *q = p->next;
}
n=3
p q
head

20 10 50 30
Method to Insert at nth position
 Link p to the new node
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
Node *p = head;
for(int i=1; i<n-1 ;i++){
p=p->next;
}
Node *q = p->next; temp 40
p->next = temp;
}
p q
head

20 10 50 30
Method to Insert at nth position
 And then link the new node to q
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
Node *p = head;
for(int i=1; i<n-1 ;i++){
p=p->next;
}
Node *q = p->next; temp 40
p->next = temp;
temp->next = q;
} p q
head

20 10 50 30
Method to Insert at nth position
 Special Cases ? if n=1
void insert(Node *&head ,int x,int n){ “if wants to
Node *temp = new Node();
temp->data = x;
insert at head”
temp->next = NULL;
Node *p = head; n=count(head)
for(int i=1; i<n-1 ;i++){ “if wants to
p=p->next;
} insert at end”
Node *q = p->next;
p->next = temp; n>count(head)
temp->next = q; “position is larger
}
than the list”
head

20 10 40 50 30
Method to Insert at nth position
 Special Cases ? if n=1
void insert(Node *&head ,int x,int n){
if(n==1){
“if wants to
//if he wants to insert at head insert at head”
insertHead(head,x);
return;
} n=count(head)
else if(n == count(head)){
//if he wants to insert at end “if wants to
insertEnd(head,x); insert at end”
return;
}
else if(n > count(head)){ n>count(head)
//if the position is larger than the list
return; “position is larger
} ……………
than the list”
} head

20 10 40 50 30
Method to Insert at nth position
void insert(Node *&head ,int x,int n){
if(n==1){
//if he wants to insert at head
insertHead(head,x);
return;
}
else if(n == count(head)){
//if he wants to insert at end Node *temp = new Node();
insertEnd(head,x);
return;
temp->data = x;
} temp->next = NULL;
else if(n > count(head)){ Node *p = head;
//if position is larger than the list
return;
for(int i=1; i<n-1 ;i++){
} p=p->next;
………. }
}
Node *q = p->next;
p->next = temp;
temp->next = q;
Count elements in the linked list
int count(Node *&head){

head

20 10 40 50 30
Count elements in the linked list
int count(Node *&head){
Node *p = head ;
int counter = 0 ;
while(p != NULL){ //traverse the whole list
counter++;
p=p->next;
}
return counter;
}

head

20 10 40 50 30
Display the linked list
void display(Node *&head){

}
head

20 10 40 50 30
Display the linked list
void display(Node *&head){
if(head == NULL){
printf("Empty Linked List");
return;
}
Node *p = head;
while(p != NULL){
printf("%d ",p->data);
p=p->next;
}
}
head

20 10 40 50 30
Destroy the Linked List
void destroy(Node *&head){

}
head

20 10 40 50 30
Destroy the Linked List
void destroy(Node *&head){
if(head == NULL){
return;
}
Node *p = head;
while(head != NULL){
p = head->next;
delete head;
head = p;
}
}
head

20 10 40 50 30
Section Homework
 Delete the nth element from the linked list

 Max , Min , Avg , Sum Submit it to


mhmoudko@msn.com
(Pseudo-code)
 Search for x

 Concatenate 2 Lists
(Join 2 Linked Lists)
END of Section
Thank You 
Eng. Mahmoud Osama Radwan
mhmoudko@msn.com – AAST CCIT

You might also like