Professional Documents
Culture Documents
Fasal 051
Fasal 051
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
Objective(s): Upon completion of this lab session, students will be able to:
Exercise 1
Write a menu driven program that implements single list with integer values and having
following functions.
a. Insert an element at beginning
b. Insert an elemt at the end of the link list
c. Insert an element at position provided by user
d. Search an element
e. Traverse the list
f. Delete an elment from beginning
g. Delete an elment from end
h. Delete an elment from position provided by user.
CODE:
#include<iostream>
using namespace std;
struct node
{
int info;
node *next;
};
node *start;
tempnode->next = start;
start = tempnode;
}
if(ptr->next==NULL)
{
InsertEnd(data);
}
else
{
tempnode = new node();
tempnode->info = data;
tempnode->next = ptr->next;
ptr->next = tempnode;
}
}
void PrintList()
{
node *ptr;
ptr = start;
cout << "LIST: ";
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
if (start == NULL) {
cout << "\nNo List Found";
return;
}
if (found) {
cout << "Element Exists At: " << iteration<<endl;
}
else {
cout << "Element Not Found"<<endl;
}
}
void DeleteBegin()
{
node* temp;
temp = start;
start = temp->next;
free(temp);
}
void DeleteEnd()
{
node* ptr, * pnode=NULL;
ptr = start;
while (ptr->next != NULL)
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
{
pnode = ptr;
ptr = ptr->next;
}
if (ptr == start)
{
start = NULL;
free(ptr);
}
else
{
pnode->next = NULL;
free(ptr);
}
}
void DeletePos()
{
int pos, i = 1;
node* temp, * temp1;
temp = start;
cout << "Enter the Position: ";
cin >> pos;
while (i < pos)
{
temp = temp->next;
i++;
}
temp1 = temp->next;
temp->next = temp1->next;
free(temp1);
}
void main()
{
int opt,data,pos;
bool terminate = false;
do
{
cout << endl;
cout << "Enter Operation: ";
cin >> opt;
switch (opt)
{
case 1:
{
cout << "Enter Data: ";
cin >> data;
InsertBegin(data);
break;
}
case 2:
{
cout << "Enter Data: ";
cin >> data;
InsertEnd(data);
break;
}
case 3:
{
cout << "Enter Data: ";
cin >> data;
cout << "Enter Position: ";
cin >> pos;
InsertPos(data,pos);
break;
}
case 4:
{
cout << "Enter Data to Search: ";
cin >> data;
Search(data);
break;
}
case 5:
{
PrintList();
break;
}
case 6:
{
DeleteBegin();
break;
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
}
case 7:
{
DeleteEnd();
break;
}
case 8:
{
DeletePos();
break;
}
case 9:
{
terminate = true;
cout << endl;
break;
}
default:
{
cout << "Invalid Operation Entered.";
}
}
} while (terminate==false);
system("pause");
OUTPUT:
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
Exercise 2
Write a program in which a pile of books are arranged on a shelf of library .Following
information must be stored
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
Book Id
Title of Book
Author
Books must be arranged in LIFO fashion using single link list.Your program must display
at least 10 books.
CODE:
#include<iostream>
using namespace std;
struct node
{
int id;
string bkname;
string author;
node *next;
};
node *top;
void Pop()
{
node* temp;
temp = top;
top = temp->next;
free(temp);
cout << "Book Removed."<<endl;
}
void Display()
{
node *ptr;
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
ptr = top;
int count = 1;
cout << "PILE: "<<endl;
cout <<
"............................................................................"
<<endl;
while (ptr != NULL)
{
cout <<count<<") "<< "ID: " << ptr->id<<" "<<"Title : "<<ptr-
>bkname<<" "<<"Author : "<<ptr->author<<endl;
ptr = ptr->next;
count++;
}
cout <<
"............................................................................"
<< endl;
cout << endl;
}
void main()
{
int id,opt;
string bkname, author;
bool terminate = false;
do
{
cout << endl;
cout << "Enter Operation: ";
cin >> opt;
switch (opt)
{
case 1:
{
cout << "Enter ID: ";
cin >>id;
cout << "Enter Book Title: ";
cin >> bkname;
cout << "Enter Author: ";
cin >> author;
Push(id,bkname,author);
break;
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
}
case 2:
{
Pop();
break;
}
case 3:
{
Display();
break;
}
case 4:
{
terminate = true;
cout << endl;
break;
}
default:
{
cout << "Invalid Operation Entered.";
}
}
} while (terminate==false);
system("pause");
}
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
OUTPUT:
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
Exercise 3
The appointment scheduling with the doctor requires tokens for checkup. Each patient
visits the doctor according to their token number. Once the patient has visited the doctor
his/her token number gets deleted from the list. Write a program that implements the
scenario using single list. Using Queue along with linked list for its implementation.
CODE:
#include<iostream>
using namespace std;
struct node
{
int token;
node *next;
};
node *Front;
node* Rear;
int flag = 1;
void Pop()
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
{
node* temp;
temp = Front;
Front = temp->next;
free(temp);
cout << "Token Removed."<<endl;
}
void Display()
{
node *ptr;
ptr = Front;
cout << "Queue: ";
while (ptr != NULL)
{
cout << ptr->token<<" ";
ptr = ptr->next;
}
cout << endl;
}
void main()
{
int token,opt;
bool terminate = false;
do
{
cout << endl;
cout << "Enter Operation: ";
cin >> opt;
switch (opt)
{
case 1:
{
cout << "Enter Token: ";
cin >> token;
Push(token);
break;
}
case 2:
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
{
Pop();
break;
}
case 3:
{
Display();
break;
}
case 4:
{
terminate = true;
cout << endl;
break;
}
default:
{
cout << "Invalid Operation Entered.";
}
}
} while (terminate==false);
system("pause");
}
CSL-221 Data structure &
Algorithm
Name: Faisal khan
Enrollment: 02-134221-051
Semester: BS CS 3-A
OUTPUT: