Swap the first and last nodes in C++ - c++

I'm trying to swap the location of the first and the last node using dev c++, it's an assignment for my grades.
And the following is what I've done so far. I haven't started on the swapping function because I have no idea how.
#include <iostream>
#include <cstdlib>
using namespace std;
// Node class
class Node {
int data;
public:
Node* next;
Node(int d=0){
data = d;
next=NULL;
}
int getData() const{
return data;
}
}; //class Node
void display(Node *start, Node *end)
{
// Temp pointer
Node *tmp = start;
// One node in the list
while (tmp)
{
cout << tmp->getData() << "\t";
tmp=tmp->next;
}
} //display
Node* node;
int main()
{
Node *start, *end, *head, *last, *newNode;
start = end = new Node(5);
for (int n=10; n<=35; n=n+5) {
end->next=new Node(n);
end=end->next;
}
display(start,end);
cin.get();
//delete the first node
head=start;
start = start->next;
delete(head);
display(start,end);
cin.get();
//delete the last node
last = start;
end = start->next;
while(end->next != NULL){
last = end;
end = end->next;
}
last->next = NULL;
delete(end);
display(start,end);
cin.get();
//insert value 3 in front of the node
newNode = new Node(3);
newNode->next = NULL;
newNode->next = start;
start = newNode;
display(start,end);
cin.get();
//insert value 23 in between 20 and 25
int pos = 5;
newNode = new Node(23);
Node *temp;
end = start;
for(int i=1; i<pos-1; i++){
end = end->next;
}
temp = end->next;
end->next = newNode;
newNode->next = temp;
display(start,end);
cin.get();
//here is where the swap function must be performed.
display(start,end); //to display the result
cin.get();
} //main

Find the first, second, last and second last nodes.
first->link = null;
last->link=second;
second_last->link=first;
return last
In case of 2 nodes
first->link=null;
second->link=first;
return second;
You have created a list of nodes. Starting from first.
if( first == NULL)
return ;
else if( first ->next ==NULL)
return first;
else
{
second = first -> next;
if(second ->next == NULL)
{
first->next = NULL;
second->next = first;
return second;
}
else
{
//declare firstcopy,last,second_last
firstcopy = first;
while( firstcopy->next != NULL)
{
second_last = firstcopy;
firstcopy = firstcopy ->next;
last = firstcopy;
}
first->next = null;
last->next=second;
second_last->next=first;
return last;//or you may print the linked list starting from last.
}
}
Code
#include <iostream>
#include <cstdlib>
using namespace std;
// Node class
class Node {
private:
int data;
public:
Node* next;
Node(int d=0):data(d),next(NULL)
{
}
int getData() const{
return data;
}
Node operator=(const Node& b) {
Node box(b.getData());
box.next=b.next;
return box;
}
}; //class Node
void display(Node *start)
{
while(start!=NULL )
{
cout<<start->getData()<<" ";
start=start->next;
}
cout<<endl;
} //display
Node* swap(Node *first){
if( first == NULL)
return first;
else if( first ->next ==NULL)
return first;
else
{
Node *second = first -> next;
if(second ->next == NULL)
{
first->next = NULL;
second->next = first;
return second;
}
else
{
Node* firstcopy,*last,*second_last;
firstcopy=first;
while( firstcopy->next != NULL)
{
second_last = firstcopy;
firstcopy = firstcopy ->next;
last = firstcopy;
}
first->next = NULL;
last->next=second;
second_last->next=first;
return last;//or you may print the linked list starting from last.
}
}
}
Node* node;
int main()
{
Node *start, *end;
start = end = new Node(5);
for (int n=10; n<=35; n=n+5) {
end->next=new Node(n);
end=end->next;
}
display(start);
cin.get();
start = swap(start);
display(start);
cin.get();
} //main

