0% found this document useful (0 votes)
8 views

Practical File On Data Structures Using C++: Submitted To: Submitted by

Uploaded by

Ash
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)
8 views

Practical File On Data Structures Using C++: Submitted To: Submitted by

Uploaded by

Ash
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/ 51

PRACTICAL

FILE ON
DATA STRUCTURES USING C++

Submitted To: Submitted By:


Dr. Yogita Sangwan Anushka Gupta
Assistant Professor M.C.A. 1st Sem
Class Roll No: 23/MCA/

Department Of Computer Science & Applications


Kurukshetra University, Kurukshetra
Session 2023-2025
Sr. No. Program Page Sign
No.
1. Program to implement insertion , deletion and traversing with an array.

2. Program to implement different operations of stack using array.

3. Program to implement different operations of queue using array.

4. Program to implement different operations of a circular queue using an array.

5. Program to implement linear search using an array.

6. Program to implement binary search using an array.

7. Program to implement selection sort using an array.

8. Program to implement bubble sort using an array.

9. Program to implement insertion sort using an array.

10. Program to implement different operations of stack using linked list.

11. Program to implement different operations of queue using linked list.

12. Program to implement different operations of circular queue using linked list.

13. Program to implement a double linked list.

14. Declarations to create a node and playing with pointers.

15. Program adding or appending a node in the list.

16. Program adding nodes to the list , and then printing from the beginning until the
end.
17. Program to insert the element at the end of the linked list.

18. Program to insert the element at the top of the linked list.

19. Program to insert the element at the middle of the linked list.

20. Program to delete a node from a specific value from the list.

21. Program to delete a first node from the list.

22. Program to delete the last node from the list.


1.Program to implement insertion , deletion and traversing with
an array.
#include <iostream>

using namespace std;

