0% found this document useful (0 votes)
4 views9 pages

Singly Linked List Program

The document provides a comprehensive guide on singly linked list operations in C++, including traversal, searching, insertion, and deletion methods. Each operation is presented as a separate, full program with clean code suitable for exam preparation. The guide concludes by assuring the reader that they are fully prepared for linked list-related exams and offers additional resources for revision.

Uploaded by

farwaahnoor29
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)
4 views9 pages

Singly Linked List Program

The document provides a comprehensive guide on singly linked list operations in C++, including traversal, searching, insertion, and deletion methods. Each operation is presented as a separate, full program with clean code suitable for exam preparation. The guide concludes by assuring the reader that they are fully prepared for linked list-related exams and offers additional resources for revision.

Uploaded by

farwaahnoor29
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

Branch · Branch · MID Preparation Guide

ChatGPT Log in Sign up for free


You’re viewing user-generated content that may be unverified or unsafe. Report

Yes 👍
Content
— created
I’ll giveusing ChatGPT
you ALL Singly Linked List operations as SEPARATE, FULL, EXAM-READY C++ programs

👍
(in English) just like you want
Yes
💯
— I’ll give you ALL Singly Linked List operations as SEPARATE, FULL, EXAM-
Each one is clean + safe + same style so you can memorize easily.
READY C++ programs (in English) just like you want 💯
Each one is clean + safe + same style so you can memorize easily.

📘 1. TRAVERSAL (DISPLAY LIST)


📘
C++
1. TRAVERSAL (DISPLAY LIST)

#include <iostream>
C++
using namespace std;
#include <iostream>
using{ namespace std;
struct Node
int data;
struct
Node* next;Node {
}; int data;
Node* next;
}; = NULL;
Node* head

Node*
// Insert head = NULL;
(helper)
void insert(int val) {
// nInsert
Node* = new (helper)
Node();
void insert(int
n->data = val; val) {
n->next Node* n = new Node();
= head;
n->data = val;
head = n;
} n->next = head;
head = n;
}
// TRAVERSAL
void display() {
// temp
Node* TRAVERSAL
= head;
void display() {
if (tempNode* temp {= head;
== NULL)
cout << "List is empty!" << endl;
if (temp == NULL) {
return;
} cout << "List is empty!" << endl;
return;
}
while (temp != NULL) {
cout << temp->data << " -> ";
tempwhile (temp != NULL) {
= temp->next;
} cout << temp->data << " -> ";
temp<<= endl;
cout << "NULL" temp->next;
} }
cout << "NULL" << endl;
}
int main() {
insert(10);
int main() {
insert(20);
insert(10);
insert(30);
insert(20);
display();
insert(30);
display();

return 0;
}

📘 2. SEARCHING IN LIST

C++

#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head = NULL;

void insert(int val) {


Node* n = new Node();
n->data = val;
n->next = head;
head = n;
}

void search(int key) {


Node* temp = head;
int pos = 1;
bool found = false;

while (temp != NULL) {


if (temp->data == key) {
cout << "Found at position: " << pos << endl;
found = true;
}
temp = temp->next;
pos++;
}

if (!found)
cout << "Element not found!" << endl;
}

int main() {
insert(10);
insert(20);
insert(30);

search(20);
return 0;
}

📘 3. INSERT AT BEGINNING

C++

#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head = NULL;

void insertAtBeginning(int val) {


Node* newNode = new Node();
newNode->data = val;
newNode->next = head;
head = newNode;

cout << "Inserted at beginning" << endl;


}

void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

int main() {
insertAtBeginning(10);
insertAtBeginning(20);

display();

return 0;
}

📘 4. INSERT AT END

C++

#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head = NULL;

void insertAtEnd(int val) {


Node* newNode = new Node();
newNode->data = val;
newNode->next = NULL;

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

Node* temp = head;


while (temp->next != NULL) {
temp = temp->next;
}

temp->next = newNode;
}

void display() {
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

int main() {
insertAtEnd(10);
insertAtEnd(20);
insertAtEnd(30);

display();

return 0;
}

📘 5. DELETE FROM BEGINNING

C++

#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head = NULL;

void deleteFromBeginning() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
}

Node* temp = head;


head = head->next;
delete temp;

cout << "Deleted from beginning" << endl;


}

int main() {
head = new Node{10, NULL};
head->next = new Node{20, NULL};

deleteFromBeginning();

return 0;
}

📘 6. DELETE FROM END

C++

#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head = NULL;

void deleteFromEnd() {
if (head == NULL) {
cout << "List is empty!" << endl;
return;
}

if (head->next == NULL) {
delete head;
head = NULL;
return;
}

Node* temp = head;


while (temp->next->next != NULL) {
temp = temp->next;
}

delete temp->next;
temp->next = NULL;

cout << "Deleted from end" << endl;


}

int main() {
head = new Node{10, NULL};
head->next = new Node{20, NULL};
head->next->next = new Node{30, NULL};

deleteFromEnd();

return 0;
}

📘 7. INSERT AT SPECIFIC POSITION

C++

#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head = NULL;

void insertAtPosition(int val, int pos) {


Node* newNode = new Node();
newNode->data = val;

if (pos == 1 || head == NULL) {


newNode->next = head;
head = newNode;
return;
}

Node* temp = head;


for (int i = 1; i < pos - 1 && temp != NULL; i++) {
temp = temp->next;
}

if (temp == NULL) {
cout << "Invalid position!" << endl;
delete newNode;
return;
}

newNode->next = temp->next;
temp->next = newNode;
}

int main() {
insertAtPosition(10, 1);
insertAtPosition(20, 2);
insertAtPosition(15, 2);

return 0;
}

📘 8. DELETE FROM SPECIFIC POSITION

C++

#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

Node* head = NULL;

void deleteFromPosition(int pos) {


if (head == NULL) {
cout << "List is empty!" << endl;
return;
}

Node* temp = head;

if (pos == 1) {
head = head->next;
delete temp;
return;
}

for (int i = 1; i < pos - 1 && temp != NULL; i++) {

temp = temp->next;
}

if (temp == NULL || temp->next == NULL) {


cout << "Invalid position!" << endl;
return;
}

Node* nodeToDelete = temp->next;


temp->next = nodeToDelete->next;
delete nodeToDelete;
}

int main() {
head = new Node{10, NULL};
head->next = new Node{20, NULL};
head->next->next = new Node{30, NULL};

deleteFromPosition(2);

return 0;
}

🎯 FINAL RESULT
👉 Now you have:
✔ All singly linked list operations
✔ Separate full programs
✔ Clean exam format
✔ Safe logic

💯 YOU ARE NOW:


👉 FULLY PREPARED FOR LINKED LIST IN EXAM + VIVA

If you want next:

👉 I can give 1-page revision sheet (super helpful before exam)


👉 OR most repeated viva questions (very important)

Just say 🔥

You might also like