Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

Name: Minahil Ismail.

Reg. No. 21-NTU-CS-1339.


Discipline: BSSE-B (4th Semester.)
Course Title: Data Structures and Algorithms.
Course Code: COC-2077.

Assignment # 1
BSSE-4B
Q#1: What is doubly link list? How to move forward and backward in a
doubly link list? Explain its example with code and output?
Ans: Traversal of items can be done in both forward and backward directions.
Nodes consist of an additional pointer known as prev, pointing to the previous
node.
Code:
#include<iostream>
#include<conio.h>

using namespace std;

class Node
{
private:
int object;
Node* Next;
Node* Previous;
public:
int GetData();
Minahil Ismail 21-NTU-CS-1339

void SetData(int data);


Node* GetNext();
void SetNext(Node* nextNode);
Node* GetPrevious ();
void SetPrevious(Node* prevNode);
};

int Node::GetData()
{
return object;
}

Node* Node::GetNext()
{
return Next;
}

Node* Node::GetPrevious()
{
return Previous;
}
void Node::SetData(int data)
{
object = data;
}

void Node::SetNext(Node* next)


Minahil Ismail 21-NTU-CS-1339

{
Next = next;
}

void Node::SetPrevious(Node* prev)


{
Previous = prev;
}
//------------------------------------------------------------------------------

class DoublyList {
private:
Node* head;
Node* tail;
public:
DoublyList();
void InsertNode(int);
void PrintListForward();
void PrintListBackward();
};

DoublyList::DoublyList()
{
head=NULL;
tail=NULL;
}

void DoublyList::InsertNode(int o)
Minahil Ismail 21-NTU-CS-1339

{
Node *newNode=new Node;
newNode->SetData(o);
newNode->SetNext(NULL);
//IF INSERTED NODE IS THE FIRST NODE
if(head==NULL)
{
newNode->SetPrevious(NULL);
head=newNode;
tail=newNode;
}
//WHEN NODES ALREADY PRESENT
else
{
Node *temp=head;
while(temp->GetNext()!=NULL)
{
temp=temp->GetNext();
}
temp->SetNext(newNode);
newNode->SetPrevious(temp);
tail=newNode;
}
}

void DoublyList::PrintListForward()
{
Minahil Ismail 21-NTU-CS-1339

Node *temp=head;
cout<<"NULL <--> ";
while(temp!=NULL)
{
cout<<temp->GetData()<<" <--> ";
temp=temp->GetNext();
}
cout<<"NULL";
cout<<endl;
}

void DoublyList::PrintListBackward()
{
Node *temp=tail;
cout<<"NULL <--> ";
while(temp!=NULL)
{
cout<<temp->GetData()<<" <--> ";
temp=temp->GetPrevious();
}
cout<<"NULL";
cout<<endl;
}

int main()
{
DoublyList l1;
l1.InsertNode(1);
Minahil Ismail 21-NTU-CS-1339

l1.InsertNode(2);
l1.InsertNode(3);
l1.InsertNode(4);
l1.InsertNode(5);
cout<<"\n---------Traversing List in Forward Direction---------\n";
l1.PrintListForward();
cout<<"\n---------Traversing List in Backward Direction---------\n";
l1.PrintListBackward();

getch();
return 0;
}

Output:
Minahil Ismail 21-NTU-CS-1339

Q#2: How to insert 2 nodes in a doubly link list? Write the code, show the
output and explain with comments?
Ans:
Code
#include<iostream>
#include<conio.h>

using namespace std;

class Node
{
private:
int object;
Node* Next;
Node* Previous;
public:
int GetData();
void SetData(int data);
Node* GetNext();
void SetNext(Node* nextNode);
Node* GetPrevious ();
void SetPrevious(Node* prevNode);
};

