ccc121 l3
ccc121 l3
Objective:
To explore and implement basic operations on a singly linked list in C++. By the end of the activity, students
should understand how to create nodes, link them, and perform various operations on the linked list.
Materials/Tools:
Instructions:
In this lab, you will create a program that performs operations to manipulate a singly linked list in C++, such as:
You will receive a partially completed code defining a linked list's basic structure. Your task is to implement the
missing operations.
Task Breakdown
1. Node Structure:
Create a structure Node that will hold an integer data and a pointer next to the next node.
struct Node { // uses struct in creating a node //a linked list is composed of nodes
int data; // declares an integer variable named data that will store the node's value or content.
Node *next; // pointer to the next node
// A linked list comprises these "nodes," which store data and a reference (pointer) to the next node in the list.
};
2. Class LinkedList:
Define a class called LinkedList to represent the list. It will have two pointers, head and tail, representing the
list's start and end.
You’ll declare functions to perform various operations. Here is the starting structure of your class:
Write a function addNodeBeginning(int value) to add a node to the beginning of the list. Similarly, write a
function addNodeEnd(int value) to add a node to the end of the list.
4. Deleting Nodes:
Write a function deleteNodeBeginning() to remove the first node from the list. Similarly, write a function
deleteNodeEnd() to remove the last node from the list.
Notes:
void LinkedList::deleteNodeBeginning() {
// Function used to delete the node at the beginning of the linked list;
the second node becomes the new head using the pointer.
}
void LinkedList::deleteNodeEnd() {
// Function used to delete the node at the end of the linked list;
the second last node becomes the new tail using the pointer.
}
Write a function displayList() that prints all the nodes from head to tail.
void LinkedList::displayList() {
// Traverse and print each node's data
}
LABORATORY 03: SINGLY LINKED LIST AND ITS OPERATIONS
6. Main Program:
Your program’s main() function should allow users to choose which operation they want to perform (using a list
of linked list operations and switch statements). Use the following structure:
int main() {
LinkedList myList;
int choice, value;
while (true) {
cout << "Link List Operations: " << endl;
cout << "1. Add a node at the beginning" << endl;
cout << "2. Add a node at the end" << endl;
cout << "3. Delete a node from the beginning" << endl;
cout << "4. Delete a node from the end" << endl;
cout << "5. Display list" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value to add: ";
cin >> value;
myList.addNodeBeginning(value);
break;
case 2:
cout << "Enter value to add: ";
cin >> value;
myList.addNodeEnd(value);
break;
case 3:
myList.deleteNodeBeginning();
break;
case 4:
myList.deleteNodeEnd();
break;
case 5:
myList.displayList();
break;
case 6:
return 0;
default:
cout << "Invalid choice!" << endl;
}
}
}
Additional Notes:
#include <iostream>
using namespace std;
2. You can break down your code into smaller steps, test each operation as you go along, and verify that it
works correctly before moving on to the next.
3. Consider edge cases, such as trying to delete from an empty list or inserting nodes into a list with only one
element.
Submission:
In-Lab Submission: Once your linked list code works as expected, call the instructor to verify your output and score your
lab output.
MOLE Submission: After completing and testing your linked list code, submit it via MOLE. Fill out the lab template by
copying your code, taking a screenshot of the output, and writing a brief reflection on the challenges you faced and how you
resolved them, along with your key takeaways from the activity.
LABORATORY 03: SINGLY LINKED LIST AND ITS OPERATIONS
CODE
OUTPUT
Key Takeaways: