You are on page 1of 25

CAPITAL UNIVERSITY OF SCIENCE AND TECHNOLOGY

“Data-Structures”
Lab -06
Aimen Nadeem
BCS201011

1
Task # 01:
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};

class List
{
public:
Node* head; int count = 0;
List()
{
head = NULL;
}
void insert(int val, int x, int num)
{
if (count < 10)
{
Node* temp = new Node();
temp->data = val;

2
temp->next = NULL;
if (head == NULL)
{
head = temp;
count++;
}
else
{
if (x == 1)
{
count++;
temp->next = head;
head = temp;
}
else if (x == 2)
{
count++;
Node* current = head;
while (current->next != NULL)
{
current = current->next;
}
current->next = temp;
}
else if (x == 3)
{

3
int x = 0;
Node* current = head;
while (current->next != NULL)
{
if (current->data == num)
{
temp->next = current->next;
current->next = temp;
x = 1;
count++;
break;
}
current = current->next;
}
if (x == 0)cout << "Number Not Found" << endl;
}
}
}
else
cout << "List is Full\n" << endl;
}
void delet(int x, int num)
{
if (x == 4)
{

4
if (count > 1)
{
Node* temp = head;
head = temp->next;
delete temp;
}
else
head = 0;
count--;

}
else if (x == 5)
{

if (count > 1)
{
Node* current = head;
while (current->next->next != NULL)
{
current = current->next;
}
current->next = NULL;
}
else
head = 0;
count--;

5
}
else if (x == 6)
{

Node* current = head;


if (count > 1)
{
if (current->data == num)
{
head = current->next; count--;
}
else
{
while (current != NULL)
{
if (current->next->data == num)
{
current->next = current->next->next;
count--; break;
}
else
{
current = current->next;
}

6
}
}
}
else
{
count--; head = 0;
}
}
}
void display()
{
Node* current = head;
cout << "Data is" << endl;
if (count > 0)
{
do
{
cout << " " << current->data;
if (current->next == NULL)
break;
else
current = current->next;
} while (1);
cout << endl << endl;
}
else

7
cout << "Empty" << endl;
}

};
int main()
{
int op = 0, val = 0, pos = 0; List ls;
do
{
cout << "1 Insert front\n";
cout << "2 Insert End\n";
cout << "3 Insert after number\n";
cout << "4 Delete front\n";
cout << "5 Delete End\n";
cout << "6 Delete any number from list\n";
cout << "7 Display all element\n ";
cout << "selcet any one : ";
cin >> op;
switch (op)
{
case 1:
cout << "Enter Value : "; cin >> val;
ls.insert(val, op, pos);
break;
case 2:
cout << "Enter Value : "; cin >> val;

8
ls.insert(val, op, pos);
break;
case 3:
cout << "Enter Value : "; cin >> val;
cout << "Enter Number : "; cin >> pos;
ls.insert(val, op, pos);
break;
case 4:
ls.delet(op, pos);
break;
case 5:
ls.delet(op, pos);
break;
case 6:
cout << "Enter Number : "; cin >> pos;
ls.delet(op, pos);
break;
case 7:
ls.display();
break;
default:
break;
}
} while (1);
}

9
Task # 02
#include<iostream>
using namespace std;
class Node
{
public:
int data;
Node* next;
};

