Professional Documents
Culture Documents
Enrollment #: 01-134192-070
Class/Section: BSCS-3A
Objective
This lab session is aimed at enhancing the algorithmic development skills of the students by
writing small utility functions for linked lists. In addition, the students will also implement the
‘Stack’ class using a linked list.
Exercises
Exercise 1
A stack can be implemented using a linked list. The first node can serve as the ‘top’ of Stack and
‘push’ and ‘pop’ operations can be implemented by adding and removing nodes at the head of
the linked list. Implement the Stack class using a linked list and provide all the standard member
functions.
Code:
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
class Node {
public:
int Data;
Node* Next;
};
class Stack {
Node* Top;
public:
Stack()
{
Top = NULL;
}
Data Structures and Algorithms Page 1
Lab Journal – Lab 3
bool isempty()
{
if (Top == NULL)
return true;
else
return false;
}
void push(int temp)
{
Node* topush = new Node;
topush->Data = temp;
topush->Next = NULL;
if (Top != NULL)
{
topush->Next = Top;
}
Top = topush;
}
int pop()
{
int temp;
if (Top != NULL)
{
temp = Top->Data;
Top = Top->Next;
return temp;
}
else
{
cout << "Top empty.";
return NULL;
}
}
int top()
{
int temp;
if (Top != NULL)
{
temp = Top->Data;
return temp;
}
else
{
cout << "Top empty.";
return NULL;
}
}
};
int main()
{
Stack S1;
if (S1.isempty())
cout << "Stack Empty.\n";
else
cout << "Stack has Data.\n";
S1.push(10);
S1.push(20);
Data Structures and Algorithms Page 2
Lab Journal – Lab 3
S1.push(30);
S1.push(40);
cout << " POP First Element: " << S1.pop() << endl;
cout << " POP Second Element: " << S1.pop() << endl;
cout << " Top Value Now : " << S1.top() << endl;
system("pause");
return 0;
}
Output:
Exercise 2
Create an organized linked list in ascending order i.e., all the entries should be added in the list
in ascending order. In order to do so the insert(int value) member function must be modified in
the linked list class. An illustration of the working of the program is presented in the following.
List list ;
list.insert (5) ; //5
list.insert (2) ; //2 5
list.insert (7) ; //2 5 7
Code:
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
class Node {
public:
int Data;
Node* Next;
};
class List {
Node* Top;
public:
List()
{
Top = NULL;
}
bool isempty()
{
if (Top == NULL)
return true;
else
return false;
}
void treverse()
{
Node* x = Top;
if (Top == NULL)
{
cout << "\nLIST IS EMPTY.\n";
}
else
{
while (x != NULL)
{
cout << x->Data << " , ";
x = x->Next;
}
}
}
void insert(int temp)
{
Node* Tempo = new Node;
Tempo->Data = temp;
Tempo->Next = NULL;
if (Top == NULL)
{
Top = Tempo;
}
else if (Top->Next == NULL)
{
if (Top->Data < temp)
Top->Next = Tempo;
else
{
Tempo->Next = Top;
Top = Tempo;
}
}
else
{
Node* x = Top;
Node* y = Top->Next;
while (y != NULL)
{
if (x->Data < temp && y->Data>temp)
{
x->Next = Tempo;
Tempo->Next = y;
}
else if (x->Data > temp)
{
Tempo->Next = x;
Top = Tempo;
}
x = x->Next;
y = y->Next;
}
}
}
};
int main()
{
List S1;
if (S1.isempty())
cout << "List is empty.\n";
else
{
cout << "List has some data.\n";
}
cout << "\nEntering Data Now. ";
S1.insert(5);
S1.insert(21);
S1.insert(2);
S1.insert(10);
if (S1.isempty())
cout << "\n\nList is empty.\n";
else
cout << "\n\nList has some data.\n";
cout << "\n\nTreverse : ";
S1.treverse();
cout << endl;
system("pause");
return 0;
}
Output:
Exercise 3
Code:
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
class Node {
public:
char Data;
Node* Next;
};
class List {
Node* Top;
public:
List()
{
Top = NULL;
}
bool isempty()
{
if (Top == NULL)
return true;
else
return false;
}
void treverse()
{
Node* x = Top;
if (Top == NULL)
{
cout << "\nLIST IS EMPTY.\n";
}
else
{
while (x != NULL)
{
cout << x->Data << " , ";
x = x->Next;
}
}
}
void insert(char temp)
{
Node* Tempo = new Node;
Tempo->Data = temp;
Tempo->Next = NULL;
if (Top == NULL)
{
Top = Tempo;
}
else
{
Node* x = Top;
while (x->Next != NULL)
{
x = x->Next;
}
x->Next = Tempo;
}
}
List concate(List nex)
{
if (Top == NULL)
{
Top = nex.Top;
}
else
{
Node* x = Top;
while (x->Next != NULL)
{
x = x->Next;
}
x->Next = nex.Top;
}
List x;
x.Top = Top;
return x;
}
void intersection(List nex)
{
Data Structures and Algorithms Page 7
Lab Journal – Lab 3
y = y->Next;
}
y = nex.Top;
x = x->Next;
}
}
}
int lexi(List temp)
{
int a;
Node* x = Top;
Node* y = temp.Top;
while (x != NULL && y != NULL)
{
if (x->Data == y->Data)
{
a = 0;
}
else if (x->Data > y->Data)
{
a = 1;
}
else
a = -1;
}
return a;
}
};
int main()
{
List S1, S2, S3;
S1.insert('a');
S1.insert('b');
S1.insert('c');
S1.insert('d');
S2.insert('d');
S2.insert('x');
S2.insert('y');
S2.insert('z');
cout << "\n\n Treverse List 1: ";
Data Structures and Algorithms Page 8
Lab Journal – Lab 3
S1.treverse();
cout << "\n\n Treverse List 2: ";
S2.treverse();
S3 = S1.concate(S2);
cout << "\n\n Treverse Concatenated List : ";
S3.treverse();
cout << "\n\n Intersection : ";
S1.intersection(S1);
if (S1.isempty())
cout << "\n List is empty.\n";
else
cout << "\n List has some data.\n";
system("pause");
return 0;
}
Output:
Implement the given exercises and get them checked by your instructor.
2. Exercise 2
3. Exercise 3a.
4. Exercise 3b.
5. Exercise 3c.
+++++++++++++++++++++++++