SlideShare ist ein Scribd-Unternehmen logo
1 von 74
CS261
DATA STRUCTURES & ALGORITHMS
(WEEK-4)
LECTURE-7 & 8
INTRODUCTION TO DATA STRUCTURES &
ALGORITHMS
Lecturer
Azka Aziz
Azka.a@scocs.edu.pk
Data structures & Algorithms
Lecture#07 Circular Link List
Course contents
Circular Link List
Data Structure (Declaration, Initialization, Updating)
Insert After a specific location
Insert a value before a specific location
Insert a value as a first item of list
Delete a value from a link list
Circular Link list
A circular linked list is a type
of linked list in which last
node of the list points to
start node of the list instead
of NULL.
data data
data
data
data
data
data
start
next next
next
next
next
next
next
Circular Link list
Circular linked list contains all the operations that a simple linked list has
The only difference is that ‘next’ pointer of last node points to first (start) node
of list
This small change gives the list a circular form affecting the implementation of
few operations in the list including add_node(), insert_node(), append_node(),
delete_node() etc
Except when list is empty, no node pointer points to NULL
Circular Link list … node ADT
Circular Link list … list ADT
Circular Linked list … Operations
Operation Description Pseudocode
Clist(); A constructor that sets
‘start’ pointer to NULL
Assign NULL to ‘start’
int size(); Calculate and return the
number of elements in list
 If ‘start’ points to NULL, return 0
 Otherwise,
 Create a variable ‘count’ and assign 1 to it
 Create a node pointer ‘temp’ and point it
to next of ‘start’
 Iterate through all list items until ‘temp’
points to ‘start’ again; at that point current
value of ‘count’ will be returned
 During every iteration ‘count’ will be
incremented by 1 and ‘temp’ will point to
‘next’ pointer of node currently it is
pointing to
Circular Linked list … size()
Circular Linked list … Operations
Operation Description Pseudocode
void print_all(); Prints all nodes’ data
values
 Assign ‘start’ to a Node pointer named ‘temp’
 Traverse through the list until either ‘temp’
reaches to the ‘start’ of CList again
 During every iteration print the ‘data’ value
Circular Linked list … print_all()
Circular Linked list … Operations
Operation Description Pseudocode
Node* search(int key); Searches through the list
for a Node with given value
and returns its reference
 Create a new node pointer ‘temp’ and point it
to ‘start’
 Traverse through the list until either ‘temp’
reaches to the Node with value equal to ‘key’ or
it reaches to the ‘start’ of list again
 If ‘temp’ reaches to the ‘start’ of CList again,
return NULL
 Otherwise, return the node pointer ‘temp’
Circular Linked list … search()
Circular Linked list … Operations
Operation Description Pseudocode
void add_node(int); Create a node with given
value and add it
immediately after the start
of the list
 Create a node with given ‘key’ as its ‘data’
pointed by a node pointer ‘temp’ and set its
‘next’ to itself
 If size() of CList is equal to 0, assign ‘temp’ to
‘start’
 Otherwise,
 Set ‘next’ of ‘temp’ to ‘next’ of ‘start’
 Assign ‘temp’ to ‘next’ of ‘start’
Circular Linked list … add_node()
Circular Linked list … Operations
Operation Description Pseudocode
void append_node(int); Create a node with given
value and append (add to
end) it to the list
 Create a node with given ‘key’ as its ‘data’
pointed by a node pointer ‘n’ and set its ‘next’
to itself
 Create a node pointer ‘temp’ and point it to
‘start’ of Clist
 If size() of CList is equal to 0,
 Assign ‘n’ to ‘start’, Exit
 Otherwise,
 Traverse ‘temp’ to the last node of Clist
 Set ‘next’ of ‘n’ to ‘start’
 Set ‘next’ of ‘temp’ to ‘n’
 Exit
Circular Linked list … append_node()
Circular Linked list … Operations
Operation Description Pseudocode
void insert (int after, int
key);
Insert a node with given
value in linked list
immediately after a specific
node already present in the
list
 Create a new node pointer ‘temp’ and assign
‘start’ to it
 If ‘start’ is pointing to NULL, Exit
 Move to next nodes until either ‘temp’ reaches
to the node with ‘data’ value equal to ‘after’ or
it reaches to ‘start’ of CList again
 If ‘temp’ is pointing to ‘start’ of CList again,
 exit
 Else,
 Create a new node in memory with ‘key’
as data and point it by a pointer ‘n’
 Set ‘next’ of ‘n’ to ‘next’ of ‘temp’
 Set ‘next’ of ‘temp’ to ‘n’
 Exit
Circular Linked list … insert()
Circular Linked list … Operations
Operation Description Pseudocode
void delete_node
(int key);
Delete the node with
given value from the list
 If size() of CList is 0, Exit
 Create a node pointer ‘temp’ and point it to ‘start’
 Iterate ‘temp’ to next nodes until either ‘temp’ reaches to a
node whose ‘next’ node has ‘data’ equal to ‘key’ or its ‘next’
node is ‘start’
 If ‘next’ of ‘temp’ points to ‘start’
 If ‘data’ of ‘start’ is not equal to ‘key’, Exit
 Else if size() of CList is equal to 1
 Assign NULL to ‘start’, Exit
 Otherwise
 Move ‘start’ to next node in list
 Assign ‘next’ of ‘temp’ to a node pointer, say ‘x’
 Set ‘next’ of ‘temp’ to ‘start’
 Delete ‘x’
 Exit
 Else,
 Delete ‘next’ pointer of ‘temp’ and adjust pointers
accordingly
 Exit
