0% found this document useful (0 votes)
5 views8 pages

SLL 1

The document contains a C++ program that implements a singly linked list with various functionalities including insertion at the beginning, after a specific node, and at the end, as well as deletion from the beginning, a specific node, and the end. It also includes a display function to traverse and print the list, and a menu-driven interface for user interaction. The program utilizes a struct to define the nodes and manages the linked list using a head pointer.
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)
5 views8 pages

SLL 1

The document contains a C++ program that implements a singly linked list with various functionalities including insertion at the beginning, after a specific node, and at the end, as well as deletion from the beginning, a specific node, and the end. It also includes a display function to traverse and print the list, and a menu-driven interface for user interaction. The program utilizes a struct to define the nodes and manages the linked list using a head pointer.
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/ 8

#include <iostream>

using namespace std;

struct Node {

int data;

Node* next;

};

// Head pointer initialized to NULL

Node* head = NULL;

// Function to insert a node at the beginning

void insertAtBeginning(int value) {

Node* newNode = new Node();

newNode->data = value;

if (head == NULL) {

newNode->next = NULL;

head = newNode;

} else {

newNode->next = head;

head = newNode;

// Function to insert a node after a given node value

void insertAfter(Node*& head, int location, int value) {

Node* newNode = new Node();

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {
head = newNode;

return;

Node* temp = head;

while (temp != NULL && temp->data != location) {

temp = temp->next;

if (temp == NULL) {

cout << "Given node is not found in the list! Insertion not possible!" << endl;

delete newNode;

return;

newNode->next = temp->next;

temp->next = newNode;

// Function to insert a node at the end of the list

void insertAtEnd(Node*& head, int value) {

Node* newNode = new Node();

newNode->data = value;

newNode->next = NULL;

if (head == NULL) {

head = newNode;

return;

}
Node* temp = head;

while (temp->next != NULL) {

temp = temp->next;

temp->next = newNode;

// Function to delete a node from the beginning

void deleteFromBeginning() {

if (head == NULL) {

cout << "List is empty! Deletion is not possible." << endl;

return;

Node* temp = head;

if (temp->next == NULL) {

head = NULL;

} else {

head = temp->next;

delete temp;

cout << "Node deleted from the beginning." << endl;

// Function to delete a specific node from the list

void deleteNode(Node*& head, int key) {

if (head == NULL) {

cout << "List is Empty! Deletion is not possible." << endl;


return;

Node* temp1 = head;

Node* temp2 = NULL;

while (temp1 != NULL && temp1->data != key) {

temp2 = temp1;

temp1 = temp1->next;

if (temp1 == NULL) {

cout << "Given node not found in the list! Deletion not possible." << endl;

return;

if (temp1 == head && temp1->next == NULL) {

head = NULL;

delete temp1;

return;

if (temp1 == head) {

head = head->next;

delete temp1;

return;

if (temp1->next == NULL) {

temp2->next = NULL;

delete temp1;
return;

temp2->next = temp1->next;

delete temp1;

// Function to delete a node from the end of the list

void deleteFromEnd(Node*& head) {

if (head == NULL) {

cout << "List is Empty! Deletion is not possible." << endl;

return;

Node* temp1 = head;

if (temp1->next == NULL) {

cout << "Deleting node: " << temp1->data << endl;

delete temp1;

head = NULL;

return;

Node* temp2 = temp1;

while (temp1->next != NULL) {

temp2 = temp1;

temp1 = temp1->next;

temp2->next = NULL;

cout << "Deleting node: " << temp1->data << endl;


delete temp1;

// Function to display the linked list

void display() {

if (head == NULL) {

cout << "List is empty." << endl;

return;

Node* temp = head;

while (temp != NULL) {

cout << temp->data << " -> ";

temp = temp->next;

cout << "NULL" << endl;

// Main function

int main() {

int choice,location,value,key;

char ch;

do

cout<<"\n Menu Driven Program";

cout<<"\n 1. Insert At Beginning \n 2. Insert After a Specific Node \n 3. Insert At End \n 4.


Delete From Beginning \n 5. Delete a Specific Node \n 6. Delete From End \n 7. Traversing";

cout<<"\n Enter your choice:";

cin>>choice;

switch(choice)

{
case 1: cout<<"\n Enter the value to be inserted:";

cin>>value;

insertAtBeginning(value);

break;

case 2: cout<<"\n Enter location:";

cin>>location;

cout<<"\nEnter value to be inserted:";

cin>>value;

insertAfter(head,location,value);

break;

case 3: cout<<"\n Enter value to be inserted:";

cin>>value;

insertAtEnd(head,value);

break;

case 4: deleteFromBeginning();

break;

case 5: cout<<"\n Enter the value to be deleted:";

cin>>key;

deleteNode(head,key);

break;

case 6: deleteFromEnd(head);

break;

case 7: display();

break;

default:cout<<"wrong choice";

cout<<"\n Do you want to continue?";

cout<<"(Press y/n)";

cin>>ch;

}while(ch=='y');
return 0;

You might also like