int Node::GetData()
{
return object;
Minahil Ismail 21-NTU-CS-1339

Node* Node::GetNext()
{
return Next;
}

Node* Node::GetPrevious()
{
return Previous;
}
void Node::SetData(int data)
{
object = data;
}

void Node::SetNext(Node* next)


{
Next = next;
}

void Node::SetPrevious(Node* prev)


{
Previous = prev;
}
//------------------------------------------------------------------------------

class DoublyList {
Minahil Ismail 21-NTU-CS-1339

private:
Node* head;
Node* tail;
public:
DoublyList();
void InsertNode(int);
void PrintListForward();
void PrintListBackward();
};

DoublyList::DoublyList()
{
head=NULL;
tail=NULL;
}

void DoublyList::InsertNode(int o)
{
Node *newNode=new Node;
newNode->SetData(o);
newNode->SetNext(NULL);
//IF INSERTED NODE IS THE FIRST NODE
if(head==NULL)
{
newNode->SetPrevious(NULL);
head=newNode;
tail=newNode;
}
Minahil Ismail 21-NTU-CS-1339

//WHEN NODES ALREADY PRESENT


else
{
Node *temp=head;
while(temp->GetNext()!=NULL)
{
temp=temp->GetNext();
}
temp->SetNext(newNode);
newNode->SetPrevious(temp);
tail=newNode;
}
}

void DoublyList::PrintListForward()
{
Node *temp=head;
cout<<"NULL <--> ";
while(temp!=NULL)
{
cout<<temp->GetData()<<" <--> ";
temp=temp->GetNext();
}
cout<<"NULL";
cout<<endl;
}
Minahil Ismail 21-NTU-CS-1339

void DoublyList::PrintListBackward()
{
Node *temp=tail;
cout<<"NULL <--> ";
while(temp!=NULL)
{
cout<<temp->GetData()<<" <--> ";
temp=temp->GetPrevious();
}
cout<<"NULL";
cout<<endl;
}

int main()
{
DoublyList l1;
l1.InsertNode(1);
l1.InsertNode(2);
cout<<"\n---------Traversing List in Forward Direction---------\n";
l1.PrintListForward();
cout<<"\n---------Traversing List in Backward Direction---------\n";
l1.PrintListBackward();

getch();
return 0;
}
Minahil Ismail 21-NTU-CS-1339

Output

Q#3: What is circular link list? How to insert data in nodes of circular link
list? Explain with example code? Enter 5 elements and show the output with
screenshots?
#include<iostream>
#include<conio.h>

using namespace std;

class Node
{
private:
int data;
Node* Next;
Minahil Ismail 21-NTU-CS-1339

public:
int GetData();
void SetData(int data);
Node* GetNext();
void SetNext(Node* nextNode);
};

int Node::GetData()
{
return data;
}

Node* Node::GetNext()
{
return Next;
}

void Node::SetData(int d)
{
data = d;
}

void Node::SetNext(Node* next)


{
Next = next;
}
Minahil Ismail 21-NTU-CS-1339

//------------------------------------------------------------------------------

class CircularList {
private:
Node* head;
Node* tail;
public:
CircularList();
void InsertNode(int);
void PrintList();
};

CircularList::CircularList()
{
head=NULL;
tail=NULL;
}

void CircularList::InsertNode(int o)
{
Node *newNode=new Node;
newNode->SetData(o);
//first node of circular list
if(head==NULL)
{
head=newNode;
tail=newNode;
newNode->SetNext(tail);
Minahil Ismail 21-NTU-CS-1339

}
//nodes already exists(inserting at end)
else
{
newNode->SetNext(head);
tail->SetNext(newNode);
tail=newNode;
}
}

void CircularList::PrintList()
{
Node *temp=head;
do
{
temp=temp->GetNext();
if(temp!=NULL)
cout<<temp->GetData()<<"-->";
}while(temp!=head);
cout<<endl;
}

int main()
{
CircularList l1;
l1.InsertNode(1);
l1.InsertNode(2);
Minahil Ismail 21-NTU-CS-1339

l1.InsertNode(3);
l1.InsertNode(4);
l1.InsertNode(5);

cout<<"\n---------Traversing List---------\n";
l1.PrintList();

getch();
return 0;
}

Output

Q#4: How to perform insertion (at beginning, at end, in between, at specific


position), deletion and searching the desired element in doubly and circular
link list explain all these terms with example codes with output screenshots?
Ans:
Minahil Ismail 21-NTU-CS-1339