#include <iostream>
#include <cstdlib>
using namespace std;
// Node class
class Node {
int data;
public:
Node* next;
Node(int d=0){
data = d;
next=NULL;
}
int getData() const{
return data;
}
}; //class Node
void display(Node *start, Node *end)
{
// Temp pointer
Node *tmp = start;
// One node in the list
while (tmp)
{
cout << tmp->getData() << "\t";
tmp=tmp->next;
}
} //display
void swap(Node *first, *second, *second_last, *firstcopy){
first->next = NULL;
last->next=second;
second_last->next=first;
return last;
if( first == NULL)
return ;
else if( first ->next ==NULL)
return first;
else{
second = first -> next;
if(second ->next == NULL)
{
first->next = NULL;
second->next = first;
return second;
}
else
{
//declare firstcopy,last,second_last
firstcopy = first;
while( firstcopy->next != NULL)
{
second_last = firstcopy;
firstcopy = firstcopy ->next;
last = firstcopy;
}
first->next = NULL;
last->next=second;
second_last->next=first;
return last;//or you may print the linked list starting from last.
}
}
}
Node* node;
int main()
{
Node *start, *end, *head, *last, *newNode;
start = end = new Node(5);
for (int n=10; n<=35; n=n+5) {
end->next=new Node(n);
end=end->next;
}
display(start,end);
cin.get();
//delete the first node
head=start;
start = start->next;
delete(head);
display(start,end);
cin.get();
//delete the last node
last = start;
end = start->next;
while(end->next != NULL){
last = end;
end = end->next;
}
last->next = NULL;
delete(end);
display(start,end);
cin.get();
//insert value 3 in front of the node
newNode = new Node(3);
newNode->next = NULL;
newNode->next = start;
start = newNode;
display(start,end);
cin.get();
//insert value 23 in between 20 and 25
int pos = 5;
newNode = new Node(23);
Node *temp;
end = start;
for(int i=1; i<pos-1; i++){
end = end->next;
}
temp = end->next;
end->next = newNode;
newNode->next = temp;
display(start,end);
cin.get();
//swap the position of the first and the last node
swap(&first, &second, &second_last, &firstcopy);
display(start,end);
cin.get();
} //main

Related

How Can I Create A Single function that can create multiple Linked Lists

as shown in the code , i have to use 2 similar functions for creating 2 linked lists . isn't there a way i can create as many lists as i want with just one function , i tried using struct Node **p and struct Node *p as a parameter to the function but the didn't work
can someone help me to create multiple linked lists using this same function
and i want to create a append function not a insert function which asks for position as well.
#include <iostream>
using namespace std;
struct Node
{
int data = 10 ;
struct Node *next;
} *first , *second , *third;
void Display(struct Node *p)
{
while (p)
{
cout<<p->data<<" ";
p = p->next ;
}
cout<<"\n";
}
void Append_1(int elem)
{
Node* t , *last;
t = new Node;
t->data = elem;
t->next = NULL;
if(first == 0)
first = last = t;
else
{
last->next = t;
last = t;
}
}
void Append_2(int elem)
{
Node* t , *last;
t = new Node;
t->data = elem;
t->next = NULL;
if(second == 0)
second = last = t;
else
{
last->next = t;
last = t;
}
}
//void SortMerge(struct Node *p , struct Node *q);
int main()
{
Append_1(3);
Append_1(7);
Display(first);
Append_2(10);
Append_2(14);
Append_2(21);
Display(second);
//SortMerge(first , second);
Display(third);
return 0;
}
You can create a class like here:
struct Node{
int data;
Node* next;
Node* previous;
};
class Graph{
public:
Graph(int = 0);
~Graph();
void display_left_right();
void display_right_left();
void append(int);
void append_at_pos(int,int);
void prepend(int);
int get_num_elt();
int get_data_at_pos(int);
private:
Node* head;
Node* tail;
int num_elt=0;
};
Graph::Graph(int first_data){
head = new Node;
head->next = NULL;
head->previous = NULL;
head->data = first_data;
tail = head;
num_elt++;
}
Graph::~Graph(){
Node* main_traverser = head;
while(main_traverser){
main_traverser = head->next;
delete head;
head = main_traverser;
}
std::cout <<"Graph deleted!" << std::endl;
}
void Graph::display_left_right(){
Node* traverser = head;
while(traverser != NULL){
std::cout << traverser->data << " ";
traverser = traverser->next;
}
std::cout << std::endl;
}
void Graph::display_right_left(){
Node* traverser = tail;
while(traverser != NULL){
std::cout << traverser->data << " ";
traverser = traverser->previous;
}
std::cout << std::endl;
}
void Graph::append(int new_data){
Node* add = new Node;
add->data = new_data;
add->next = NULL;
add->previous = tail;
tail->next = add;
tail = add;
num_elt++;
}
void Graph::append_at_pos(int pos, int new_data){
if(pos > num_elt+1 || pos<=0){std::cout << "Wrong position!" << std::endl; return;}
if(pos==1){
prepend(new_data);
return;
}
if(pos==num_elt+1){
append(new_data);
return;
}
Node* add = new Node;
Node* traverser = head;
add->data = new_data;
for(int i=0; i<pos-2; i++){
traverser = traverser->next;
}
add->next = traverser->next;
add->previous = traverser;
traverser->next->previous = add;
traverser->next = add;
}
void Graph::prepend(int new_data){
Node* add = new Node;
add->next = head;
add->previous = NULL;
add->data = new_data;
head->previous = add;
head = add;
num_elt++;
}
int Graph::get_num_elt(){
return num_elt;
}
int Graph::get_data_at_pos(int pos){
Node* traverser = head;
if(pos <=0 || pos> num_elt){std::cout << "Wrong position!" << std::endl; return 0;}
for(int i=0; i<pos-1; i++){
traverser = traverser->next;
}
return traverser->data;
}
main(){
Graph a(2);
a.append(3);
a.append(4);
a.prepend(1);
a.display_left_right();
a.append_at_pos(1,6);
a.display_left_right();
std::cout << "data at 1: " << a.get_data_at_pos(1) << std::endl;
}
When you say "create multiple linked lists," I think you mean creating nodes to a linked list, which you have 2 append functions. I think the reason you have these 2 functions is because you do not know where to start traversing your linked list. For this reason, I think in your main function you should declare the head of the linked list, a single node that is the start. Set it's data and next to null, and then pass the head value into a function so it can start traversing from the head. Here is a generic append function that adds a node on the end, where the parameters are a reference to the head node, and the value for the new node:
void append(Node ** head, int new_data)
{
Node * select_node = * head;
// select node is set to the head node, and will traverse until it is at the end
while (select_node -> next != NULL)
{
// select node is set to the next node until it is NULL (end of linked list)
select_node = select_node -> next;
}
// now that select node is the last node, we need to make it's next value a node
// and that node should be a new node (allocated in heap) with the value of the input value
//and the next value be NULL (because it's the end of the linked list)
Node * next_node = new Node();
next_node -> data = new_data;
next_node -> next = NULL;
select_node -> next = next_node;
}

