0% found this document useful (0 votes)
91 views4 pages

Lab 07 The Linked List As A Data Structure

This document discusses linked lists and exercises for implementing a linked list data structure in C++. It includes exercises to write methods for a LinkedList class to perform common linked list operations like inserting nodes, searching for nodes, removing nodes, and emptying the list. Additional exercises are provided to implement removal of a known node, considering cases for removing the first, last, and interior nodes. Sample code is given for a Node struct and LinkedList class template to get started, along with a main function to test the linked list implementation.

Uploaded by

vignesvaran
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)
91 views4 pages

Lab 07 The Linked List As A Data Structure

This document discusses linked lists and exercises for implementing a linked list data structure in C++. It includes exercises to write methods for a LinkedList class to perform common linked list operations like inserting nodes, searching for nodes, removing nodes, and emptying the list. Additional exercises are provided to implement removal of a known node, considering cases for removing the first, last, and interior nodes. Sample code is given for a Node struct and LinkedList class template to get started, along with a main function to test the linked list implementation.

Uploaded by

vignesvaran
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/ 4

Lab 07 The Linked List as a Data Structure

Exercise 1: Understanding the Vector Class (30 min)


The C++ vector class has the following 2 methods:
size() Returns the number of elements in the vector.
capacity() Returns the number of elements that the vector could contain without
allocating more storage.
The following program inserts 20 elements into a vector. Then remove one element from the vector at
a time until the vector is empty. Answer the following questions:
i) When will the vector increase its capacity?
ii) By how much does the vector increase its capacity each time?
iii) Does the vector ever decrease its capacity automatically?
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib> // system("PAUSE");
using namespace std;
int main() {
vector<int> v1;
cout << "Adding 20 items to vector.\n";
cout << "vector automatically doubles the capacity of its internal dynamic
array when it is full.\n";
cout << "Size\tCapacity\n";
cout << v1.size() << "\t" << v1.capacity() << endl;
for (int i = 0; i < 20; i++) {
v1.push_back (i);
cout << v1.size() << "\t" << v1.capacity() << endl;
}
system("PAUSE");
cout << "Removing the items in vector.\n";
cout << "vector never automatically reduces its array capacity.\n";
cout << "Size\tCapacity\n";
while (!v1.empty()) {
v1.erase (v1.begin());
cout << v1.size() << "\t" << v1.capacity() << endl;
}
}

Exercise 2: Creating a Linked List Structure (80 min)


Use a LinkedList class to complete the program below by writing the methods needed to perform
the following operations:
displayNodes Display the list of nodes.
insertFront Inserting a node to the beginning of a list.
insertBack Inserting a node to the end of a list.
insertAfter Inserting a node after a target node.
search Search for a node from a list (returns a boolean value).
makeEmpty Empty the list by deleting all nodes.
#include <iostream>
#include <string>
using namespace std;
class Point {
int x;
int y;
public:
Point (int x = 0, int y = 0) : x(x), y(y) {}
friend ostream& operator<< (ostream& os, Point& p) {
// Add code.
}
friend istream& operator>> (istream& is, Point& p) {
cout << "Input x y: ";
// Add code.
}
friend bool operator== (Point& left, Point& right) {
// Add code.
}
};
template <typename T>
struct Node {
T info;
Node<T> *next;
};
template <typename T>
class LinkedList {
Node<T> *start;
public:
LinkedList() {
// Add code.
}
~LinkedList() {
// Add code.
}

void insertFront (T& newElement) {


// Add code.
}
void insertBack (T& newElement) {
// Add code.
}
bool insertAfter (T& target, T& newElement) {
// Add code.
}
bool search (T& target) {
// Add code.
}
void makeEmpty () {
// Add code.
}
friend ostream& operator<< (ostream& os, LinkedList<T>& list) {
// Add code.
}
};
int main() {
int seed = 0;
// seed for automatic value of x and y.
LinkedList<Point> list;
int choice; // user choice.
bool found;
Point newPoint, target;
do {
cout << "Choice:\n"
<< "1: Display points\n"
<< "2: Insert new point at the front\n"
<< "3: Insert new point at the end\n"
<< "4: Insert new point after a target\n"
<< "5: Search a point\n"
<< "6: Empty the list\n"
<< "Others: Exit\n";
cin >> choice;
switch (choice) {
case 1 : cout << list << endl;
break;
case 2 : newPoint = Point(++seed, seed);
list.insertFront (newPoint);
break;
case 3 : newPoint = Point(++seed, seed);

list.insertBack (newPoint);
break;
case 4 : cin >> target;
newPoint = Point(++seed, seed);
found = list.insertAfter (target, newPoint);
if (!found) cout << "Target not found\n";
break;
case 5 : cin >> target;
found = list.search (target);
if (!found) cout << "Target not found\n";
else
cout << "Target found\n";
break;
case 6 : list.makeEmpty();
cout << "List is emptied\n\n";
break;
}
} while (choice >= 1 && choice <= 6);
}
Additional Exercise: Remove A Node (20 min)
[Complete this as homework if you run out of time during tutorial session]
Add another function to perform the removal of a known node from the list.
You need to carefully consider all possible scenarios: What if the node is the last node in the linked
list? What if the node is the first node?

You might also like