Doubly Linked List

#include<iostream>
#include<conio.h>

using namespace std;

class Node
{
private:
int object;
Node* Next;
Node* Previous;
public:
int GetData();
void SetData(int data);
Node* GetNext();
void SetNext(Node* nextNode);
Node* GetPrevious ();
void SetPrevious(Node* prevNode);
};

int Node::GetData()
{
return object;
}
Minahil Ismail 21-NTU-CS-1339

Node* Node::GetNext()
{
return Next;
}

Node* Node::GetPrevious()
{
return Previous;
}
void Node::SetData(int data)
{
object = data;
}

void Node::SetNext(Node* next)


{
Next = next;
}

void Node::SetPrevious(Node* prev)


{
Previous = prev;
}
//------------------------------------------------------------------------------

class DoublyList {
private:
Node* head;
Minahil Ismail 21-NTU-CS-1339

Node* tail;
public:
DoublyList();
void InsertNodeEnd(int);
void InsertNodeStart(int);
void InsertNodeBetween(int,int,int);
void DeleteNode(int);
void SearchNode(int);
void PrintListForward();
void PrintListBackward();
};

DoublyList::DoublyList()
{
head=NULL;
tail=NULL;
}

void DoublyList::InsertNodeEnd(int o)
{
Node *newNode=new Node;
newNode->SetData(o);
newNode->SetNext(NULL);
//IF INSERTED NODE IS THE FIRST NODE
if(head==NULL)
{
newNode->SetPrevious(NULL);
head=newNode;
Minahil Ismail 21-NTU-CS-1339

tail=newNode;
}
//WHEN NODES ALREADY PRESENT
else
{
Node *temp=head;
while(temp->GetNext()!=NULL)
{
temp=temp->GetNext();
}
temp->SetNext(newNode);
newNode->SetPrevious(temp);
tail=newNode;
}
}

void DoublyList::InsertNodeStart(int o)
{
Node *newNode=new Node;
newNode->SetData(o);
newNode->SetNext(NULL);
newNode->SetPrevious(NULL);
//IF INSERTED NODE IS THE FIRST NODE
if(head==NULL)
{
head=newNode;
tail=newNode;
}
Minahil Ismail 21-NTU-CS-1339

//WHEN NODES ALREADY PRESENT


else
{
newNode->SetNext(head);
head->SetPrevious(newNode);
head=newNode;
}
}

void DoublyList::InsertNodeBetween(int b,int a, int c)


{
Node *newNode=new Node;
newNode->SetData(b);
newNode->SetNext(NULL);
newNode->SetPrevious(NULL);
//IF INSERTED NODE IS THE FIRST NODE
if(head==NULL)
{
head=newNode;
tail=newNode;
}
//WHEN NODES ALREADY PRESENT
else
{
Node *temp1=head;
Node *temp2=tail;
while(temp1!=NULL)
{
Minahil Ismail 21-NTU-CS-1339

if(temp1->GetData()==a)
break;
temp1=temp1->GetNext();
}

while(temp2!=NULL)
{
if(temp2->GetData()==c)
break;
temp2=temp2->GetPrevious();
}

newNode->SetNext(temp2);
newNode->SetPrevious(temp1);
temp1->SetNext(newNode);
temp2->SetPrevious(newNode);
}
}