Doubly LinkedList

I have encountered Segmentation fault in my PopAtEnd Can someone please help me out
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node* prev;
};
Node* head = NULL;
Node* last = NULL;
void InsertAtBeg(int ele){
Node* ptr = new Node();
ptr->data = ele;
if(head == NULL && last == NULL){
head = last = ptr;
ptr->next = NULL;
ptr->prev = NULL;
return;
}
ptr->prev = NULL;
ptr->next = head;
head = ptr;
}
void InsertAtEnd(int ele){
Node* ptr = new Node();
ptr->data = ele;
last->next = ptr;
ptr->next = NULL;
last = ptr;
}
void PopAtBeg(){
Node* temp = head;
head = head->next;
head->prev = NULL;
delete temp;
}
//**Segmentation Fault**//
void PopAtEnd(){
Node* temp = last;
last = last->prev;
last->next = NULL;
delete temp;
}
void display(){
Node* temp = head;
int i = 0;
while(temp->next != NULL){
cout << temp->data <<" ";
temp = temp->next;
i++;
}
cout<<temp->data<<endl;
cout<<"Size of the list is "<<i + 1<<endl;
}
void search(int key){
Node* temp = head;
int i = 0;
while(temp->next != NULL){
if(temp->data == key)
cout<<key<<" is found at index "<<i<<endl;
temp = temp->next;
i++;
}
}
int main()
{
InsertAtBeg(7);
InsertAtBeg(2);
InsertAtBeg(9);
InsertAtEnd(1);
InsertAtEnd(3);
cout<<"The linked list is : ";
display();
search(9);
search(1);
PopAtBeg();
PopAtEnd();
cout<<"The linked list is : ";
display();
}
Thanks In Advance for your help.
As user4581301 said, you have to set prev everywhere not to stay null. For example, what causes segmentation fault in PopAtEnd() is that you say last = last->prev; and last->prev is NULL. So, you have to add this line: ptr->prev = last; in InsertAtEnd() before last = ptr;

C++ Inserting into a linked list