Circular Linked list … delete_node()
Structure of Node in CLL
struct node
{
int data;
struct node*link;
};
Main Operations on CLL
#include<iostream>
#include<stdlib.h>
#define null 0
#define true 1
using namespace std;
struct node{
int data;
struct node*link;
};
struct node*ptfirst=null;
void insert(int value);
void display();
void insert_first(int value);
void insert_after(int position,int value);
void insert_before(int position,int value);
void Delete(int value);
int main(){
int opt_no,value,position;
while(true){
cout<<"Enter 1 for insert"<<endl;
cout<<"Enter 2 for Display"<<endl;
cout<<"Enter 3 for insert first"<<endl;
cout<<"Enter 4 for insert after"<<endl;
cout<<"Enter 5 for insert before"<<endl;
cout<<"Enter 6 for delete"<<endl;
cout<<"Enter 7 for Exit"<<endl<<endl;
cout<<"Now Enter Option Number: ";
cin>>opt_no;
switch (opt_no)
{
case 1:
cout<<"insert value: ";
cin>>value;
cout<<endl;
insert(value);
break;
case 2:
display();
break;
case 3:
cout<<"insert value: ";
cin>>value;
cout<<endl;
insert_first(value);
break;
case 4:
cout<<"enter position: ";
cin>>position;
cout<<"insert value: ";
cin>>value;
cout<<endl;
insert_after(position,value);
break;
case 5:
cout<<"enter position: ";
cin>>position;
cout<<"insert value: ";
cin>>value;
cout<<endl;
insert_before(position,value);
break;
case 6:
cout<<"insert value: ";
cin>>value;
cout<<endl;
Delete(value);
break;
case 7:
exit(1);
default:
cout<<"I N V A L I D ___ O P T I O N"<<endl;
}
Insert a Value in CLL
void insert(int value)
{
struct node*temp;
if(ptfirst==null){
temp=(struct node*)malloc(sizeof(node));
ptfirst=temp;
}
else{
temp=ptfirst;
do{
temp=temp->link;
}while(temp->link!=ptfirst);
temp->link=(struct node*)malloc(sizeof(node));
temp=temp->link;
}
temp->link=ptfirst;
temp->data=value;
return;
}
Delete a value from a CLL
void Delete(int value){
if(ptfirst==null){
cout<<"No value in the list to delete."<<endl;
return;
}
struct node*temp;
struct node*prev;
temp=ptfirst;
prev=temp;
do{
if(temp->data==value){
if(temp==ptfirst && temp->link==ptfirst){
delete temp;
ptfirst=null;
return;
}
else if(temp==ptfirst){
do{
prev=prev->link;
}
while(prev->link!=ptfirst);
prev->link=temp->link;
ptfirst=temp->link;
delete temp;
return;
}
else{
prev->link=temp->link;
delete temp;
return;
}
}
else{
prev=temp;
temp=temp->link;
}
}
while(temp!=ptfirst);
return;
}
Display CLL
void display()
{
struct node*temp;
if(ptfirst==null){
cout<<"List is empty"<<endl;
return;
}
temp=ptfirst;
do{
cout<<temp->data<<endl;
temp=temp->link;
}
while(temp!=ptfirst);
return;
}
Insert after
void insert_after(int position,int value){
if(position<=0 || ptfirst==null){
cout<<"Invalid Position"<<endl;
return;
}
struct node*temp;
struct node*q;
temp=ptfirst;
for(int i=1; i<position; i++){
temp=temp->link;
if(temp==ptfirst){
cout<<"Invalid Position OR Position does not exists"<<endl;
return;
}
}
q=(struct node*)malloc(sizeof(struct node));
q->data=value;
q->link=temp->link;
temp->link=q;
return;
Insert Before
void insert_before(int position,int value){
struct node *temp;
if(position<=0){
cout<<"invalid position";
return;
}
else if(position==1){
struct node*temp;
struct node *q;
q=(struct node*)malloc(sizeof(node));
q->data=value;
q->link=ptfirst;
temp=ptfirst;
do{
temp=temp->link;
}
while(temp->link!=ptfirst);
temp->link=q;
ptfirst=q;
return;
}
else if(position>1){
temp=ptfirst;
for(int i=1;i<position-1;i++){
temp=temp->link;
if(temp==ptfirst){
cout<<"invalid position"<<endl;
return;
}
}
struct node *q;
q=(struct node*)malloc(sizeof(node));
q->data=value;
q->link=temp->link;
temp->link=q;
return;
}
}
Insert value as first value
void insert_first(int value)
{
struct node*temp;
struct node *q;
q=(struct node*)malloc(sizeof(node));
q->data=value;
q->link=ptfirst;
temp=ptfirst;
do{
temp=temp->link;
}
while(temp->link!=ptfirst);
temp->link=q;
ptfirst=q;
return;
}
Data structures & Algorithms
Lecture#08 Double Circular Link List
Course contents
 Double Circular Link List
Data Structure (Declaration, Initialization, Updating)
Insert After a specific location
Insert a value before a specific location
Insert a value as a first item of list
Delete a value from a link list
Double circular Linked list
Double circular linked list is a type of linked list with every node
containing two node pointers (next, prev). Moreover next link of
end node points to start node and prev link of start node points
to end node.
Double circular Linked list
Double circular Link list … Node ADT
Double circular Linked list … ADT
Double circular Link list … Operations
Operation Description Pseudocode
DList(); A constructor that sets
‘start’ pointer to NULL
Assign NULL to ‘start’
Assign NULL to ‘end’
Double circular Link list … Operations
Operation Description Pseudocode
int size(); Counts and returns the
number of nodes in double
linked list
 Create a Node Pointer ‘temp’ and point it to
‘start’
 If ‘temp’ points to NULL
 Return 0, Exit
 Otherwise,
 Create a variable ‘count’ and assign 1 to it
 Iterate through list nodes until ‘temp’
points to ‘start’ again
 During each iteration increase value of
‘count’ by 1 and move to ‘next’ node
 When ‘temp’ points to ‘start’ again, return
‘count’, Exit
Double circular Link list … size()
Double circular Link list … Operations
Operation Description Pseudocode
void print_all(); Prints all nodes’ data
values
 Create a node pointer ‘temp’ and point it to ‘start’ of
list
 Traverse through the list until ‘temp’ reaches to the
‘start’ of list again
 During every iteration print the ‘data’ value and move
to ‘next’ node
Double circular Link list … print_all()
Double Linked list … print_all_backward()
Operation Description Pseudocode
void print_all_backward(); Print all nodes’ ‘data’ values
in backward direction
 Create a node pointer ‘temp’ and point it to
‘end’ of list
 Traverse back through the list until ‘temp’
reaches to the ‘end’ node of the list again
 During every iteration print the ‘data’ value and
move to previous node
Double circular Link list … print_all_backward()
Double circular Linked list … SEARCH()
Operation Description Pseudocode
Node* search(int key); Searches through the list
for a Node with given value
and returns its reference
 Create a node pointer ‘temp’ and point it to
‘start’
 Traverse through the list until either ‘temp’
reaches to the Node with given value or it
reaches to the ‘start’ of list again
 If ‘temp’ points to ‘node’ with its ‘data’ equal to
‘key’ then return ‘temp’ reference
 Otherwise, return NULL
Double CIRCULAR Linked list … SEARCH()
Double Linked list … Operations
Operation Description Pseudocode
void add_node(int key); Create a node with given
value and add it to start of
the list
 Create a node with given ‘key’ as its ‘data’,
NULL as ‘next’, NULL as ‘prev’
 Point the newly created node with pointer
‘temp’
 If size() of list is ZERO, assign ‘temp’ to ‘start’,
assign ‘temp’ to ‘end’, assign ‘next’ of ‘temp’ to
‘temp’ and ‘prev’ of ‘temp’ to ‘temp’
 Otherwise
 Assign ‘start’ to ‘next’ of ‘temp’
 Assign ‘end’ to ‘prev’ of ‘temp’
 Assign ‘temp’ to ‘next’ of ‘end’
 Assign ‘temp’ to ‘prev’ of ‘start’
 Assign ‘temp’ to ‘start’
Double Linked list … add_node()
Double Linked list … Operations
Operation Description Pseudocode
void append_node(int
key);
Create a node with given
value and add it to end of
the list
 Create a node with given ‘key’ as its ‘data’,
NULL as ‘next’, NULL as ‘prev’
 Point the newly created node with pointer
‘temp’
 If size() of list is ZERO, assign ‘temp’ to ‘start’,
assign ‘temp’ to ‘end’, assign ‘next’ of ‘temp’ to
‘temp’ and ‘prev’ of ‘temp’ to ‘temp’
 Otherwise
 Assign ‘start’ to ‘next’ of ‘temp’
 Assign ‘end’ to ‘prev’ of ‘temp’
 Assign ‘temp’ to ‘next’ of ‘end’
 Assign ‘temp’ to ‘prev’ of ‘start’
 Assign ‘temp’ to ‘end’
Double Linked list … add_node()
Double Linked list … Operations
Operation Description Pseudocode
void delete_node
(int key);
Delete the node with
given value (key) from
the list
 Search node with given value as key (say it is ‘temp’)
 If ‘temp’ is equl to NULL then Exit
 Else If ‘temp’ is equal to ‘start’ and it is equal to ‘end’
 Assign NULL to ‘start’ and assign NULL to ‘end’, Exit
 Else if ‘temp’ is equal to ‘start’
 Assign ‘start’ to ‘next’ of ‘start’
 Assign ‘end’ to ‘prev’ of ‘start’
 Assign ‘start’ to ‘next’ of ‘end’
 Delete ‘temp’, Exit
 Else if ‘temp’ is equal to ‘end’
 Assign ‘end’ to ‘prev’ of ‘end’
 Assign ‘start’ to ‘next’ of ‘end’
 Assign ‘end’ to ‘prev’ of ‘start’
 Delete ‘temp’, Exit
Double Linked list … Operations
Operation Description Pseudocode
void delete_node
(int key);
Delete the node with
given value (key) from
the list
 Otherwise,
 Create node pointers ‘n’ and ‘p’ and assign them ‘next’ of
‘temp’ and ‘prev’ of ‘temp’ respectively
 Assign ‘n’ to the ‘next’ of ‘p’
 Assign ‘p’ to the ‘prev’ of ‘n’
 Delete ‘temp’, Exit
Double Linked list … delete_node
Double Linked list … Operations
Operation Description Pseudocode
void insert_after(int after,
int key);
Insert a node with given
value in linked list
immediately after a specific
node already present in the
list
 Search the node with data is equal to ‘after’, say
it is ‘temp’
 If ‘temp’ is pointing to NULL,
 exit
 Else,
 Create a new node in memory and point it
be a pointer ‘new_node’
 Set ‘key’ as data of ‘new_node’
 Assign ‘next’ of ‘temp’ to ‘next’ of ‘new_node’
 Assign ‘temp’ to ‘prev’ of ‘new_node’
 Assign ‘new_node’ to ‘prev’ of ‘next’ of
‘temp’
 Assign ‘new_node’ to the ‘next’ of ‘temp’
 If ‘temp’ points to ‘end’, Assign ‘new_node’
to ‘end’
 Exit
Double Linked list … insert_after
Double Linked list … Operations
Operation Description Pseudocode
void insert_before(int
before, int key);
Insert a node with given
value in linked list
immediately after a
specific node already
present in the list
 Search the node with data is equal to ‘before’, say
it is ‘temp’
 If ‘temp’ is pointing to NULL,
 exit
 Else,
 Create a new node in memory and point it be
a pointer ‘new_node’
 Set ‘key’ as data of ‘new_node’
 Assign ‘temp’ to ‘next’ of ‘new_node’
 Assign ‘prev’ of ‘temp’ to ‘prev’ of ‘new_node’
 Assign ‘new_node’ to ‘next’ of ‘prev’ of ‘temp’
 Assign ‘new_node’ to the ‘prev’ of ‘temp’
 If ‘temp’ points to ‘start’, Assign ‘new_node’ to
‘start’
 Exit
Double Linked list … insert_before
Structure of Node
struct node
{
struct node*prev;
int data;
struct node*next;
};
Main operations on DCLL
#include<iostream>
#include<stdlib.h>
#define null 0
#define True 1
using namespace std;
struct node{
struct node*prev;
int data;
struct node*next;
};
struct node*ptfirst=null;
void insert(int value);
void display();
void insert_first(int value);
void insert_after(int value, int position);
void insert_before(int value, int position);
void Delete(int value);
int main(){
int value,position,option;
while(True){
cout<<endl;
cout<<"Enter Option Number: "<<endl;
cout<<"1 to Insert a value."<<endl;
cout<<"2 to Display the list."<<endl;
cout<<"3 to Insert at First."<<endl;
cout<<"4 to Insert after a specific value."<<endl;
cout<<"5 to Insert before a specific value."<<endl;
cout<<"6 to Delete a value."<<endl;
cout<<"7 to EXIT"<<endl;
cin>>option;
switch(option){
case 1:
cout<<endl<<"Enter a Value: "<<endl;
cin>>value;
insert(value);
break;
case 2:
display();
break;
case 3:
cout<<endl<<"Enter a Value: "<<endl;
cin>>value;
insert_first(value);
break;
case 4:
cout<<endl<<"Enter a Value: "<<endl;
cin>>value;
cout<<"Enter the Position: ";
cin>>position;
insert_after(value,position);
break;
case 5:
cout<<endl<<"Enter a Value: "<<endl;
cin>>value;
cout<<"Enter the Position: ";
cin>>position;
insert_before(value,position);
break;
case 6:
cout<<endl<<"Insert Value to Delete: "<<endl;
cin>>value;
Delete(value);
break;
case 7:
exit(0);
break;
default:
cout<<endl<<"INVALID OPTION"<<endl;
}
}
return 0;
}
void insert(int value){
struct node*temp;
if (ptfirst==null) {
temp=(struct node*)malloc(sizeof(struct node));
ptfirst=temp;
temp->next=ptfirst;
temp->prev=ptfirst;
}
else {
temp=ptfirst;
do{
temp=temp->next;
}
while (temp->next != ptfirst);
temp->next=(struct node*)malloc(sizeof(struct node));
temp->next->prev=temp;
temp=temp->next;
temp->next=ptfirst;
temp->next->prev=temp;
}
temp->data=value;
return;
}
void display(){
struct node*temp;
if (ptfirst==null) {
cout<<endl<<"List is Empty"<<endl;
return;
}
temp=ptfirst;
do{
cout<<temp->data<<endl;
temp=temp->next;
}
while(temp!=ptfirst);
return;
}
void insert_first(int value){
struct node*temp;
struct node *q;
if (ptfirst==null) {
temp=(struct node*)malloc(sizeof(struct node));
ptfirst=temp;
temp->next=ptfirst;
temp->prev=ptfirst;
temp->data=value;
return;
}
else{
q=(struct node*)malloc(sizeof(node));
q->data=value;
q->next=ptfirst;
q->next->prev=q;
temp=ptfirst;
do{
temp=temp->next;
}
while(temp->next!=ptfirst);
temp->next=q;
q->prev=temp;
ptfirst=q;
return;
}
}
void insert_after(int value, int position)
{
struct node*temp;
struct node*q;
if (position<=0 || ptfirst==null) {
cout<<endl<<"INVALID POSITION"<<endl;
return;
}
temp=ptfirst;
for (int i=1; i<position; i++) {
temp=temp->next;
if (temp==ptfirst) {
cout<<endl<<"INVALID POSITION"<<endl;
return;
}
}
q=(struct node*)malloc(sizeof(struct node));
q->data=value;
if (temp->next == ptfirst) {
q->prev=temp;
q->next=temp->next;
temp->next=q;
return;
}
else {
q->prev=temp;
q->next=temp->next;
temp->next->prev=q;
temp->next=q;
}
return;
}
void insert_before(int value, int position){
struct node *temp;
if(position<=0){
cout<<"invalid position";
return;
}
else if(position==1){
struct node *temp;
struct node *q;
temp=ptfirst;
q=(struct node*)malloc(sizeof(node));
q->data=value;
q->next=temp;
do{
temp=temp->next;
}
while(temp->next!=ptfirst);
ptfirst=q;
temp->next=ptfirst;
q->prev=temp->next;
return;
}
else
if(position>1){
temp=ptfirst;
for(int i=1;i<position-1;i++){
temp=temp->next;
if(temp->next==ptfirst){
cout<<"invalid position"<<endl;
return;
}
}
struct node *q;
q=(struct node*)malloc(sizeof(node));
q->data=value;
temp->next->prev=q;
q->prev=temp;
q->next=temp->next;
temp->next=q;
return;
}
}
void Delete(int value){
if(ptfirst==null){
cout<<"No value in the list to delete."<<endl;
return;
}
struct node*temp;
temp=ptfirst;
do {
if(temp->data == value) {
if(temp->prev == ptfirst && temp->next == ptfirst) {
ptfirst=null;
delete temp;
return;
}
else if(temp==ptfirst){
temp->next->prev=temp->prev;
temp->prev->next=temp->next;
ptfirst=temp->next;
delete temp;
return;
}
else{
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
delete temp;
return;
}
}
temp=temp->next;
}
while (temp != ptfirst);
cout<<endl<<"Value NOT FOUND"<<endl;
return;
}

Weitere ähnliche Inhalte

Ähnlich wie Data structure.pptx

I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfforladies
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfankit11134
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked listAmit Vats
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdfharihelectronicspune
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptxchin463670
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data StructureMuhazzab Chouhadry
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfvishalateen
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfpoblettesedanoree498
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdffortmdu
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Muhammad Hammad Waseem
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfconnellalykshamesb60
 

Ähnlich wie Data structure.pptx (20)

I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
Mi 103 linked list
Mi 103 linked listMi 103 linked list
Mi 103 linked list
 
Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf#includeiostream #includecstdio #includecstdlib using na.pdf
#includeiostream #includecstdio #includecstdlib using na.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Ll.pptx
Ll.pptxLl.pptx
Ll.pptx
 
linked list.pptx
linked list.pptxlinked list.pptx
linked list.pptx
 
Linked lists in Data Structure
Linked lists in Data StructureLinked lists in Data Structure
Linked lists in Data Structure
 
Linked list1.ppt
Linked list1.pptLinked list1.ppt
Linked list1.ppt
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
Linked list
Linked listLinked list
Linked list
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
Data structure
Data  structureData  structure
Data structure
 
Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]Data Structures - Lecture 7 [Linked List]
Data Structures - Lecture 7 [Linked List]
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdf
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 

Mehr von SajalFayyaz

Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptxSajalFayyaz
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.pptSajalFayyaz
 
data structure 9.pptx
data structure 9.pptxdata structure 9.pptx
data structure 9.pptxSajalFayyaz
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptxSajalFayyaz
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptxSajalFayyaz
 
Data structure 6.pptx
Data structure 6.pptxData structure 6.pptx
Data structure 6.pptxSajalFayyaz
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptxSajalFayyaz
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptxSajalFayyaz
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptxSajalFayyaz
 

Mehr von SajalFayyaz (9)

Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Data structure.ppt
Data structure.pptData structure.ppt
Data structure.ppt
 
data structure 9.pptx
data structure 9.pptxdata structure 9.pptx
data structure 9.pptx
 
Data structure 8.pptx
Data structure 8.pptxData structure 8.pptx
Data structure 8.pptx
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Data structure 6.pptx
Data structure 6.pptxData structure 6.pptx
Data structure 6.pptx
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 

Kürzlich hochgeladen

Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Klinik kandungan
 
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...HyderabadDolls
 
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...nirzagarg
 
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service AvailableVastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Availablegargpaaro
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubaikojalkojal131
 
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...kumargunjan9515
 
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...gajnagarg
 
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...ThinkInnovation
 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...gajnagarg
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteedamy56318795
 
Aspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - AlmoraAspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - AlmoraGovindSinghDasila
 
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...kumargunjan9515
 
Gartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxGartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxchadhar227
 
Introduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptxIntroduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptxAniqa Zai
 
Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?RemarkSemacio
 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...HyderabadDolls
 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxronsairoathenadugay
 
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...gajnagarg
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...nirzagarg
 
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...gajnagarg
 

Kürzlich hochgeladen (20)

Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
Jual obat aborsi Bandung ( 085657271886 ) Cytote pil telat bulan penggugur ka...
 
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
Kalyani ? Call Girl in Kolkata | Service-oriented sexy call girls 8005736733 ...
 
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Rohtak [ 7014168258 ] Call Me For Genuine Models We...
 
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service AvailableVastral Call Girls Book Now 7737669865 Top Class Escort Service Available
Vastral Call Girls Book Now 7737669865 Top Class Escort Service Available
 
Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubai
 
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...Fun all Day Call Girls in Jaipur   9332606886  High Profile Call Girls You Ca...
Fun all Day Call Girls in Jaipur 9332606886 High Profile Call Girls You Ca...
 
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Indore [ 7014168258 ] Call Me For Genuine Models We...
 
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
Identify Customer Segments to Create Customer Offers for Each Segment - Appli...
 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Aspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - AlmoraAspirational Block Program Block Syaldey District - Almora
Aspirational Block Program Block Syaldey District - Almora
 
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...Top Call Girls in Balaghat  9332606886Call Girls Advance Cash On Delivery Ser...
Top Call Girls in Balaghat 9332606886Call Girls Advance Cash On Delivery Ser...
 
Gartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxGartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptx
 
Introduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptxIntroduction to Statistics Presentation.pptx
Introduction to Statistics Presentation.pptx
 
Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?Case Study 4 Where the cry of rebellion happen?
Case Study 4 Where the cry of rebellion happen?
 
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
Nirala Nagar / Cheap Call Girls In Lucknow Phone No 9548273370 Elite Escort S...
 
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptxRESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
RESEARCH-FINAL-DEFENSE-PPT-TEMPLATE.pptx
 
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Nandurbar [ 7014168258 ] Call Me For Genuine Models...
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
 
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Latur [ 7014168258 ] Call Me For Genuine Models We ...
 

Data structure.pptx

  • 1. CS261 DATA STRUCTURES & ALGORITHMS (WEEK-4) LECTURE-7 & 8 INTRODUCTION TO DATA STRUCTURES & ALGORITHMS Lecturer Azka Aziz Azka.a@scocs.edu.pk
  • 2. Data structures & Algorithms Lecture#07 Circular Link List
  • 3. Course contents Circular Link List Data Structure (Declaration, Initialization, Updating) Insert After a specific location Insert a value before a specific location Insert a value as a first item of list Delete a value from a link list
  • 4. Circular Link list A circular linked list is a type of linked list in which last node of the list points to start node of the list instead of NULL. data data data data data data data start next next next next next next next
  • 5. Circular Link list Circular linked list contains all the operations that a simple linked list has The only difference is that ‘next’ pointer of last node points to first (start) node of list This small change gives the list a circular form affecting the implementation of few operations in the list including add_node(), insert_node(), append_node(), delete_node() etc Except when list is empty, no node pointer points to NULL
  • 6. Circular Link list … node ADT
  • 7. Circular Link list … list ADT
  • 8. Circular Linked list … Operations Operation Description Pseudocode Clist(); A constructor that sets ‘start’ pointer to NULL Assign NULL to ‘start’ int size(); Calculate and return the number of elements in list  If ‘start’ points to NULL, return 0  Otherwise,  Create a variable ‘count’ and assign 1 to it  Create a node pointer ‘temp’ and point it to next of ‘start’  Iterate through all list items until ‘temp’ points to ‘start’ again; at that point current value of ‘count’ will be returned  During every iteration ‘count’ will be incremented by 1 and ‘temp’ will point to ‘next’ pointer of node currently it is pointing to
  • 9. Circular Linked list … size()
  • 10. Circular Linked list … Operations Operation Description Pseudocode void print_all(); Prints all nodes’ data values  Assign ‘start’ to a Node pointer named ‘temp’  Traverse through the list until either ‘temp’ reaches to the ‘start’ of CList again  During every iteration print the ‘data’ value
  • 11. Circular Linked list … print_all()
  • 12. Circular Linked list … Operations Operation Description Pseudocode Node* search(int key); Searches through the list for a Node with given value and returns its reference  Create a new node pointer ‘temp’ and point it to ‘start’  Traverse through the list until either ‘temp’ reaches to the Node with value equal to ‘key’ or it reaches to the ‘start’ of list again  If ‘temp’ reaches to the ‘start’ of CList again, return NULL  Otherwise, return the node pointer ‘temp’
  • 13. Circular Linked list … search()
  • 14. Circular Linked list … Operations Operation Description Pseudocode void add_node(int); Create a node with given value and add it immediately after the start of the list  Create a node with given ‘key’ as its ‘data’ pointed by a node pointer ‘temp’ and set its ‘next’ to itself  If size() of CList is equal to 0, assign ‘temp’ to ‘start’  Otherwise,  Set ‘next’ of ‘temp’ to ‘next’ of ‘start’  Assign ‘temp’ to ‘next’ of ‘start’
  • 15. Circular Linked list … add_node()
  • 16. Circular Linked list … Operations Operation Description Pseudocode void append_node(int); Create a node with given value and append (add to end) it to the list  Create a node with given ‘key’ as its ‘data’ pointed by a node pointer ‘n’ and set its ‘next’ to itself  Create a node pointer ‘temp’ and point it to ‘start’ of Clist  If size() of CList is equal to 0,  Assign ‘n’ to ‘start’, Exit  Otherwise,  Traverse ‘temp’ to the last node of Clist  Set ‘next’ of ‘n’ to ‘start’  Set ‘next’ of ‘temp’ to ‘n’  Exit
  • 17. Circular Linked list … append_node()
  • 18. Circular Linked list … Operations Operation Description Pseudocode void insert (int after, int key); Insert a node with given value in linked list immediately after a specific node already present in the list  Create a new node pointer ‘temp’ and assign ‘start’ to it  If ‘start’ is pointing to NULL, Exit  Move to next nodes until either ‘temp’ reaches to the node with ‘data’ value equal to ‘after’ or it reaches to ‘start’ of CList again  If ‘temp’ is pointing to ‘start’ of CList again,  exit  Else,  Create a new node in memory with ‘key’ as data and point it by a pointer ‘n’  Set ‘next’ of ‘n’ to ‘next’ of ‘temp’  Set ‘next’ of ‘temp’ to ‘n’  Exit
  • 19. Circular Linked list … insert()
  • 20. Circular Linked list … Operations Operation Description Pseudocode void delete_node (int key); Delete the node with given value from the list  If size() of CList is 0, Exit  Create a node pointer ‘temp’ and point it to ‘start’  Iterate ‘temp’ to next nodes until either ‘temp’ reaches to a node whose ‘next’ node has ‘data’ equal to ‘key’ or its ‘next’ node is ‘start’  If ‘next’ of ‘temp’ points to ‘start’  If ‘data’ of ‘start’ is not equal to ‘key’, Exit  Else if size() of CList is equal to 1  Assign NULL to ‘start’, Exit  Otherwise  Move ‘start’ to next node in list  Assign ‘next’ of ‘temp’ to a node pointer, say ‘x’  Set ‘next’ of ‘temp’ to ‘start’  Delete ‘x’  Exit  Else,  Delete ‘next’ pointer of ‘temp’ and adjust pointers accordingly  Exit
  • 21. Circular Linked list … delete_node()
  • 22. Structure of Node in CLL struct node { int data; struct node*link; };
  • 23. Main Operations on CLL #include<iostream> #include<stdlib.h> #define null 0 #define true 1 using namespace std; struct node{ int data; struct node*link; }; struct node*ptfirst=null; void insert(int value); void display(); void insert_first(int value); void insert_after(int position,int value); void insert_before(int position,int value); void Delete(int value); int main(){ int opt_no,value,position;
  • 24. while(true){ cout<<"Enter 1 for insert"<<endl; cout<<"Enter 2 for Display"<<endl; cout<<"Enter 3 for insert first"<<endl; cout<<"Enter 4 for insert after"<<endl; cout<<"Enter 5 for insert before"<<endl; cout<<"Enter 6 for delete"<<endl; cout<<"Enter 7 for Exit"<<endl<<endl; cout<<"Now Enter Option Number: "; cin>>opt_no;
  • 25. switch (opt_no) { case 1: cout<<"insert value: "; cin>>value; cout<<endl; insert(value); break; case 2: display(); break; case 3: cout<<"insert value: "; cin>>value; cout<<endl; insert_first(value); break; case 4: cout<<"enter position: "; cin>>position; cout<<"insert value: "; cin>>value; cout<<endl; insert_after(position,value); break;
  • 26. case 5: cout<<"enter position: "; cin>>position; cout<<"insert value: "; cin>>value; cout<<endl; insert_before(position,value); break; case 6: cout<<"insert value: "; cin>>value; cout<<endl; Delete(value); break; case 7: exit(1); default: cout<<"I N V A L I D ___ O P T I O N"<<endl; }
  • 27. Insert a Value in CLL void insert(int value) { struct node*temp; if(ptfirst==null){ temp=(struct node*)malloc(sizeof(node)); ptfirst=temp; } else{ temp=ptfirst; do{ temp=temp->link; }while(temp->link!=ptfirst); temp->link=(struct node*)malloc(sizeof(node)); temp=temp->link; } temp->link=ptfirst; temp->data=value; return; }
  • 28. Delete a value from a CLL void Delete(int value){ if(ptfirst==null){ cout<<"No value in the list to delete."<<endl; return; } struct node*temp; struct node*prev; temp=ptfirst; prev=temp; do{ if(temp->data==value){ if(temp==ptfirst && temp->link==ptfirst){ delete temp; ptfirst=null; return; } else if(temp==ptfirst){
  • 30. Display CLL void display() { struct node*temp; if(ptfirst==null){ cout<<"List is empty"<<endl; return; } temp=ptfirst; do{ cout<<temp->data<<endl; temp=temp->link; } while(temp!=ptfirst); return; }
  • 31. Insert after void insert_after(int position,int value){ if(position<=0 || ptfirst==null){ cout<<"Invalid Position"<<endl; return; } struct node*temp; struct node*q; temp=ptfirst; for(int i=1; i<position; i++){ temp=temp->link; if(temp==ptfirst){ cout<<"Invalid Position OR Position does not exists"<<endl; return; } } q=(struct node*)malloc(sizeof(struct node)); q->data=value; q->link=temp->link; temp->link=q; return;
  • 32. Insert Before void insert_before(int position,int value){ struct node *temp; if(position<=0){ cout<<"invalid position"; return; } else if(position==1){ struct node*temp; struct node *q; q=(struct node*)malloc(sizeof(node)); q->data=value; q->link=ptfirst; temp=ptfirst; do{ temp=temp->link; } while(temp->link!=ptfirst); temp->link=q; ptfirst=q; return; }
  • 33. else if(position>1){ temp=ptfirst; for(int i=1;i<position-1;i++){ temp=temp->link; if(temp==ptfirst){ cout<<"invalid position"<<endl; return; } } struct node *q; q=(struct node*)malloc(sizeof(node)); q->data=value; q->link=temp->link; temp->link=q; return; } }
  • 34. Insert value as first value void insert_first(int value) { struct node*temp; struct node *q; q=(struct node*)malloc(sizeof(node)); q->data=value; q->link=ptfirst; temp=ptfirst; do{ temp=temp->link; } while(temp->link!=ptfirst); temp->link=q; ptfirst=q; return; }
  • 35. Data structures & Algorithms Lecture#08 Double Circular Link List
  • 36. Course contents  Double Circular Link List Data Structure (Declaration, Initialization, Updating) Insert After a specific location Insert a value before a specific location Insert a value as a first item of list Delete a value from a link list
  • 37. Double circular Linked list Double circular linked list is a type of linked list with every node containing two node pointers (next, prev). Moreover next link of end node points to start node and prev link of start node points to end node.
  • 39. Double circular Link list … Node ADT
  • 40. Double circular Linked list … ADT
  • 41. Double circular Link list … Operations Operation Description Pseudocode DList(); A constructor that sets ‘start’ pointer to NULL Assign NULL to ‘start’ Assign NULL to ‘end’
  • 42. Double circular Link list … Operations Operation Description Pseudocode int size(); Counts and returns the number of nodes in double linked list  Create a Node Pointer ‘temp’ and point it to ‘start’  If ‘temp’ points to NULL  Return 0, Exit  Otherwise,  Create a variable ‘count’ and assign 1 to it  Iterate through list nodes until ‘temp’ points to ‘start’ again  During each iteration increase value of ‘count’ by 1 and move to ‘next’ node  When ‘temp’ points to ‘start’ again, return ‘count’, Exit
  • 43. Double circular Link list … size()
  • 44. Double circular Link list … Operations Operation Description Pseudocode void print_all(); Prints all nodes’ data values  Create a node pointer ‘temp’ and point it to ‘start’ of list  Traverse through the list until ‘temp’ reaches to the ‘start’ of list again  During every iteration print the ‘data’ value and move to ‘next’ node
  • 45. Double circular Link list … print_all()
  • 46. Double Linked list … print_all_backward() Operation Description Pseudocode void print_all_backward(); Print all nodes’ ‘data’ values in backward direction  Create a node pointer ‘temp’ and point it to ‘end’ of list  Traverse back through the list until ‘temp’ reaches to the ‘end’ node of the list again  During every iteration print the ‘data’ value and move to previous node
  • 47. Double circular Link list … print_all_backward()
  • 48. Double circular Linked list … SEARCH() Operation Description Pseudocode Node* search(int key); Searches through the list for a Node with given value and returns its reference  Create a node pointer ‘temp’ and point it to ‘start’  Traverse through the list until either ‘temp’ reaches to the Node with given value or it reaches to the ‘start’ of list again  If ‘temp’ points to ‘node’ with its ‘data’ equal to ‘key’ then return ‘temp’ reference  Otherwise, return NULL
  • 49. Double CIRCULAR Linked list … SEARCH()
  • 50. Double Linked list … Operations Operation Description Pseudocode void add_node(int key); Create a node with given value and add it to start of the list  Create a node with given ‘key’ as its ‘data’, NULL as ‘next’, NULL as ‘prev’  Point the newly created node with pointer ‘temp’  If size() of list is ZERO, assign ‘temp’ to ‘start’, assign ‘temp’ to ‘end’, assign ‘next’ of ‘temp’ to ‘temp’ and ‘prev’ of ‘temp’ to ‘temp’  Otherwise  Assign ‘start’ to ‘next’ of ‘temp’  Assign ‘end’ to ‘prev’ of ‘temp’  Assign ‘temp’ to ‘next’ of ‘end’  Assign ‘temp’ to ‘prev’ of ‘start’  Assign ‘temp’ to ‘start’
  • 51. Double Linked list … add_node()
  • 52. Double Linked list … Operations Operation Description Pseudocode void append_node(int key); Create a node with given value and add it to end of the list  Create a node with given ‘key’ as its ‘data’, NULL as ‘next’, NULL as ‘prev’  Point the newly created node with pointer ‘temp’  If size() of list is ZERO, assign ‘temp’ to ‘start’, assign ‘temp’ to ‘end’, assign ‘next’ of ‘temp’ to ‘temp’ and ‘prev’ of ‘temp’ to ‘temp’  Otherwise  Assign ‘start’ to ‘next’ of ‘temp’  Assign ‘end’ to ‘prev’ of ‘temp’  Assign ‘temp’ to ‘next’ of ‘end’  Assign ‘temp’ to ‘prev’ of ‘start’  Assign ‘temp’ to ‘end’
  • 53. Double Linked list … add_node()
  • 54. Double Linked list … Operations Operation Description Pseudocode void delete_node (int key); Delete the node with given value (key) from the list  Search node with given value as key (say it is ‘temp’)  If ‘temp’ is equl to NULL then Exit  Else If ‘temp’ is equal to ‘start’ and it is equal to ‘end’  Assign NULL to ‘start’ and assign NULL to ‘end’, Exit  Else if ‘temp’ is equal to ‘start’  Assign ‘start’ to ‘next’ of ‘start’  Assign ‘end’ to ‘prev’ of ‘start’  Assign ‘start’ to ‘next’ of ‘end’  Delete ‘temp’, Exit  Else if ‘temp’ is equal to ‘end’  Assign ‘end’ to ‘prev’ of ‘end’  Assign ‘start’ to ‘next’ of ‘end’  Assign ‘end’ to ‘prev’ of ‘start’  Delete ‘temp’, Exit
  • 55. Double Linked list … Operations Operation Description Pseudocode void delete_node (int key); Delete the node with given value (key) from the list  Otherwise,  Create node pointers ‘n’ and ‘p’ and assign them ‘next’ of ‘temp’ and ‘prev’ of ‘temp’ respectively  Assign ‘n’ to the ‘next’ of ‘p’  Assign ‘p’ to the ‘prev’ of ‘n’  Delete ‘temp’, Exit
  • 56. Double Linked list … delete_node
  • 57. Double Linked list … Operations Operation Description Pseudocode void insert_after(int after, int key); Insert a node with given value in linked list immediately after a specific node already present in the list  Search the node with data is equal to ‘after’, say it is ‘temp’  If ‘temp’ is pointing to NULL,  exit  Else,  Create a new node in memory and point it be a pointer ‘new_node’  Set ‘key’ as data of ‘new_node’  Assign ‘next’ of ‘temp’ to ‘next’ of ‘new_node’  Assign ‘temp’ to ‘prev’ of ‘new_node’  Assign ‘new_node’ to ‘prev’ of ‘next’ of ‘temp’  Assign ‘new_node’ to the ‘next’ of ‘temp’  If ‘temp’ points to ‘end’, Assign ‘new_node’ to ‘end’  Exit
  • 58. Double Linked list … insert_after
  • 59. Double Linked list … Operations Operation Description Pseudocode void insert_before(int before, int key); Insert a node with given value in linked list immediately after a specific node already present in the list  Search the node with data is equal to ‘before’, say it is ‘temp’  If ‘temp’ is pointing to NULL,  exit  Else,  Create a new node in memory and point it be a pointer ‘new_node’  Set ‘key’ as data of ‘new_node’  Assign ‘temp’ to ‘next’ of ‘new_node’  Assign ‘prev’ of ‘temp’ to ‘prev’ of ‘new_node’  Assign ‘new_node’ to ‘next’ of ‘prev’ of ‘temp’  Assign ‘new_node’ to the ‘prev’ of ‘temp’  If ‘temp’ points to ‘start’, Assign ‘new_node’ to ‘start’  Exit
  • 60. Double Linked list … insert_before
  • 61. Structure of Node struct node { struct node*prev; int data; struct node*next; };
  • 62. Main operations on DCLL #include<iostream> #include<stdlib.h> #define null 0 #define True 1 using namespace std; struct node{ struct node*prev; int data; struct node*next; }; struct node*ptfirst=null; void insert(int value); void display(); void insert_first(int value); void insert_after(int value, int position); void insert_before(int value, int position); void Delete(int value);
  • 63. int main(){ int value,position,option; while(True){ cout<<endl; cout<<"Enter Option Number: "<<endl; cout<<"1 to Insert a value."<<endl; cout<<"2 to Display the list."<<endl; cout<<"3 to Insert at First."<<endl; cout<<"4 to Insert after a specific value."<<endl; cout<<"5 to Insert before a specific value."<<endl; cout<<"6 to Delete a value."<<endl; cout<<"7 to EXIT"<<endl; cin>>option;
  • 64. switch(option){ case 1: cout<<endl<<"Enter a Value: "<<endl; cin>>value; insert(value); break; case 2: display(); break; case 3: cout<<endl<<"Enter a Value: "<<endl; cin>>value; insert_first(value); break; case 4: cout<<endl<<"Enter a Value: "<<endl; cin>>value; cout<<"Enter the Position: "; cin>>position; insert_after(value,position); break;
  • 65. case 5: cout<<endl<<"Enter a Value: "<<endl; cin>>value; cout<<"Enter the Position: "; cin>>position; insert_before(value,position); break; case 6: cout<<endl<<"Insert Value to Delete: "<<endl; cin>>value; Delete(value); break; case 7: exit(0); break; default: cout<<endl<<"INVALID OPTION"<<endl; } } return 0; }
  • 66. void insert(int value){ struct node*temp; if (ptfirst==null) { temp=(struct node*)malloc(sizeof(struct node)); ptfirst=temp; temp->next=ptfirst; temp->prev=ptfirst; } else { temp=ptfirst; do{ temp=temp->next; } while (temp->next != ptfirst); temp->next=(struct node*)malloc(sizeof(struct node)); temp->next->prev=temp; temp=temp->next; temp->next=ptfirst; temp->next->prev=temp; } temp->data=value; return; }
  • 67. void display(){ struct node*temp; if (ptfirst==null) { cout<<endl<<"List is Empty"<<endl; return; } temp=ptfirst; do{ cout<<temp->data<<endl; temp=temp->next; } while(temp!=ptfirst); return; }
  • 68. void insert_first(int value){ struct node*temp; struct node *q; if (ptfirst==null) { temp=(struct node*)malloc(sizeof(struct node)); ptfirst=temp; temp->next=ptfirst; temp->prev=ptfirst; temp->data=value; return; } else{ q=(struct node*)malloc(sizeof(node)); q->data=value; q->next=ptfirst; q->next->prev=q; temp=ptfirst; do{ temp=temp->next; } while(temp->next!=ptfirst); temp->next=q; q->prev=temp; ptfirst=q; return; } }
  • 69. void insert_after(int value, int position) { struct node*temp; struct node*q; if (position<=0 || ptfirst==null) { cout<<endl<<"INVALID POSITION"<<endl; return; } temp=ptfirst; for (int i=1; i<position; i++) { temp=temp->next; if (temp==ptfirst) { cout<<endl<<"INVALID POSITION"<<endl; return; } }
  • 70. q=(struct node*)malloc(sizeof(struct node)); q->data=value; if (temp->next == ptfirst) { q->prev=temp; q->next=temp->next; temp->next=q; return; } else { q->prev=temp; q->next=temp->next; temp->next->prev=q; temp->next=q; } return; }
  • 71. void insert_before(int value, int position){ struct node *temp; if(position<=0){ cout<<"invalid position"; return; } else if(position==1){ struct node *temp; struct node *q; temp=ptfirst; q=(struct node*)malloc(sizeof(node)); q->data=value; q->next=temp; do{ temp=temp->next; } while(temp->next!=ptfirst); ptfirst=q; temp->next=ptfirst; q->prev=temp->next; return; } else
  • 72. if(position>1){ temp=ptfirst; for(int i=1;i<position-1;i++){ temp=temp->next; if(temp->next==ptfirst){ cout<<"invalid position"<<endl; return; } } struct node *q; q=(struct node*)malloc(sizeof(node)); q->data=value; temp->next->prev=q; q->prev=temp; q->next=temp->next; temp->next=q; return; } }
  • 73. void Delete(int value){ if(ptfirst==null){ cout<<"No value in the list to delete."<<endl; return; } struct node*temp; temp=ptfirst; do { if(temp->data == value) { if(temp->prev == ptfirst && temp->next == ptfirst) { ptfirst=null; delete temp; return; } else if(temp==ptfirst){ temp->next->prev=temp->prev; temp->prev->next=temp->next; ptfirst=temp->next; delete temp; return; } else{