int main() {

const int MAX_SIZE = 100; // Maximum size of the array

int arr[MAX_SIZE]; // Array to hold elements

int size = 0; // Current number of elements in the array

while (true) {

int choice, value;

// Display menu

cout << "\nMenu:\n";

cout << "1. Insert\n";

cout << "2. Delete\n";

cout << "3. Traverse\n";

cout << "4. Exit\n";

cout << "Enter your choice: ";

cin >> choice;

switch (choice) {

case 1: // Insert

if (size < MAX_SIZE) {

cout << "Enter element to insert: ";

cin >> value;

arr[size] = value; // Add the element

size++; // Increase the size

cout << "Inserted: " << value << endl;

} else {

cout << "Array is full!" << endl;

}
break;

case 2: // Delete

cout << "Enter element to delete: ";

cin >> value;

bool found = false;

for (int i = 0; i < size; i++) {

if (arr[i] == value) {

found = true;

// Shift elements to the left

for (int j = i; j < size - 1; j++) {

arr[j] = arr[j + 1];

size--; // Decrease the size

cout << "Deleted: " << value << endl;

break;

if (!found) {

cout << "Element not found!" << endl;

break;

case 3: // Traverse

if (size == 0) {

cout << "Array is empty!" << endl;

} else {

cout << "Array elements: ";

for (int i = 0; i < size; i++) {

cout << arr[i] << " "; // Print each element

cout << endl;

}
break;

case 4: // Exit

return 0; // Exit the program

default:

cout << "Invalid choice! Please try again." << endl;

return 0;

}
// 2. To implement stack operations using array.

#include <iostream>
using namespace std;
int stack[100], n=100, top=-1;
void push(int val)
{
if(top>=n-1)
cout<<"Stack Overflow"<<endl;
else
{
top++;
stack[top]=val;
}
}
void pop()
{
if(top<=-1)
{
cout<<"Stack Underflow"<<endl;
}
else
{
cout<<"The popped element is "<< stack[top]
<<endl; top--;
}
}

void display()
{
if(top>=0)
{
cout<<"Stack elements are:"<<endl;
for(int i=top; i>=0; i--) cout<<stack[i]<<"\
n";
cout<<endl;
}
else
cout<<"Stack is empty"<<endl;
}
int main()
{
int ch, val;
cout<<"Roll No. 23/MCA/92."<<endl;
cout<<"1. Push in stack"<<endl;
cout<<"2. Pop from stack"<<endl;
cout<<"3. Display stack"<<endl;
cout<<"4. Exit"<<endl;
do {
cout<<"Enter choice: "<<endl;
cin>>ch;
switch(ch)
{
case 1: {
cout<<"Enter value to be pushed:"<<endl;
cin>>val;
push(val);
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
//3. To implement Queue using Array.

#include<iostream>
using namespace std;

int queue[100], n = 100, front = - 1, rear = - 1;


void push() {
int val;
if (rear == n - 1)
cout<<"Queue Overflow"<<endl;
else {

if (front == - 1)
front = 0;
cout<<"Insert the element in queue : "<<endl;
cin>>val;
rear++;
queue[rear] = val;
}
}
void pop() {
if (front == - 1 || front > rear) {
cout<<"Queue Underflow "<<endl;
return ;
}
else {
cout<<"Element deleted from queue is : "<< queue[front] <<endl; front+
+;;
}
}

void display() {
if (front == -1 || front>rear)
{
cout<<"Queue is empty"<<endl;
}
else
{
cout<<"Queue elements are : "<<"\n";
for (int i = front; i <= rear; i++)
cout<<queue[i]<<"\n";
cout<<endl;
}
}
int main() {
int ch;
cout<<"Roll No. 23/MCA/92."<<endl;
cout<<"1. Insert element to queue"<<endl;
cout<<"2. Delete element from queue"<<endl;
cout<<"3. Display all the elements of queue"<<endl;
cout<<"4. Exit"<<endl;
do {
cout<<"Enter your choice : "<<endl;
cin>>ch;
switch (ch) {
case 1: push();
break;
case 2:
pop(); break;
case 3: display();
break;
case 4: cout<<"Exit"<<endl;
break;
default: cout<<"Invalid choice"<<endl;
}
} while(ch!=4);
return 0;
}
//4. To implement operations in circular queue using array in C++.

#include <iostream>
using namespace std;
int cqueue[5];
int front = -1, rear = -1, n=5;

void insertCQ(int val) {


if ((front == 0 && rear == n-1) || (front == rear+1))
{ cout<<"Queue Overflow \n";
return;
}
if (front == -1) {
front = 0;
rear = 0;
}
else
{
if (rear == n - 1)
rear = 0;
else
rear = rear + 1;

}
cqueue[rear] = val ;
}
void deleteCQ() {
if (front == -1) {
cout<<"Queue Underflow\n";
return ;
}
cout<<"Element deleted from queue is : "<<cqueue[front]<<endl;
if (front == rear) {
front = -1;
rear = -1;
}
else
{
if (front == n - 1)
front = 0;
else
front = front + 1;
}
}
void displayCQ() {
int f = front, r = rear;
if (front == -1) {
cout<<"Queue is empty"<<endl;
return;
}
cout<<"Queue elements are :\n";
if (f <= r) {
while (f <= r){
cout<<cqueue[f]<<"\n";
f++;
}
}
else
{
while (f <= n - 1) {
cout<<cqueue[f]<<"\n"; f+
+;

}
f = 0;

while (f <= r) {
cout<<cqueue[f]<<"\n";
f++;
}
}
cout<<endl;
}
int main() {
int ch, val;
cout<<"Roll No. 23/MCA/92"<<endl;
cout<<"1. Insert\n";
cout<<"2. Delete\n";
cout<<"3. Display\n";
cout<<"4. Exit\n";
do {
cout<<"Enter choice : "<<endl;
cin>>ch;
switch(ch) {
case 1:
cout<<"Input for insertion: "<<endl;
cin>>val;
insertCQ(val);
break;
case 2:
deleteCQ();
break;
case 3:
displayCQ();
break;
case 4:
cout<<"Exit\n";
break;
default: cout<<"Incorrect!\n";
}
} while(ch !=
4); return 0;
}
// 5. To implement Linear Search.

#include<iostream>
using namespace std;
int main()
{
int n, arr[n], i, num, index;
cout<<"Roll No. 23/MCA/92"<<endl;
cout<<"Enter total no. of elements: ";
cin>>n;
cout<<"Enter elements:"<<endl;
for(i=0; i<n; i++)
{
cin>>arr[i];
}
cout<<"\nEnter a Number to Search:
"; cin>>num;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
index =
i; break;
}
}
cout<<"\nFound at Index No."<<index;
cout<<endl;
return 0;
}
//6. Program to insert binary search

#include <iostream>
using namespace std;

int main() {
int n; // Number of elements
cout << "Enter total number of elements: ";
cin >> n;

int arr[n]; // Declare the array

cout << "Enter sorted elements:\n";


for (int i = 0; i < n; i++) {
cin >> arr[i]; // Input sorted elements
}

int target; // Number to search for


cout << "Enter a number to search: ";
cin >> target;

int left = 0, right = n - 1; // Initial left and right indices


bool found = false; // Flag to check if found

// Binary search
while (left <= right) {
int mid = left + (right - left) / 2; // Calculate mid index

if (arr[mid] == target) {
cout << "Found at Index: " << mid << endl;
found = true;
break;
} else if (arr[mid] < target) {
left = mid + 1; // Search in the right half
} else {
right = mid - 1; // Search in the left half
}
}

if (!found) {
cout << "Number not found!" << endl; // Number not found
}

return 0;
}
//7.To implement selection sort.

#include<iostream>

using namespace std;

int main()

int n;

cout<<"Roll No. 23/MCA/92"<<endl;

cout<<"Enter total no of elements:";

cin>>n;

cout<<"Enter elements:"<<endl;

int arr[n];

for(int i=0;i<n;i++)

cin>>arr[i];

for(int i=0;i<n-1;i++)

for(int j=i+1;j<n;j++)

if(arr[j]<arr[i])

int temp=arr[j];

arr[j]=arr[i];

arr[i]=temp;

cout<<"Sorted Array is:";

for(int i=0;i<n;i++)

cout<<" "<<arr[i];
}

return 0;

}
//8.To implement bubble sort.

#include<iostream>

using namespace std;

int main()

int n;

cout<<"Roll No. 23/MCA/92"<<endl;

cout<<"Enter total no of elements:"<<endl;

cin>>n;

cout<<"Enter elements:"<<endl;

int arr[n];

for(int i=0;i<n;i++)

cin>>arr[i];

int counter=0;

while(counter<n-1)

for(int i=0;i<n-counter-1;i++)

if(arr[i]>arr[i+1]){

int temp=arr[i];

arr[i]=arr[i+1];

arr[i+1]=temp;

counter++;

cout<<"Sorted elements are:"<<endl;

for(int i=0;i<n;i++)

cout<<" "<<arr[i];
}

return 0;

}
//9.To implement insertion sort.

#include<iostream>

using namespace std;

int main()

int n;

cout<<"Roll No. 23/MCA/92"<<endl;

cout<<"Enter total no of elements:";

cin>>n;

cout<<"Enter elements:"<<endl;

int arr[n];

for(int i=0;i<n;i++)

cin>>arr[i];

for(int i=1;i<n;i++)

int current=arr[i];

int j=i-1;

while(arr[j]>current && j>=0)

arr[j+1]=arr[j];

j--;

arr[j+1]= current;

cout<<"Sorrted elements are:"<<endl;

for(int i=0;i<n;i++)
{

cout<<" "<<arr[i];

return 0;

//10. programto implement different operations of stack using linked list


#include <iostream>
using namespace std;

// Node structure for linked list


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

// Simple Stack class


class Stack {
private:
Node* top; // Pointer to the top of the stack

public:
Stack() : top(nullptr) {} // Constructor to initialize stack

// Push operation
void push(int value) {
Node* newNode = new Node(); // Create a new node
newNode->data = value; // Set the node's data
newNode->next = top; // Link to the previous top
top = newNode; // Update top
}

// Pop operation
void pop() {
if (top == nullptr) {
cout << "Stack is empty!" << endl;
return;
}
Node* temp = top; // Store the top node
top = top->next; // Move top to the next node
delete temp; // Free memory
cout << "Popped from stack." << endl;
}

// Display stack elements


void display() {
Node* temp = top; // Start from the top
if (temp == nullptr) {
cout << "Stack is empty!" << endl;
return;
}
cout << "Stack elements: ";
while (temp) {
cout << temp->data << " "; // Print each element
temp = temp->next; // Move to next node
}
cout << endl;
}
};

int main() {
Stack stack; // Create a stack
int choice, value;

while (true) {
cout << "\n1. Push\n2. Pop\n3. Display\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
stack.display();
break;
case 4:
return 0; // Exit
default:
cout << "Invalid choice! Please try again." << endl;
}
}

return 0;
}

//11. Program to implement different operations of queue using linked list.

#include <iostream>
using namespace std;

// Node structure for linked list


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

// Simple Queue class


class Queue {
private:
Node* front; // Pointer to the front of the queue
Node* rear; // Pointer to the rear of the queue

public:
Queue() : front(nullptr), rear(nullptr) {} // Constructor

// Enqueue operation
void enqueue(int value) {
Node* newNode = new Node(); // Create a new node
newNode->data = value; // Set data
newNode->next = nullptr; // New node will be at the end

if (rear) {
rear->next = newNode; // Link old rear to new node
} else {
front = newNode; // If empty, set front to new node
}
rear = newNode; // Update rear to new node
cout << value << " enqueued." << endl;
}
// Dequeue operation
void dequeue() {
if (front == nullptr) {
cout << "Queue is empty!" << endl;
return;
}
Node* temp = front; // Store front node
front = front->next; // Move front to the next node
cout << temp->data << " dequeued." << endl;
delete temp; // Free memory
if (front == nullptr) {
rear = nullptr; // If queue is empty, reset rear
}
}

// Display queue elements


void display() {
if (front == nullptr) {
cout << "Queue is empty!" << endl;
return;
}
Node* temp = front; // Start from the front
cout << "Queue elements: ";
while (temp) {
cout << temp->data << " "; // Print each element
temp = temp->next; // Move to next node
}
cout << endl;
}
};

int main() {
Queue queue; // Create a queue
int choice, value;

while (true) {
cout << "\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
queue.enqueue(value);
break;
case 2:
queue.dequeue();
break;
case 3:
queue.display();
break;
case 4:
return 0; // Exit
default:
cout << "Invalid choice! Please try again." << endl;
}
}

return 0;
}

//12. Program to implement different operations of circular queue using linked list.

#include <iostream>
using namespace std;

// Node structure for linked list


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

// Circular Queue class using linked list


class CircularQueue {
private:
Node* front; // Pointer to the front of the queue
Node* rear; // Pointer to the rear of the queue

public:
CircularQueue() : front(nullptr), rear(nullptr) {} // Constructor

// Enqueue operation
void enqueue(int value) {
Node* newNode = new Node(); // Create a new node
newNode->data = value; // Set data
if (front == nullptr) { // If the queue is empty
front = newNode; // Set front to new node
rear = newNode; // Set rear to new node
rear->next = front; // Point rear to front (circular)
} else {
rear->next = newNode; // Link the old rear to the new node
rear = newNode; // Update rear to the new node
rear->next = front; // Point rear to front (circular)
}
cout << value << " enqueued." << endl;
}

// Dequeue operation
void dequeue() {
if (front == nullptr) {
cout << "Queue is empty!" << endl;
return;
}
Node* temp = front; // Store front node
if (front == rear) {
// If only one node is in the queue
front = nullptr; // Reset front
rear = nullptr; // Reset rear
} else {
front = front->next; // Move front to the next node
}
cout << temp->data << " dequeued." << endl;
delete temp; // Free memory
}

// Display queue elements


void display() {
if (front == nullptr) {
cout << "Queue is empty!" << endl;
return;
}
Node* temp = front; // Start from the front
cout << "Queue elements: ";
do {
cout << temp->data << " "; // Print each element
temp = temp->next; // Move to next node
} while (temp != front);
cout << endl;
}

// Check if the queue is empty


bool isEmpty() {
return front == nullptr; // Returns true if front is null
}

// Destructor to free memory


~CircularQueue() {
while (!isEmpty()) {
dequeue(); // Dequeue all elements
}
}
};

int main() {
CircularQueue queue; // Create a circular queue
int choice, value;

while (true) {
cout << "\nMenu:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Display\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to enqueue: ";
cin >> value;
queue.enqueue(value);
break;
case 2:
queue.dequeue();
break;
case 3:
queue.display();
break;
case 4:
return 0; // Exit the program
default:
cout << "Invalid choice! Please try again." << endl;
}
}

return 0;
}

//13. Program to implement a double linked list.

#include <iostream>
using namespace std;

// Node structure for doubly linked list


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

Node(int value) : data(value), next(nullptr), prev(nullptr) {} // Constructor


};

// Doubly Linked List class


class DoublyLinkedList {
private:
Node* head; // Pointer to the head of the list

public:
DoublyLinkedList() : head(nullptr) {} // Constructor

// Insert at the beginning


void insertAtBeginning(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode; // If the list is empty
} else {
newNode->next = head; // Link new node to the old head
head->prev = newNode; // Link old head back to the new node
head = newNode; // Update head to the new node
}
cout << value << " inserted at the beginning." << endl;
}

// Insert at the end


void insertAtEnd(int value) {
Node* newNode = new Node(value);
if (head == nullptr) {
head = newNode; // If the list is empty
} else {
Node* temp = head;
while (temp->next) { // Traverse to the end
temp = temp->next;
}
temp->next = newNode; // Link last node to new node
newNode->prev = temp; // Link new node back to the last node
}
cout << value << " inserted at the end." << endl;
}

// Delete a node by value


void deleteNode(int value) {
if (head == nullptr) {
cout << "List is empty. Cannot delete." << endl;
return;
}

Node* temp = head;


while (temp) {
if (temp->data == value) {
if (temp->prev) {
temp->prev->next = temp->next; // Bypass the node
} else {
head = temp->next; // Update head if needed
}
if (temp->next) {
temp->next->prev = temp->prev; // Bypass the node
}
delete temp; // Free memory
cout << value << " deleted." << endl;
return;
}
temp = temp->next;
}
cout << value << " not found in the list." << endl;
}

// Display the list


void display() {
if (head == nullptr) {
cout << "List is empty!" << endl;
return;
}
Node* temp = head;
cout << "Doubly Linked List: ";
while (temp) {
cout << temp->data << " "; // Print each element
temp = temp->next;
}
cout << endl;
}

// Destructor to free memory


~DoublyLinkedList() {
while (head) {
Node* temp = head; // Store current head
head = head->next; // Move head to the next node
delete temp; // Free memory
}
}
};

int main() {
DoublyLinkedList list; // Create a doubly linked list
int choice, value;

while (true) {
cout << "\nMenu:\n";
cout << "1. Insert at Beginning\n";
cout << "2. Insert at End\n";
cout << "3. Delete Node\n";
cout << "4. Display List\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch (choice) {
case 1:
cout << "Enter value to insert at beginning: ";
cin >> value;
list.insertAtBeginning(value);
break;
case 2:
cout << "Enter value to insert at end: ";
cin >> value;
list.insertAtEnd(value);
break;
case 3:
cout << "Enter value to delete: ";
cin >> value;
list.deleteNode(value);
break;
case 4:
list.display();
break;
case 5:
return 0; // Exit the program
default:
cout << "Invalid choice! Please try again." << endl;
}
}

return 0;
}

//14.Declarations to create a node and playing with pointers .

#include<iostream>

using namespace std;

struct Node

int data;

Node *link;

};

