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

ccc121 l3

Computer Programming C++

Uploaded by

Kent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views4 pages

ccc121 l3

Computer Programming C++

Uploaded by

Kent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LABORATORY 03: SINGLY LINKED LIST AND ITS OPERATIONS

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:

• C++ Compiler: Any (e.g., Code::Blocks, DevC++)

Instructions:
In this lab, you will create a program that performs operations to manipulate a singly linked list in C++, such as:

1. Adding a node at the linked list's beginning or end.


2. Deleting a node from the linked list's beginning or end.
3. Locating the head and tail of the list.
4. Displaying the entire list from the beginning to the end.

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.

You’ll use the following code as a guide:

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:

class LinkedList { // uses class in creating a linked list


public:
Node *head, *tail; // Head and tail pointers

LinkedList() { // Constructor to initialize head and tail


head = NULL; // initially, head and tail is NULL
tail = NULL;
}
// Define function prototypes here for the operations
void addNodeBeginning(int value); // Add node at the beginning
void addNodeEnd(int value); // Add node at the end
void deleteNodeBeginning(); // Delete node at the beginning
void deleteNodeEnd(); // Delete node at the end
void displayList(); // Display the list from head to tail
};
LABORATORY 03: SINGLY LINKED LIST AND ITS OPERATIONS
3. Adding Nodes:

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.

Here’s a starting point:

• Add at the Beginning:


o Create a new node.
o If the list is empty (head == NULL), set both head and tail to the new node.
o Otherwise, insert the new node before the current head and update the head.
• Add at the End:
o Create a new node.
o If the list is empty, set both head and tail to the new node.
o Otherwise, insert the new node after the current tail and update the tail.

Hint: Use the Node struct to create new nodes.

void LinkedList::addNodeBeginning(int value) { // Implement this function


//Function used to add a node at the beginning of the linked list, replacing the current head of the list
}
void LinkedList::addNodeEnd(int value) { // Implement this function
//Function used to add a node at the end of the linked list, the new tail of the list, next to NULL
}

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:

• Delete from Beginning:


o Check if the list is empty.
o Update the head to point to the next node.
o If deleting the only node in the list, update tail to NULL.
• Delete from End:
o Check if the list is empty.
o Traverse the list to find the second-to-last node.
o Update its next pointer to NULL and update the tail.

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.
}

5. Displaying the List:

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:

1. Make sure to include necessary libraries like:

#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

Names: ______________________________________________ Section: ____________ Date: _____________

CODE

OUTPUT

Key Takeaways:

You might also like