0% found this document useful (0 votes)
24 views3 pages

Bsf23006565 Dsa 3rd Assignment

Uploaded by

XEON
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)
24 views3 pages

Bsf23006565 Dsa 3rd Assignment

Uploaded by

XEON
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/ 3

Division of Science & Technology

Department of Information Sciences

Assignment#3

Linked List

Name: M.Faizan

Roll Number: Bsf23006565

Program: BSCS T

Session: Evening T

Section: B T

Semester: 3rd Semester

Course Instructor: Sir Khalid

University of Education, Lahore


Linked List
A linked list is a linear data structure where each element (node) points to the next node in the sequence,
forming a chain. Each node typically contains data and a pointer to the next node. A simple (or singly) linked
list only has pointers going in one direction, so each node only points to the next node in the list.

Basic Operations for a Linked List


1. Insert at Beginning: Adds a new node at the start of the list.
2. Insert at End: Adds a new node at the end of the list.
3. Delete First Node: Removes the first node in the list.
4. Insert at Position: Inserts a new node at a specified position

#include <iostream> head = head->next;


using namespace std; delete temp;
struct Node { }
int data; void insertAtPosition(int value, int position) {
Node* next; Node* newNode = new Node();
}; newNode->data = value;
class LinkedList {
private: if (position == 1) {
Node* head; newNode->next = head;
public: head = newNode;
LinkedList() { return;
head = nullptr; }
}
void insertAtBeginning(int value) { Node* temp = head;
Node* newNode = new Node(); for (int i = 1; i < position - 1 && temp != nullptr; i++) {
newNode->data = value; temp = temp->next;
newNode->next = head; }
head = newNode;
} if (temp == nullptr) {
void insertAtEnd(int value) { cout << "Position out of bounds.\n";
Node* newNode = new Node(); delete newNode;
newNode->data = value; return;
newNode->next = nullptr; }
if (head == nullptr) {
head = newNode; newNode->next = temp->next;
} else { temp->next = newNode;
Node* temp = head; }
while (temp->next != nullptr) {
temp = temp->next; void display() {
} Node* temp = head;
temp->next = newNode; while (temp != nullptr) {
} cout << temp->data << " -> ";
} temp = temp->next;
void deleteFirstNode() { }
if (head == nullptr) { cout << "NULL\n";
cout << "List is empty, nothing to delete.\n"; }
return; };
}
Node* temp = head; int main() {
LinkedList list;

list.insertAtBeginning(10);

list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtEnd(40);
list.insertAtEnd(50);

cout << "List after inserts:\n";


list.display();

list.deleteFirstNode();
cout << "List after deleting the first node:\n";
list.display();

list.insertAtPosition(25, 3);
cout << "List after inserting 25 at position 3:\n";
list.display();

return 0;
}

Dry Run:
Starting with an empty list, we first insert at the beginning with 10, making the list 10 -> NULL. Next, we insert at the
end four times: adding 20 gives 10 -> 20 -> NULL, then adding 30 results in 10 -> 20 -> 30 -> NULL, adding 40 results
in 10 -> 20 -> 30 -> 40 -> NULL, and adding 50 gives 10 -> 20 -> 30 -> 40 -> 50 -> NULL. We then delete the first
node, removing 10, so the list becomes 20 -> 30 -> 40 -> 50 -> NULL. Finally, we insert 25 at position 3, changing the
list to 20 -> 30 -> 25 -> 40 -> 50 -> NULL. Each operation modifies the node pointers accordingly, resulting in the final
list structure.

You might also like