int main()

{
Node *Start=NULL;

Node *t;

Node *n;

n=new Node;

n->data=10;

n->link=NULL;

Start=n;

t=n;

n=new Node;

n->data=20;

n->link=NULL;

cout<<Start->data<<endl;

cout<<Start<<endl;

cout<<t->data<<endl;

cout<<t<<endl;
cout<<n->data<<endl;

cout<<n<<endl;

Start->link=n;

cout<<n<<endl;

t=t->link;

cout<<t<<endl;

}
//15. Adding or Appending a Node to the list.
#include <iostream>
using namespace std;
//Declaration to create a node
struct Node
{
int data;
Node *next;
};
// define the head pointer and make it points to null
Node *head = NULL;
// Adding or Appending a Node to the List
void appendNode(int value)
{
Node *n, *curr;
// Allocate a new node & store data
n = new Node();
n->data = value;
n->next = NULL;
// If there are no nodes in the list
// make n the first node
if (head==NULL)
head = n;
else // Otherwise, insert newNode at end
{
// Initialize curr to head of list
curr = head;
// Find the last node in the list
while (curr->next!= NULL)
{
curr = curr->next;
}
// Insert newNode as the last node
curr->next = n;
}
}
int main()
{
appendNode(2);
appendNode(14);
appendNode(26);
return 0;
}
//16. Adding Nodes to the list and then and then printing them from the beginning until the end.

