Professional Documents
Culture Documents
CS212 Sep2016 03 DoubleLinkedLists
CS212 Sep2016 03 DoubleLinkedLists
CS212 Sep2016 03 DoubleLinkedLists
Data Structures
and Algorithms (CS212)
Section 03 :
Sept
Double Linked Lists
2016
Eng. Mahmoud Osama Radwan
mhmoudko@msn.com – AAST CCIT
Double Linked List
struct Node {
int data;
Node *next;
Node *prev;
};
Method to Insert at Head of the List
First , create the function header:
void insert(Node *&head ,int x){
head
10 20 30 40
Method to Insert at Head of the List
Then, Create the node
void insert(Node *&head ,int x){ temp
Node *temp = new Node();
temp->data=x; 50
temp->next=NULL;
temp->prev=NULL;
}
head
10 20 30 40
Method to Insert at Head of the List
Then, adjust the links
void insert(Node *&head ,int x){
Node *temp = new Node();
temp->data=x;
temp->next=NULL;
temp->prev=NULL;
head->prev=temp;
}
temp
50
head
10 20 30 40
Method to Insert at Head of the List
Then, adjust the links
void insert(Node *&head ,int x){
Node *temp = new Node();
temp->data=x;
temp->next=NULL;
temp->prev=NULL;
head->prev=temp;
temp->next=head;
}
temp
50
head
10 20 30 40
Method to Insert at Head of the List
Adjust the head
void insert(Node *&head ,int x){
Node *temp = new Node();
temp->data=x;
temp->next=NULL;
temp->prev=NULL;
head->prev=temp;
temp->next=head;
head = temp;
}
head
temp
50 10 20 30 40
Method to Insert at Head of the List
If list is empty
void insert(Node *&head ,int x){
Node *temp = new Node();
temp->data=x;
temp->next=NULL;
temp->prev=NULL; temp
head->prev=temp;
temp->next=head; 50
head = temp;
}
head
Method to Insert at Head of the List
If list is empty
void insert(Node *&head ,int x){
Node *temp = new Node();
temp->data=x;
temp->next=NULL; head
temp->prev=NULL; temp
if(head==NULL){ 50
head = temp;
return; //stop
}
head->prev=temp;
temp->next=head;
head = temp;
}
Method to Insert at End of the List
head
10 20 30 40
Method to Insert at End of the List
Create the node
void insertEnd(Node *&head ,int x){ temp
Node *temp = new Node();
temp->data = x; 50
temp->next = NULL;
temp->prev = NULL;
}
head
10 20 30 40
Method to Insert at End of the List
Go to the last node
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
Node *p = head;
}
temp
50
p
head
10 20 30 40
Method to Insert at End of the List
Go to the last node
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
Node *p = head;
while(p->next != NULL){
p=p->next; temp
}
} 50
p
head
10 20 30 40
Method to Insert at End of the List
Then adjust the links
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
Node *p = head;
while(p->next != NULL){
p=p->next; temp
}
p->next=temp; 50
}
p
head
10 20 30 40
Method to Insert at End of the List
Then adjust the links
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
Node *p = head;
while(p->next != NULL){
p=p->next;
} temp
p->next=temp;
temp->prev=p; 50
}
p
head
10 20 30 40
Method to Insert at End of the List
If list is empty ?
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
temp->data = x;
temp->next = NULL; temp
temp->prev = NULL; 50
Node *p = head;
while(p->next != NULL){
p=p->next;
}
p->next=temp;
temp->prev=p;
head
}
Method to Insert at End of the List
If list is empty ?
void insertEnd(Node *&head ,int x){
Node *temp = new Node();
head
temp->data = x;
temp->next = NULL; temp
temp->prev = NULL; 50
if(head==NULL){
head=temp;
return;
}
Node *p = head;
while(p->next != NULL){
p=p->next;
}
p->next=temp;
temp->prev=p;
}
Method to Insert at nth position
void insert(Node *&head ,int x,int n){
head
10 20 30 40
n=3
Method to Insert at nth position
Create the node
void insert(Node *&head ,int x,int n){
Node *temp = new Node(); temp
temp->data = x;
temp->next = NULL; 50
temp->prev = NULL;
}
head
10 20 30 40
n=3
Stop before the desired position Method to Insert at nth position
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
temp->data = x;
temp->next = NULL; temp
temp->prev = NULL;
50
Node *p = head;
}
p
head
10 20 30 40
n=3
Stop before the desired position Method to Insert at nth position
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
temp->data = x;
temp->next = NULL; temp
temp->prev = NULL;
50
Node *p = head;
for(int i=1;i<n-1;i++){
p=p->next;
}
}
p
head
10 20 30 40
n=3
Save the next node Method to Insert at nth position
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
temp->data = x;
temp->next = NULL; temp
temp->prev = NULL;
50
Node *p = head;
for(int i=1;i<n-1;i++){
p=p->next;
}
Node *q = p->next;
}
p q
head
10 20 30 40
n=3
Adjust the links Method to Insert at nth position
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
Node *p = head;
for(int i=1;i<n-1;i++){
p=p->next; temp
}
Node *q = p->next; 50
p->next=temp;
}
p q
head
10 20 30 40
n=3
Adjust the links Method to Insert at nth position
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
Node *p = head;
for(int i=1;i<n-1;i++){
p=p->next; temp
}
Node *q = p->next; 50
p->next=temp;
temp->prev=p;
} p q
head
10 20 30 40
n=3
Adjust the links
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
Method to Insert at nth position
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
Node *p = head;
for(int i=1;i<n-1;i++){
p=p->next;
}
Node *q = p->next; temp
p->next=temp;
50
temp->prev=p;
temp->next=q;
} q
p
head
10 20 30 40
n=3
Adjust the links
void insert(Node *&head ,int x,int n){
Node *temp = new Node();
Method to Insert at nth position
temp->data = x;
temp->next = NULL;
temp->prev = NULL;
Node *p = head;
for(int i=1;i<n-1;i++){
p=p->next;
}
Node *q = p->next; temp
p->next=temp;
50
temp->prev=p;
temp->next=q;
q->prev=temp; q
p
} head
10 20 30 40
n=3
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;
Node *temp = new Node();
} temp->data = x;
else if(n == count(head)){ temp->next = NULL;
//if he wants to insert at end
insertEnd(head,x);
temp->prev = NULL;
return; Node *p = head;
} for(int i=1;i<n-1;i++){
else if(n > count(head)){
//if position is larger than the list
p=p->next;
return; }
} Node *q = p->next;
……….
}
p->next=temp;
temp->prev=p;
temp->next=q;
q->prev=temp;
Count elements in the linked list
int count(Node *&head){
head
10 20 30 40
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;
} Same as the Single LL
return counter;
}
head
10 20 30 40
Display the linked list
void display(Node *&head){
}
head
10 20 30 40
Display the linked list
void display(Node *&head){
if(head == NULL){
printf("Empty Linked List");
return;
}
Node *p = head; Same as the Single LL
while(p != NULL){
printf("%d ",p->data);
p=p->next;
}
}
head
10 20 30 40
Section Homework
Delete the nth element