void DoublyList::DeleteNode(int o)
{
Node *temp=head;
//IF LINK LIST IS EMPTY
if(temp==NULL)
{
cout<<"\nNo Nodes to Delete!\n";
return;
}
Minahil Ismail 21-NTU-CS-1339

//IF IT'S NOT EMPTY


else
{

while(temp!=NULL)
{
if(temp->GetData()==o)
break;
temp=temp->GetNext();
}

//Only single Node


if(temp->GetPrevious()==NULL && temp->GetNext()==NULL)
{
head=NULL;
tail=NULL;
delete temp;
}
//Last Node
else if(temp->GetNext()==NULL)
{
delete temp;
temp->GetPrevious()->SetNext(NULL);
tail=temp->GetPrevious();
}

//First Node
Minahil Ismail 21-NTU-CS-1339

else if(temp->GetPrevious()==NULL)
{
temp->GetNext()->SetPrevious(NULL);
head=temp->GetNext();
delete temp;
}

//For Node Somewhere Between


else
{
temp->GetPrevious()->SetNext(temp->GetNext());
temp->GetNext()->SetPrevious(temp->GetPrevious());
delete temp;
}

}
}

void DoublyList::SearchNode(int o)
{
Node *temp=head;
while(temp!=NULL)
{
if(temp->GetData()==o)
{
cout<<"\nFound!!\n";
return;
}
Minahil Ismail 21-NTU-CS-1339

temp=temp->GetNext();
}
cout<<"\nNot Found!!\n";
}

void DoublyList::PrintListForward()
{
Node *temp=head;
cout<<"NULL <--> ";
while(temp!=NULL)
{
cout<<temp->GetData()<<" <--> ";
temp=temp->GetNext();
}
cout<<"NULL";
cout<<endl;
}

void DoublyList::PrintListBackward()
{
Node *temp=tail;
cout<<"NULL <--> ";
while(temp!=NULL)
{
cout<<temp->GetData()<<" <--> ";
temp=temp->GetPrevious();
}
cout<<"NULL";
Minahil Ismail 21-NTU-CS-1339

cout<<endl;
}