#include <iostream>
using namespace std;
//Declaration to create a node
struct Node
{
int data;
Node *next;
};
// define the head pointer and make it points to null
Node *head = NULL;
// Adding or Appending a Node to the List
void appendNode(int value)
{
Node *n, *curr;
// Allocate a new node & store data
n = new Node();
n->data = value;
n->next = NULL;
// If there are no nodes in the list
// make n the first node
if (head==NULL)
head = n;

else // Otherwise, insert newNode at end


{
// Initialize curr to head of list
curr = head;
// Find the last node in the list
while (curr->next!= NULL)
{
curr = curr->next;
}
// Insert newNode as the last node
curr->next = n;
}
}
// displaying the list from the beginning
void displayList(void)
{
Node *curr;
curr = head;
while (curr!=NULL)
{
cout << curr->data << endl;
curr = curr->next;
}
}
// main function
int main()
{
appendNode(2);
appendNode(14);
appendNode(26);
displayList();
return 0;
}
//17 Insertion at the end of the list.
#include <iostream>
using namespace std;
//Declaration to create a node
struct Node
{
int data;
Node *next;
};
// define the head pointer and make it points to null
Node *head = NULL;
// Adding or Appending a Node to the List
void appendNode(int value)
{
Node *n, *curr;
// Allocate a new node & store data
n = new Node();
n->data = value;
n->next = NULL;
// If there are no nodes in the list
// make n the first node
if (head==NULL)
head = n;
else // Otherwise, insert newNode at end
{
// Initialize curr to head of list
curr = head;
// Find the last node in the list
while (curr->next!= NULL)
{
curr = curr->next;
}
// Insert newNode as the last node
curr->next = n;
}
}
// displaying the list from the beginning
void displayList(void)
{
Node *curr;
curr = head;
while (curr!=NULL)
{
cout << curr->data << endl;
curr = curr->next;
}
}
// main function
int main()
{
appendNode(2);
appendNode(14);
appendNode(26);
displayList();
return 0;
}
//18.Insertion at the top of the list.
#include <iostream>
using namespace std;
//Declaration to create a node
struct Node
{
int data;
Node *next;
};
// define the head pointer and make it points to null
Node *head = NULL;
// Adding or Appending a Node to the List
void InsertNode(int value)
{
Node *n, *curr;
// Allocate a new node & store data
n = new Node();
n->data = value;
n->next = NULL;
// If there are no nodes in the list
// make n the first node
if (head==NULL)
head = n;
else // Otherwise, insert newNode at end
{
// Initialize curr to head of list
curr = head;
// Insert n as the first node
head = n;
// connect the first node to the rest of the nodes
head->next = curr;
}
}
// displaying the list from the beginning
void displayList(void)
{
Node *curr;
curr = head;
while (curr!=NULL)
{
cout << curr->data << endl;
curr = curr->next;
}
}
// main function
int main()
{
InsertNode(2);
InsertNode(14);
InsertNode(26);
displayList();
return 0;
}
//19.Insertion at the middle of the list
#include <iostream>
using namespace std;
//Declaration to create a node
struct Node
{
int data;
Node *next;
};
// define the head pointer and make it points to null
Node *head = NULL;
// Adding or Appending a Node to the List
void InsertNode(int value)
{
Node *n, *curr, *previous=NULL;
// Allocate a new node & store data
n = new Node();
n->data = value;
n->next = NULL;
// If there are no nodes in the list
// make n the first node
if (head==NULL)
head = n;
else // Otherwise, insert n at end
{
curr = head;
// Skip all nodes whose value member is less than value.
while (curr != NULL && curr->data < value)
{
previous = curr;
curr = curr->next;
}
// If n is to be the 1st in the list, insert it before all other nodes
if (previous == NULL)
{
head = n;
n->next = curr;
}
else
{
previous->next = n;
n->next = curr;
}
}
}
// displaying the list from the beginning
void displayList(void)
{
Node *curr;
curr = head;
while (curr!=NULL)
{
cout << curr->data << endl;
curr = curr->next;
}
}
// main function
int main()
{
InsertNode(2);
InsertNode(14);
InsertNode(26);
InsertNode(30);
InsertNode(18);
InsertNode(1);
displayList();
return 0;
}
//20.Delete a node with a specific value from the list
#include <iostream>
using namespace std;
//Declaration to create a node
struct Node
{
int data;
Node *next;
};
// define the head pointer and make it points to null
Node *head = NULL;
// Adding or Appending a Node to the List
void InsertNode(int value)
{
Node *n, *curr, *previous=NULL;
// Allocate a new node & store data
n = new Node();
n->data = value;
n->next = NULL;
// If there are no nodes in the list
// make n the first node
if (head==NULL)
head = n;
else // Otherwise, insert n at end
{
curr = head;
// Skip all nodes whose value member is less than value.
while (curr != NULL && curr->data < value)
{
previous = curr;
curr = curr->next;
}
// If n is to be the 1st in the list, insert it before all other nodes
if (previous == NULL)
{
head = n;
n->next = curr;
}
else
{
previous->next = n;
n->next = curr;
}
}
}
// displaying the list from the beginning
void displayList(void)
{
Node *curr;
curr = head;
while (curr!=NULL)
{
cout << curr->data << endl;
curr = curr->next;
}
}
void DeleteNode(int value)
{
Node *curr, *previous = NULL;
// If the list is empty, do nothing.
if (!head)
return;
// Determine if the first node is the one.
if (head->data == value)
{
curr = head->next;
delete head;
head = curr;
}
else
{
// Initialize nodePtr to head of list
curr = head;
// Skip all nodes whose value member is
// not equal to num.
while (curr != NULL && curr->data != value)
{
previous = curr;
curr = curr->next;
}
// Link the previous node to the node after
// nodePtr, then delete nodePtr.
previous->next = curr->next;
delete curr;
}
}
// main function
int main()
{
InsertNode(2);
InsertNode(14);
InsertNode(26);
InsertNode(30);
InsertNode(18);
InsertNode(1);
cout<<"the list after insertion is:\n";
displayList();
DeleteNode(18);
cout<<"the list after deleting 18 is:\n";
displayList();
return 0;
}
//21. Delete the first node from the list
#include <iostream>
using namespace std;
//Declaration to create a node
struct Node
{
int data;
Node *next;
};
// define the head pointer and make it points to null
Node *head = NULL;
// Adding or Appending a Node to the List
void InsertNode(int value)
{
Node *n, *curr, *previous=NULL;
// Allocate a new node & store data
n = new Node();
n->data = value;
n->next = NULL;
// If there are no nodes in the list
// make n the first node
if (head==NULL)
head = n;
else // Otherwise, insert n at end
{
curr = head;
// Skip all nodes whose value member is less than value.
while (curr != NULL && curr->data < value)
{
previous = curr;
curr = curr->next;
}
// If n is to be the 1st in the list, insert it before all other nodes
if (previous == NULL)
{
head = n;
n->next = curr;
}
else
{
previous->next = n;
n->next = curr;
}
}
}
// displaying the list from the beginning
void displayList(void)
{
Node *curr;
curr = head;
while (curr!=NULL)
{
cout << curr->data << endl;
curr = curr->next;
}
}
void DeleteFirstNode()
{
Node *curr;
// If the list is empty, do nothing.
if (!head)
return;
else
{
curr = head->next;
delete head;
head = curr;
}
}
// main function
int main()
{
InsertNode(2);
InsertNode(14);
InsertNode(26);
InsertNode(30);
InsertNode(18);
InsertNode(1);
cout<<"the list after insertion is:\n";
displayList();
DeleteFirstNode();
cout<<"the list after deleting the first node is:\n";
displayList();
return 0;
}
//22 Delete the last node from the list
#include <iostream>
using namespace std;
//Declaration to create a node
struct Node
{
int data;
Node *next;
};
// define the head pointer and make it points to null
Node *head = NULL;
// Adding or Appending a Node to the List
void InsertNode(int value)
{
Node *n, *curr, *previous=NULL;
// Allocate a new node & store data
n = new Node();
n->data = value;
n->next = NULL;
// If there are no nodes in the list
// make n the first node
if (head==NULL)
head = n;
else // Otherwise, insert n at end
{
curr = head;
// Skip all nodes whose value member is less than value.
while (curr != NULL && curr->data < value)
{
previous = curr;
curr = curr->next;
}
// If n is to be the 1st in the list, insert it before all other nodes
if (previous == NULL)
{
head = n;
n->next = curr;
}
else
{
previous->next = n;
n->next = curr;
}
}
}
// displaying the list from the beginning
void displayList(void)
{
Node *curr;
curr = head;
while (curr!=NULL)
{
cout << curr->data << endl;
curr = curr->next;
}
}
void DeleteLastNode()
{
Node *curr, *previous;
// If the list is empty, do nothing.
if (!head)
return;
else
{
curr = head;
while (curr->next != NULL)
{
previous = curr;
curr = curr->next;
}
}
delete curr;
previous->next = NULL;
}
// main function
int main()
{
InsertNode(2);
InsertNode(14);
InsertNode(26);
InsertNode(30);
InsertNode(18);
InsertNode(1);
cout<<"the list after insertion is:\n";
displayList();
DeleteLastNode();
cout<<"the list after deleting the last node is:\n";
displayList();
return 0;
}

You might also like