0% found this document useful (0 votes)
5 views10 pages

23-NTU-CS-1170 (Lab 4)

Uploaded by

mahaather45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views10 pages

23-NTU-CS-1170 (Lab 4)

Uploaded by

mahaather45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

National Textile University

Department of Computer
Science
Subject:
DSA
Submitted to:
Sir ARSAL
Submitted by:
Maha Ather

Reg number:
23-NTU-CS-1170
Lab no.:
4
Semester:3
Question 1:
Code:
class node{
private:
int data; //data
node* next; //pointer which points to next node
node* prev;
public:
//set data
void setdata(int data){
this->data=data;
}
//set next
void setnext(node* next){
this->next=next;
}
//set prev
void setprev(node* prev){
this->prev=prev;
}
//get data
int getdata(){
return data;
}
//get next
node* getnext(){
return next;
}
//get prev
node* getprev(){
return prev;
}
};

Question 2:
Code:
class doublylist{
private:
node* first;
node* last;
public:
doublelist(){
first=nullptr;
last=nullptr;
}
void insertdata(int value){
node* newnode=new node();
newnode->setdata(value);
if(first==NULL){
first=newnode;
last=newnode;
newnode->setnext(nullptr);
newnode->setprev(nullptr);
}else{
newnode->setnext(first);
newnode->setprev(nullptr);
first->setprev(newnode);
first = newnode;
}
}
void deletelist(int value){
if(first==NULL){
cout<<"List is empty"<<endl;
return;
}
//delete at specific node
if (first->getdata()==value) {
node* temp = first;
first = first->getnext();
if(first == NULL) {
last = NULL;
} else {
first->setprev(NULL);
}
delete temp;
return;
}
}
void display(){
node* temp=first;
while(temp!=NULL){
cout<<temp->getdata()<<"->";
temp=temp->getnext();
}
}

};

Question 3:
Code:
int main(){
doublylist* list=new doublylist();
list->insertdata(89);
list->insertdata(78);
list->insertdata(11);
list->insertdata(45);
list->deletelist(45);
list->display();
return 0;
}
Output:

Question 4: (circular linked list and josephus problem)


Code:
#include <iostream>
using namespace std;
// Node class
class Node{
public:
int data;
Node* next;
Node(int value){
data=value;
next = NULL;
}
};
// Circular LinkedList class
class CircularLinkedList {
private:
Node* head;
Node* tail;
int size;
public:
CircularLinkedList(){
head=NULL;
tail=NULL;
size=0;
}
// Insert node at the end of the list
void insert(int data) {
Node* newNode=new Node(data);
if(head==NULL){
head=newNode;
tail=newNode;
newNode->next=head;
}
else {
tail->next=newNode;
tail=newNode;
tail->next=head;
}
size++;
}

// Display the list


void display(){
Node* temp=head;
do{
cout<<temp->data<<" ";
temp=temp->next;
}while(temp!=head);
cout<<endl;
}
// Solve Josephus problem
void josephusProblem(int k) {
if(head==NULL){
cout<<"List is empty"<<endl;
return;
}
Node* temp=head;
while(temp->next!=temp){
for(int i=0;i<k-1;i++) {
temp=temp->next;
}
cout<<"Eliminated: "<<temp->next->data<<endl;
Node* eliminated=temp->next;
temp->next=temp->next->next;
if(temp->next==temp){
head=NULL;
}
else if(eliminated==head){
head=temp->next;
}
delete eliminated;
temp=temp->next;
}
cout<<"Survivor: "<<temp->data<<endl;
}
};
int main() {
CircularLinkedList list;
int n, k;
cout<<"Enter the number of people: ";
cin>>n;
cout<<"Enter the elimination interval (k): ";
cin>>k;
for(int i=1;i<=n;i++) {
list.insert(i);
}
cout<<"Initial Circle: ";
list.display();
list.josephusProblem(k);
}

You might also like