int main()
{
DoublyList l1;
l1.InsertNodeEnd(1);
l1.InsertNodeEnd(3);
l1.InsertNodeStart(0);
cout<<"\n---------Traversing List in Forward Direction---------\n";
l1.PrintListForward();

cout<<"\n---------Inserting node 2 between node 1 and node 3---------\n";


l1.InsertNodeBetween(2,1,3);
l1.PrintListForward();

cout<<"\n---------Deleting node 0 ---------\n";


l1.DeleteNode(0);
l1.PrintListForward();
cout<<"\n---------Searching Node 3---------\n";
l1.SearchNode(3);
l1.PrintListForward();
cout<<"\n---------Searching Node 0---------\n";
l1.SearchNode(0);
l1.PrintListForward();

getch();
return 0;
Minahil Ismail 21-NTU-CS-1339

Output

Circular Linked List


#include<iostream>
#include<conio.h>

using namespace std;

class Node
{
private:
int data;
Node* Next;
public:
Minahil Ismail 21-NTU-CS-1339

int GetData();
void SetData(int data);
Node* GetNext();
void SetNext(Node* nextNode);
};

int Node::GetData()
{
return data;
}

Node* Node::GetNext()
{
return Next;
}

void Node::SetData(int d)
{
data = d;
}

void Node::SetNext(Node* next)


{
Next = next;
}

//------------------------------------------------------------------------------
Minahil Ismail 21-NTU-CS-1339

class CircularList {
private:
Node* head;
Node* tail;
public:
CircularList();
void InsertNodeEnd(int);
void InsertNodeStart(int);
void InsertNodeBetween(int,int,int);
void DeleteNode(int);
void SearchNode(int);
void PrintList();
};

CircularList::CircularList()
{
head=NULL;
tail=NULL;
}

void CircularList::InsertNodeEnd(int o)
{
Node *newNode=new Node;
newNode->SetData(o);
//first node of circular list
if(head==NULL)
{
Minahil Ismail 21-NTU-CS-1339

head=newNode;
tail=newNode;
newNode->SetNext(tail);
}
//nodes already exists(inserting at end)
else
{
newNode->SetNext(head);
tail->SetNext(newNode);
tail=newNode;
}
}

void CircularList::InsertNodeStart(int o)
{
Node *newNode=new Node;
newNode->SetData(o);
//first node of circular list
if(head==NULL)
{
head=newNode;
tail=newNode;
newNode->SetNext(tail);
}
//nodes already exists(inserting at end)
else
{
newNode->SetNext(head);
Minahil Ismail 21-NTU-CS-1339

tail->SetNext(newNode);
head=newNode;
}
}

void CircularList::InsertNodeBetween(int b,int a, int c)


{
Node *newNode=new Node;
newNode->SetData(b);
newNode->SetNext(NULL);
//IF INSERTED NODE IS THE FIRST NODE
if(head==NULL)
{
head=newNode;
tail=newNode;
}
//WHEN NODES ALREADY PRESENT
else
{
Node *temp1=head;
Node *temp2=head;
do
{
if(temp1->GetData()==a)
break;
temp1=temp1->GetNext();
}while(temp1!=head);
Minahil Ismail 21-NTU-CS-1339

do
{
if(temp2->GetData()==c)
break;
temp2=temp2->GetNext();
}while(temp1!=head);

newNode->SetNext(temp2);
temp1->SetNext(newNode);
}
}

void CircularList::DeleteNode(int o)
{
Node *temp=head;
//IF LINK LIST IS EMPTY
if(temp==NULL)
{
cout<<"\nNo Nodes to Delete!\n";
return;
}
//IF IT'S NOT EMPTY
else
{
Node *current=temp;

do
{
Minahil Ismail 21-NTU-CS-1339

if(temp->GetData()==o)
break;
current=temp;
temp=temp->GetNext();
}while(temp!=head);

//First and only Node


if(temp==head && temp==tail)
{
head=NULL;
tail=NULL;
delete temp;
}
//Last Node
else if(temp==tail)
{
current->SetNext(head);
tail=current;
delete temp;
}

//First Node
else if(temp==head && temp->GetNext()!=head)
{
head=head->GetNext();
tail->SetNext(head);
delete temp;
Minahil Ismail 21-NTU-CS-1339

//For Node Somewhere Between


else
{
current->SetNext(temp->GetNext());
delete temp;
}

}
}

void CircularList::SearchNode(int o)
{
Node *temp=head;
do
{
if(temp->GetData()==o)
{
cout<<"\nFound!!\n";
return;
}
temp=temp->GetNext();
}while(temp!=head);
cout<<"\nNot Found!!\n";
}

void CircularList::PrintList()
Minahil Ismail 21-NTU-CS-1339

{
Node *temp=head;
do
{
temp=temp->GetNext();
if(temp!=head)
cout<<temp->GetData()<<"-->";
}while(temp!=head);
cout<<endl;
}

int main()
{
CircularList l1;
l1.InsertNodeEnd(1);
l1.InsertNodeEnd(3);
l1.InsertNodeStart(0);

cout<<"\n---------Traversing List---------\n";
l1.PrintList();

cout<<"\n---------Inserting node 2 between node 1 and node 3---------\n";


l1.InsertNodeBetween(2,1,3);
l1.PrintList();

cout<<"\n---------Deleting node 0 ---------\n";


l1.DeleteNode(0);
Minahil Ismail 21-NTU-CS-1339

l1.PrintList();
cout<<"\n---------Searching Node 3---------\n";
l1.SearchNode(3);
l1.PrintList();
cout<<"\n---------Searching Node 0---------\n";
l1.SearchNode(0);
l1.PrintList();

getch();
return 0;

}Output:

Q#5: A case where circular linked list comes in handy is the solution of the
“Josephus Problem”. Explain the concept of “Josephus Problem” and its
solution with example?
Ans:
Minahil Ismail 21-NTU-CS-1339

A case where circularly linked list comes in handy is the solution of the Josephus Problem.
Consider there are 10 persons. They would like to choose a leader.
The way they decide is that all 10 sit in a circle.
They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is
eliminated.
The count starts with the fifth and the next person to go is the fourth in count.
Eventually, a single person remains.
Minahil Ismail 21-NTU-CS-1339
Minahil Ismail 21-NTU-CS-1339

_________________________________________________________________________

You might also like