class List
{
public:
Node* head; int count = 0;

10
List()
{
head = NULL;
}
void insert(int val, int x, int num)
{
if (count < 10)
{
Node* temp = new Node();
temp->data = val;
temp->next = NULL;
if (head == NULL)
{
head = temp;
count++;
}
else
{
if (x == 1)
{
count++;
temp->next = head;
head = temp;
}
else if (x == 2)
{

11
count++;
Node* current = head;
while (current->next != NULL)
{
current = current->next;
}
current->next = temp;
}
else if (x == 3)
{
int x = 0;
Node* current = head;
while (current->next != NULL)
{
if (current->data == num)
{
temp->next = current->next;
current->next = temp;
x = 1;
count++;
break;
}
current = current->next;
}
if (x == 0)cout << "Number Not Found" << endl;
}

12
}
}
else
cout << "\n List is Full." << endl;
}
void delet(int x, int num)
{
if (x == 4)
{

if (count > 1)
{
Node* temp = head;
head = temp->next;
delete temp;
}
else
head = 0;
count--;

}
else if (x == 5)
{

if (count > 1)
{

13
Node* current = head;
while (current->next->next != NULL)
{
current = current->next;
}
current->next = NULL;
}
else
head = 0;
count--;

}
else if (x == 6)
{

Node* current = head;


if (count > 1)
{
if (current->data == num)
{
head = current->next; count--;
}
else
{
while (current != NULL)
{

14
if (current->next->data == num)
{
current->next = current->next->next;
count--; break;
}
else
{
current = current->next;
}

}
}
}
else
{
count--; head = 0;
}
}
}
void display()
{
Node* current = head;
cout << "Data is" << endl;
if (count > 0)
{
do

15
{
cout << " " << current->data;
if (current->next == NULL)
break;
else
current = current->next;
} while (1);
cout << endl << endl;
}
else
cout << "Empty" << endl;
}
void remove_dup()
{
int i,j,k,n,a[30];
cout<<"Which element to remove\n";
cin>>n;
cout<<"Enter elements of array\n";

for(i=0;i<n;++i)
cin>>a[i];

for(i=0;i<n;++i)
for(j=i+1;j<n;)
{
if(a[i]==a[j])

16
{
for(k=j;k<n-1;++k)
a[k]=a[k+1];

--n;
}
else
++j;
}

cout<<"\n";
for(i=0;i<n;++i)
cout<<a[i]<<" ";

};
int main()
{
int op = 0, val = 0, pos = 0; List ls;
do
{
cout << "1 Insert front\n";
cout<< "2 Insert End\n";
cout<< "3 Insert after number\n";
cout<< "4 Delete front\n";

17
cout<< "5 Delete End\n";
cout<< "6 Delete any number from list\n";
cout<< "7 Display all element\n";
cout<< "8 Remove Duplicate\n";
cout<< "select any one : ";
cin >> op;
switch (op)
{
case 1:
cout << "Enter Value : "; cin >> val;
ls.insert(val, op, pos);
break;
case 2:
cout << "Enter Value : "; cin >> val;
ls.insert(val, op, pos);
break;
case 3:
cout << "Enter Value : "; cin >> val;
cout << "Enter Number: "; cin >> pos;
ls.insert(val, op, pos);
break;
case 4:
ls.delet(op, pos);
break;
case 5:
ls.delet(op, pos);

18
break;
case 6:
cout << "Enter Number : "; cin >> pos;
ls.delet(op, pos);
break;
case 7:
ls.display();
break;
case 8:
ls.remove_dup();
break;
default:
break;
}
} while (1);
}

19
Task # 03
#include<iostream>
#include<string>
using namespace std;
class Node
{
public:
string title, authors; int price; float edtion; Node * next;
};

class List
{
public:

20
Node *head; int count = 0;
List()
{
head = NULL;
}
void insert()
{

Node *temp = new Node();


cin.ignore();
temp->next = NULL;
cout << "Enter author name : "; getline(cin, temp->authors);
cout << "Enter title name : "; getline(cin, temp->title);
cout << "Enter edition : "; cin >> temp->price;
cout << "Enter price : "; cin>> temp->edtion;
if (head == NULL)
{
head = temp;
count++;
}
else
{
count++;
Node *current = head;
while (current->next != NULL)
{

21
current = current->next;
}
current->next = temp;
}

}
void delet()
{
Node *current = head;
while (current->next != NULL)
{
if (current->next->price == 8)
{
current->next = current->next->next;
count--;
}
else
current = current->next;
}
current = head;
if (current->price == 2000 && current->next != NULL)
head = current->next;
else if (current->price == 2000)
{
head = 0; count = 0;
}

22
cout << "After Deleting books * Link List is" << endl; display();
}
void display()
{
Node *current = head;
cout << "Data is" << endl;
if (count > 0)
{
do
{
cout << "author : " << current->authors << endl;
cout << "title : " << current->title << endl;
cout << "price : " << current->price << endl;
cout << "edition : " << current->edtion << endl << endl;
if (current->next == NULL)
break;
else
current = current->next;
} while (1);
cout << endl << endl;
}
else
cout << "Empty" << endl;
}
void counter()
{

23
cout << "Node Present in List is : " << count << endl;
}

};
int main()
{
int op = 0; List ls;
do
{
cout << "1 to add student\n";
cout << "2 to delete students of semester 3\n ";
cout << "3 to display list\n" ;
cout << "select any one : ";
cin >> op;
switch (op)
{
case 1:
ls.insert();
break;
case 2:
ls.delet();
break;
case 3:
ls.display();
break;

24
default:
break;
}
} while (1);
}

25

You might also like