I am writing a program that modifies a linked list. The problem I am having is when inserting nodes into the linked list. The first few nodes are inserted and moved properly, but when reaching the end of the linked list some nodes are either removed or not displayed.
Function for inserting
void LinkedList::insert(int num, int pos)
{
Node *temp1 = new Node;
temp1->data = num;
temp1->next = NULL;
if(pos == 0)
{
temp1->next = head;
head = temp1;
return;
}
Node *temp2 = head;
for(int i = 0; i < pos-1; i++)
{
temp2 = temp2->next;
}
temp1->next = temp2->next;
temp2->next = temp1;
}
Node structure
struct Node
{
int data;
Node *next;
};
int size;
Node *head, *tail;
Driver code
nums.insert(1, 0); // 1 in location 0
nums.insert(5, 4); // 5 in location 4
nums.insert(3, 7); // 3 in location 7
List before insert
8 6 7 8 0 9
List after insert
1 8 6 7 5 8
Expected after insert
1 8 6 7 5 8 0 9 3
Would the values excluded from being display needed to be stored and inserted afterwards? Or is the inserting itself not being coded properly/missing elements?
Thanks for your help.
Full code
#include "linkedlist.h"
LinkedList::LinkedList()
{
head = nullptr;
tail = nullptr;
size = 0;
}
LinkedList::~LinkedList()
{
if(head != nullptr)
{
Node *temp;
while(head != nullptr)
{
temp = head->next;
// deletes head
delete head;
// goes to next element
head = temp;
}
}
}
void LinkedList::display()
{
Node *temp = head;
for(int i = 0; i < size; i++)
{
cout << temp->data << "\t";
temp = temp->next;
}
cout << endl;
}
void LinkedList::append(int num)
{
// list is empty
if(head == nullptr)
{
head = new Node;
head->data = num;
head->next = nullptr;
// sets tail to head
tail = head;
}
else
{
// creates new node
Node *temp = new Node;
// sets new node data
temp->data = num;
temp->next = nullptr;
// sets previous tail link to new node
tail->next = temp;
// sets this node to new tail
tail = temp;
}
// increments size
size++;
}
void LinkedList::pop()
{
if(size > 1)
{
Node *temp = head;
// loops to node before tail
while(temp->next->next != nullptr)
{
temp = temp->next;
}
// deletes tail
delete tail;
// sets new tail
tail = temp;
tail->next = nullptr;
}
// if there's only one item
else if(size == 1)
{
Node *temp = tail;
// head and tail are now null
head = nullptr;
tail = nullptr;
// deletes node
delete temp;
}
size--;
}
int LinkedList::min()
{
int min = head->data;
struct Node *temp = head;
while(temp != nullptr)
{
if(min > temp->data)
{
min = temp->data;
}
temp = temp->next;
}
return min;
}
int LinkedList::max()
{
int max = head->data;
struct Node *temp = head;
while(temp != nullptr)
{
if(max < temp->data)
{
max = temp->data;
}
temp = temp->next;
}
return max;
}
int LinkedList::mean()
{
int sum = 0;
int average = 0;
struct Node *temp = head;
while(temp != nullptr)
{
sum += temp->data;
temp = temp->next;
}
average = sum / size;
return average;
}
void LinkedList::sort()
{
Node *current1 = head;
Node *current2 = head;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size - 1; j++)
{
if(current1->data < current2->data)
{
int temp = current1->data;
current1->data = current2->data;
current2->data = temp;
}
current2 = current2->next;
}
current2 = head;
current1 = head->next;
for(int p = 0; p < i; p++)
{
current1 = current1->next;
}
}
}
void LinkedList::reverse()
{
Node *current1 = head;
Node *current2 = head;
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size - 1; j++)
{
if(current1->data > current2->data)
{
int temp = current1->data;
current1->data = current2->data;
current2->data = temp;
}
current2 = current2->next;
}
current2 = head;
current1 = head->next;
for(int p = 0; p < i; p++)
{
current1 = current1->next;
}
}
}
int LinkedList::linearSearch(int key)
{
Node *search = nullptr;
Node *temp = head;
Node *current = head;
int count = 0;
while(current != NULL && current->data != key)
{
count++;
temp = current;
current = current->next;
}
if(current != NULL)
{
search = current;
current = current->next;
}
key = count;
return key;
}
void LinkedList::insert(int num, int pos)
{
Node *temp1 = new Node;
temp1->data = num;
temp1->next = NULL;
if(pos == 0)
{
temp1->next = head;
head = temp1;
return;
}
Node *temp2 = head;
for(int i = 0; i < pos-1; i++)
{
temp2 = temp2->next;
}
temp1->next = temp2->next;
temp2->next = temp1;
}
You have a size member that your display function uses, but you never increment it in insert. So while the element gets added, you don't increase the size of the list.
Reading your whole code and getting to the solution may consume lot of time but if you want i have a code ready which is properly working. If you wanted to use this. Please go for it and also, please feel free to ask my anything though this link
#include <iostream>
using namespace std;
struct node
{
int data;
node *next;
};
class linked_list
{
private:
node *head,*tail;
public:
linked_list()
{
head = NULL;
tail = NULL;
}
void add_node(int n)
{
node *tmp = new node;
tmp->data = n;
tmp->next = NULL;
if(head == NULL)
{
head = tmp;
tail = tmp;
}
else
{
tail->next = tmp;
tail = tail->next;
}
}
node* gethead()
{
return head;
}
static void display(node *head)
{
if(head == NULL)
{
cout << "NULL" << endl;
}
else
{
cout << head->data << endl;
display(head->next);
}
}
static void concatenate(node *a,node *b)
{
if( a != NULL && b!= NULL )
{
if (a->next == NULL)
a->next = b;
else
concatenate(a->next,b);
}
else
{
cout << "Either a or b is NULL\n";
}
}
};
int main()
{
linked_list a;
a.add_node(1);
a.add_node(2);
linked_list b;
b.add_node(3);
b.add_node(4);
linked_list::concatenate(a.gethead(),b.gethead());
linked_list::display(a.gethead());
return 0;
}

