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

Write A Program in C++ To Delete A Perticular Node From Linked List. Ans

The document contains code to: 1. Delete a particular node from a linked list in C++. The code handles deleting the node whether it is the head node or a subsequent node. 2. Create a singly linked list of n nodes in C++ and display the nodes in reverse order. The code defines a Node struct, LinkedList class, and functions to add nodes, print the list normally and in reverse order.

Uploaded by

mayank
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)
41 views9 pages

Write A Program in C++ To Delete A Perticular Node From Linked List. Ans

The document contains code to: 1. Delete a particular node from a linked list in C++. The code handles deleting the node whether it is the head node or a subsequent node. 2. Create a singly linked list of n nodes in C++ and display the nodes in reverse order. The code defines a Node struct, LinkedList class, and functions to add nodes, print the list normally and in reverse order.

Uploaded by

mayank
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/ 9

1. Write a program in C++ to delete a perticular node from linked list.

Ans- #include <bits/stdc++.h>


using namespace std;

class Node

public:

int data;

Node *next;

};

void deleteNode(Node *head, Node *n)

if(head == n)

if(head->next == NULL)

cout << "There is only one node." <<

" The list can't be made empty ";

return;

}
head->data = head->next->data;

n = head->next;

head->next = head->next->next;

free(n);

return;

Node *prev = head;

while(prev->next != NULL && prev->next != n)

prev = prev->next;

if(prev->next == NULL)

cout << "\nGiven node is not present in Linked List";

return;
}

prev->next = prev->next->next;

free(n);

return;

void push(Node **head_ref, int new_data)

Node *new_node = new Node();

new_node->data = new_data;

new_node->next = *head_ref;

*head_ref = new_node;

void printList(Node *head)

while(head!=NULL)
{

cout<<head->data<<" ";

head=head->next;

cout<<endl;

int main()

Node *head = NULL;

push(&head,3);

push(&head,2);

push(&head,6);

push(&head,5);

push(&head,11);

push(&head,10);

push(&head,15);

push(&head,12);

cout<<"Given Linked List: ";

printList(head);
cout<<"\nDeleting node "<< head->next->next->data<<" ";

deleteNode(head, head->next->next);

cout<<"\nModified Linked List: ";

printList(head);

cout<<"\nDeleting first node ";

deleteNode(head, head);

cout<<"\nModified Linked List: ";

printList(head);

return 0;

OUTPUT
2. Write a program in C++ to create a singly linked list of n nodes and
display it in reverse order.

Ans- #include <iostream>


using namespace std;

struct node

int t;

node *next;

};

class xyz

private:

int size;

node *head;
public:

xyz()

size=0;

head=new node;

head=0;

~xyz()

void add(node* addMe)

node* newItem=new node;

newItem=addMe;

newItem->next=head;

head=newItem;

void addNode(int num)

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

node *pointer=new node;

cout<<"\nEnter the element.";


cin>>pointer->t;

add(pointer);

void printMe(int num)

node *print=new node;

print=head;

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

cout<<print->t<<endl;

cout<<endl;

};

int main()

xyz *xyzPointer=new xyz;

int num;
cout<<"How many elements do you want to insert?"<<endl;

cin>>num;

xyzPointer->addNode(num);

xyzPointer->printMe(num);

You might also like