All of nodes in linkedlist are the same, seems insertion is not working

I have a linked list in C++, after inserting several nodes now I see that all of them are the same, although I'm using different values to add to node each time, but it's like all of them are the same, even when trying to change a node all of them are changing together or it's the same node that is always being returned, I don't know.
class node
{
public:
int ochance = 3;
string question;
string option1;
int peopleeffectop1;
int courteffectop1;
int treasuryeffectop1;
string option2;
int peopleeffectop2;
int courteffectop2;
int treasuryeffectop2;
node *next;
};
class list
{
private:
node *head, *tail;
public:
list()
{
head=NULL;
tail=NULL;
}
void createnode(int value , string q , string ans1 , int ans1ef1 , int ans1ef2, int ans1ef3 , string ans2, int ans2ef1 , int ans2ef2, int ans2ef3 )
{
node *temp = new node;
temp->ochance = value;
temp->question = q;
temp->option1 = ans1;
temp->peopleeffectop1 = ans1ef1;
temp->courteffectop1 = ans1ef2;
temp->treasuryeffectop1 = ans1ef3;
temp->option2 = ans2;
temp->peopleeffectop2 = ans2ef1;
temp->courteffectop2 = ans2ef2;
temp->treasuryeffectop2 = ans2ef3;
temp->next = NULL;
if(head == NULL)
{
head = temp;
tail = temp;
temp = NULL;
}
else
{
tail->next = temp;
tail = temp;
}
}
node getnth(int pos)
{
node* tmp = new node;
tmp = head;
int i = 0;
while(tmp!=NULL)
{
if (i=pos)
{
return *tmp;
}
i++;
tmp = tmp->next;
}
}
int getlen()
{
node* tmp = new node;
tmp = head;
int i = 0;
while(tmp!=NULL)
{
i++;
tmp = tmp->next;
}
return i;
}
void minus(int pos)
{
node* tmp = new node;
tmp = head;
int i = 0;
while(tmp!=NULL)
{
if (i=pos)
{
tmp->ochance -=1;
}
i++;
tmp = tmp->next;
}
}
void delete_first()
{
node *temp = new node;
temp = head;
head = head->next;
delete temp;
}
void delete_last()
{
node *current = new node;
node *previous = new node;
current = head;
while(current->next != NULL)
{
previous = current;
current = current->next;
}
tail = previous;
previous->next = NULL;
delete current;
}
void delete_position(int pos)
{
node *current = new node;
node *previous = new node;
current = head;
for(int i = 1; i < pos; i++)
{
previous = current;
current = current->next;
}
previous->next = current->next;
}
};
For starters many member functions has a memory leak as for example in this function
node getnth(int pos)
{
node* tmp = new node;
tmp= head;
//
At first memory was allocated and its address was stored in the pointer tmp and then the pointer is reassigned. As a result the address of the allocated memory is lost and the memory is not deleted.
These statements
node* tmp = new node;
tmp= head;
must be substituted for this one statement
node* tmp = head;
Moreover this function
node getnth(int pos)
{
node* tmp = new node;
tmp= head;
int i =0 ;
while(tmp!=NULL){
if (i=pos) {
return *tmp;
}
i++;
tmp = tmp->next;
}
}
has undefined behavior in case when pos is higher than there are nodes in the list. In this case the function returns nothing.
In the function minus there is used the assignment operator instead of the comparison operator
while(tmp!=NULL){
if (i=pos) {
^^^^^
In this function
void delete_first()
{
node *temp=new node;
temp=head;
head=head->next;
delete temp;
}
there is no check whether head is equal to NULL and tail is not adjusted if it is point to the first node.
The same problems are also in the function delete_last only that instead of the adjacent of the tail node as in the previous function you have to adjust the head node.
This function delete_position has the same drawbacks as the previous functions but also it has a bug in the loop
for(int i=1;i<pos;i++)
A node at position 1 will never be deleted.

deleting a linked list node, C++ function not working

#include <iostream>
using namespace std;
class List {
public:
struct node {
int data;
node *next;
};
node* head = NULL;
node* tail = NULL;
node* temp = NULL;
node* prev = NULL;
public:
void addNum(int num) {
temp = new node;
temp->data = num;
temp->next = NULL;
if (head == NULL) {
head = temp;
tail = temp;
}
else {
tail->next = temp;
tail = temp;
}
}
void PrintList() {
temp = head;
while (temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
}
void DelNum(int num) {
temp = head;
while (temp != NULL) {
if (temp->data == num) {
prev->next = temp->next;
free(temp);
}
temp = prev;
temp = temp->next;
}
}
};
int main() {
List list;
list.addNum(1);
list.addNum(2);
list.addNum(3);
list.addNum(4);
list.addNum(5);
list.addNum(6);
list.DelNum(3);
list.PrintList();
return 0;
}
What is wrong with my DelNum function? When I run the program nothing pops up. Doesn't matter what number I put in.
As mss pointed out the problem is in your DelNum() function where you assign temp = prev;. In your initialization you defined that node* prev = NULL; So, prev = NULL at the point when you assigned it to temp which caused segmentation fault when you try to use it like temp = temp->next;.
Two main problems are there in DelNum function:
first, when you are in while loop
, you should assign
prev = temp;
second, when you have found your target element, after deleting it you have to break out of the loop, which isn't done in your code
below is your corrected code( also correction of some other corner case in DelNum function ):
#include <iostream>
using namespace std;
class List {
public:
struct node {
int data;
node *next;
};
node* head = NULL;
node* tail = NULL;
node* temp = NULL;
node* prev = NULL;
public:
void addNum(int num) {
temp = new node;
temp->data = num;
temp->next = NULL;
if (head == NULL) {
head = temp;
tail = temp;
}
else {
tail->next = temp;
tail = temp;
}
cout<<num<<" is added \n";
}
void PrintList() {
temp = head;
while (temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
}
void DelNum(int num) {
if(head==NULL)//empty
{
cout<<"empty linked list, can't be deleted\n";
return;
}
if(head->next==NULL)//means only one element is left
{
if(head->data==num)
{
node * fordelete=head;
head=NULL;
cout<<num<<"is deleted\n";
delete(fordelete);
}
else
{
cout<<"not found , can't be deleted\n";
}
return;
}
temp = head; // when more than one element are there
prev = temp;
while (temp != NULL) {
if (temp->data == num) {
prev->next = temp->next;
free(temp);
cout<<num<<" is deleted\n";
break;
}
prev= temp;
temp = temp->next;
}
if(temp==NULL)
{
cout<<"not found, can't be deleted\n";
}
}
};
int main() {
List list;
list.addNum(1);
list.addNum(2);
list.addNum(3);
list.addNum(4);
list.addNum(5);
list.addNum(6);
list.PrintList();
list.DelNum(3);
list.DelNum(7);
list.PrintList();
return 0;
}
I hope it will help you.