Find first and second element in list C++ - c++

Here's my program for implementing a list in C++. I type elements untill 0. Program shows me first element correctly but second is wrong. I probably make errors in second condition
if (p -> next == first) {
secondElement = first -> data;
}
. Can you say what is wrong with it. Thanks
#include "stdafx.h"
#include "iostream"
using namespace std;
struct Node {
int data;
Node *next;
};
int firstElement;
int secondElement;
int main()
{
Node *first = 0;
Node *p;
cout << "Enter a list" << endl;
int i;
while (true) {
cin >> i;
if (i == 0) break;
p = new Node;
p -> data = i;
p -> next = first;
if (first == 0) {
first = p;
firstElement = first -> data;
}
if (p -> next == first) {
secondElement = first -> data;
}
first = p;
}
cout << "First element is: " << firstElement << endl;
cout << "Second element is: " << secondElement << endl;
cout << "List: ";
p = first;
while (p) {
cout << p -> data << " ";
p = p -> next;
}
cout << endl;
return 0;
}

You can do it like this (I just edited your while loop):
while (true) {
cin >> i;
if (i == 0) break;
p = new Node;
p -> data = i;
p -> next = 0;
if (first != 0 && first->next == 0)
secondElement = p->data;
p -> next = first;
if (first == 0) {
first = p;
firstElement = first -> data;
}
first = p;
}
Hope it is what you want to achieve...

Each time through the loop you're setting the pointer of the element to the first.
p -> next = first;
Then in the check for the second element you're checking whether the pointer is set to the first, which it always is.
if (p -> next == first) // This is always true
You'll have to use some different check to see whether it's the second entry in the list, like:
if (p->next && !p->next->next) // only true second time around
{
secondElement = p -> data; // also note the change here
}

p -> next = first;
......
enter code here
if (p -> next == first) { //it's always true here
you should have had
if (p -> next == 0) {

Related

Singly-linked list issues with delete function along with sorting via strcmp C/C++

My singly linked list consists of char *id; char firstname[15]; char lastname[15]; struct rec *next;.
So my main issue that I am having in this assignment is deleting a record via passing the ID into the delete function. It will delete the ID but the second it get to the line to delete either firstname or lastname it throws this error.
"ChristmasList.exe has triggered a breakpoint. occurred".
My second main error is that my strcmp always compares the same string so it will always return 0, I am also a little shakey on how to do this anyways so even if it returned -1 or 1. I don't know if it would sort properly.
Thank you to anyone that can offer help, I have been stuck on this problem for a long time and need to fix it before we get our next lab assigned as we are adding to this code.
Main:
// ChristmasList.cpp : Adds, Deletes, and prints items in a structured list
// MyName
#include "stdafx.h"
#include "list.h"
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
rec myRec; // pointer to rec structure
int choice = 1; // initializing so choice is not 0 to start
int order; // ascending/descending input
char buf[500]; // buffer array
int result; //result of function calls
while (choice != 0)
{
cout << "Enter 1 to Add an Item.\n";
cout << "Enter 2 to delete a record.\n";
cout << "Enter 3 to Print a list of records.\n";
cout << "Enter 0 to exit the program\n";
cin >> choice; // User input for choice
switch (choice)
{
case 1: //Add
cout << "Enter your ID: "; //ask to enter ID
cin >> buf; //Input for ID into rec
myRec.id = buf;
cout << "Enter your first name: "; // Ask for first name
cin >> myRec.firstname; // Input first name into rec
cout << "Enter your last name: "; // Ask for last name
cin >> myRec.lastname; // Input last name into rec
if (AddItem(myRec)) // Call Add
{
cout << "Success\n";
}
else
{
cout << "\nFailed to add :(\n";
}
break;
case 2: //Delete
cout << "Enter the ID of the record you want to delete: "; // ask to enter ID that wants to be deleted
cin >> buf; // Input id wanting to be deleted
result = DeleteItem(buf); // Call delete
break;
case 3: //Print
cout << "Enter 0 for ascending order and 1 for descending order: "; // ask for ascending or descending
cin >> order; // User input for ascending or descending
PrintList(order); // Call Print
break;
case 0:
break;
}
}
return 0;
}
list.cpp:
#include "stdafx.h"
#include "list.h"
#include<iostream>
#include<cstring>
using namespace std;
rec *first = NULL;
rec *last = NULL;
int counter = 0;
rec *MyNewRec2[500];
int AddItem(rec r)
{
int comp;
int comp2;
rec *MyNewRec;
MyNewRec = new rec;
MyNewRec->id = new char[10];
strcpy_s(MyNewRec->id, strlen(r.id) + 1, r.id); // Copying ID
strcpy_s(MyNewRec->firstname, strlen(r.firstname) + 1, r.firstname);// Copying first name
strcpy_s(MyNewRec->lastname, strlen(r.lastname) + 1, r.lastname);// Copying last name
while (first != NULL)
{
if (*MyNewRec->id == *first->id)
{
cout << "Duplicate ID: " << MyNewRec->id << endl;
return 0;
}
else
{
MyNewRec2[counter] = MyNewRec;
last = MyNewRec2[counter];
first = first->next;
counter++;
return 1;
}
}
if (first == NULL) // If no items are in the list
{
MyNewRec->next = NULL;
first = MyNewRec;
last = MyNewRec;
MyNewRec2[counter] = MyNewRec;
counter++;
}
if (r.lastname >= "97")
{
char *temp;
temp = r.lastname - 32;
}
comp = strcmp(MyNewRec->lastname, (first->lastname - 1));
comp2 = strcmp(MyNewRec->lastname, (last->lastname - 1));
if (comp == -1 && comp <= 0) //Move first back in array
{
MyNewRec2[counter - 1] = NULL;
MyNewRec2[counter - 1] = MyNewRec;
MyNewRec->next = first;
first = MyNewRec;
return 1;
}
else if (comp2 == 1 && comp2 >= 0) //Move last back in array
{
MyNewRec->next = NULL;
last->next = last;
MyNewRec2[counter -1] = last;
last = MyNewRec;
return 1;
}
else
{
while (first->next != NULL)
{
comp = strcmp(MyNewRec->lastname, first->lastname);
if (comp == 1)
{
first = first->next;
return 1;
}
else if (comp == 1 || comp == 0)
{
MyNewRec2[counter - 1] = first;
MyNewRec->next = first->next;
first->next = MyNewRec;
return 1;
}
}
}
return 1;
}
int DeleteItem(char *delid)
{
rec *MyNewRec, *temp;
if (first == NULL)
{
return 0;
}
while (first != NULL)
{
if (*first->id == *delid)
{
MyNewRec = first->next;
//temp = MyNewRec2[counter - 1]; // Random try
//temp->next = MyNewRec; // Random try
delete[] first->id;
delete[] first->firstname;
delete[] first->lastname;
delete first;
counter--;
return 1;
}
else
{
first = first->next;
}
}
return 0;
}
void PrintList(int order)
{
rec *temp = new rec;
if (order == 0)
{
int i = 0;
temp = MyNewRec2[i];
while (temp != NULL)
{
cout.flush() << temp->id << " ";
cout.flush() << temp->firstname << " ";
cout.flush() << temp->lastname << " " << endl;
i++;
temp = MyNewRec2[i];
}
}
temp = MyNewRec2[counter - 1];
if (order == 1)
{
while (temp != NULL)
{
cout.flush() << temp->id << " ";
cout.flush() << temp->firstname << " ";
cout.flush() << temp->lastname << " " << endl;
counter--;
temp = MyNewRec2[counter - 1];
}
}
}
list.h:
struct rec
{
char *id;
char firstname[15];
char lastname[15];
struct rec *next;
};
int AddItem(rec r);
int DeleteItem(char *delid);
void PrintList(int order);
This is your struct:
struct rec
{
char *id;
char firstname[15];
char lastname[15];
struct rec *next;
};
This is how you allocated memory for the new record:
MyNewRec = new rec;
MyNewRec->id = new char[10];
And this is how you deallocated it.
delete[] first->id;
delete[] first->firstname;
delete[] first->lastname;
firstname and lastname members are not pointers that you allocated. They are fixed arrays that were allocated when rec was allocated via new. Or put it another way, if you didn't new it, then you don't delete it.
Remove these two lines:
delete[] first->firstname;
delete[] first->lastname;
I don't have time to debug the rest of the code, but I'll just say this. These next line looks weird and incorrect:
comp = strcmp(MyNewRec->lastname, (first->lastname - 1));
comp2 = strcmp(MyNewRec->lastname, (last->lastname - 1));
I don't get the -1 thing. But I gotta run....

Visual studio 2017 code not outputting after a certain line

I am writing a program that takes 8 user inputted integers and makes a linked list out of them. I have the program print the linked list, then I delete the last node and print the list in reverse. Along the way I've been testing to program to make sure each part works, and it's worked up to the point of printing out the original linked list.
When I finished writing the code for the modification and then printing part, I ran into a problem - the program won't output anything after printing out the original list. So for example, if I input 1,2,3,4,5,6,7,8, it will output:
1
2
3
4
5
6
7
8
and that's it. I've tried putting cout << "testing"; at different points to see where my code stops outputting and the latest point where it successfully outputs is right before the while loop.
I'm not sure why a while loop would cause the program to straight up stop outputting anything, even an arbitrary cout statement that has nothing to do with the while loop itself, so I figured I'd ask on here. I'm using visual studio 2017 if that helps. Thanks for any and all help!
#include <iostream>
using namespace std;
void getdata(int & info); //function that assigns a user inputted value to each node
const int nil = 0;
class node_type // declaration of class
{
public:
int info;
node_type *next;
};
int main()
{
node_type *first, *p, *q, *r, *newnode;
first = new node_type;
newnode = new node_type;
int info;
getdata(info); //first node
(*first).info = info;
(*first).next = nil;
getdata(info); //second node
(*newnode).info = info;
(*first).next = newnode;
(*newnode).next = nil;
p = newnode;
for (int i = 2; i < 8; i++) //nodes 3-8
{
newnode = new node_type;
getdata(info);
(*newnode).info = info;
(*p).next = newnode;
p = newnode;
(*newnode).next = nil;
}
q = first;
while (q != nil) // printing linked list
{
cout << (*q).info << "\n";
q = (*q).next;
}
//deletes last node then reverses list
p = first;
q = (*p).next;
r = (*q).next;
if (first == nil) //if list is empty
cout << "Empty list";
else if ((*first).next == nil) //if list has one node
first = nil;
else if (r == nil) //if list has two nodes
q = nil;
else //general case
{
(*first).next = nil; //last line where when i put a cout << ""; it prints in the output window
while ((*r).next != nil)
{
(*q).next = p;
(*r).next = q;
p = q;
q = r;
r = (*r).next;
}
(*q).next = p;
first = q;
}
q = first;
while (q != nil) // printing newly modified list.
{
cout << (*q).info << "\n";
q = (*q).next;
}
return 0;
}
void getdata(int & info)
{
cout << "Enter number: \n";
cin >> info;
}
else //general case
{
(*first).next = nil; //last line where when i put a cout << ""; it prints in the output window
while ((*r).next != nil)
{
(*q).next = p;
(*r).next = q;
p = q;
q = r;
r = (*r).next;
}
(*q).next = p;
first = q;
}
You run into an infinite loop when reversing your list. That's why it doesn't output anything.
As soon as you link element 2 to element 3, you link element 3 to element 2 in the next step.
That causes an infinite loop when iterating through the list while reversing it.
A proper way to reverse it in the general case would be like that:
else //general case
{
node_type* current = first;
node_type* next = first->next;
(*first).next = nil; //last line where when i put a cout << ""; it prints in the output window
while (next)
{
node_type* temp = next->next;
next->next = current;
current = next;
next = temp;
}
first = current;
}
On a sidenote, you should use bla->foo to dereference, instead of (*bla).foo.

Bidirectional list

The following code calculates the sum of the elements of the unidirectional list items greater than 3 and smaller than 8 and the result of the sum is changed the beginning of the list.
#include <iostream>
using namespace std;
struct List
{
int num;
List* nItem;
};
int Input()
{
int number;
cout << "Enter the number: "; cin >> number;
return number;
}
void MakeList(List **head, int n)
{
if (n > 0) {
*head = new List;
(*head)->num = Input();
(*head)->nItem = NULL;
MakeList(&(*head)->nItem, n - 1);
}
}
void Print(List* head)
{
if (head != NULL) {
cout << head->num << " ";
Print(head->nItem);
}
}
List* Add_start(List* head, int index, int elm)
{
List* p = new List;
p->num = elm;
p->nItem = NULL;
if (head == NULL) {
head = p;
}
else {
List* current = head;
for (int i = 0; (i < index - 1) && (current->nItem != NULL); i++)
{
current = current->nItem;
}
if (index == 0)
{
p->nItem = head;
head = p;
}
else {
if (current->nItem != NULL) {
p->nItem = current->nItem;
}
current->nItem = p;
}
}
return head;
}
int Sum(List* head)
{
int sum = 0;
List* p = head;
while(p) {
if ((p->num > 3) && (p->num < 8))
sum += p->num;
p = p->nItem;
}
return sum;
}
void DeleteList(List* head)
{
if (head != NULL) {
DeleteList(head->nItem);
delete head;
}
}
int main()
{
int n = 10;
List* head = NULL;
cout << "Enter 10 number to the list\n" << endl;
MakeList(&head, n);
int sum = Sum(head);
head = Add_start(head, 0, sum);
cout << "\nList: ";
Print(head);
cout << endl;
DeleteList(head);
system("pause");
return 0;
}
How can I do the same operation with a bidirectional list?
Notes:
A bidirectional (or double linked) list, also has a member pointing to the previous node: this is the whole difference between the 2 list types (as a consequence the first element - or the one at the left of the list, will have this member pointing to NULL). So, when such a node is created/inserted into a list, this new member should be set as well (I commented in the code places where this happens), for the new node and for the one following it (if any).
I modified the way of how a list is created - MakeList replaced by _MakeList2 + MakeList2; the underscore(_) in _MakeList2 specifies that it's somehow private (convention borrowed from Python) - it's not very nice, but I thought it's easier this way
I don't have Visual Studio on this computer, so I used gcc. It complained about system function so I had to add #include <stdlib.h>
I renamed some of the identifiers (List -> Node, Add_start -> AddNode, nItem -> nNode) either because the new names make more sense, or their names are consistent
I tried to keep the changes to a minimum (so the solution is as close as possible to your original post)
I enhanced (by adding an additional argument: toRight (default value: 1)) the Print func, so it can iterate the list both ways - I am iterating right to left (for testing purposes) before deleting the list
I corrected some (minor) coding style issues
Here's the modified code:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node {
int num;
Node *pNode, *nNode; // Add a new pointer to the previous node.
};
int Input() {
int number;
cout << "Enter the number: "; cin >> number;
return number;
}
Node *_MakeList2(int n, Node *last=NULL) {
if (n > 0) {
Node *node = new Node;
node->num = Input();
node->pNode = last;
node->nNode = _MakeList2(n - 1, node);
return node;
}
return NULL;
}
Node *MakeList2(int n) {
return _MakeList2(n);
}
void Print(Node *head, int toRight=1) {
if (head != NULL) {
cout << head->num << " ";
if (toRight)
Print(head->nNode, 1);
else
Print(head->pNode, 0);
}
}
Node* AddNode(Node *head, int index, int elm) {
Node *p = new Node;
p->num = elm;
p->pNode = NULL; // Make the link between this node and the previous one.
p->nNode = NULL;
if (head == NULL) {
head = p;
} else {
Node *current = head;
for (int i = 0; (i < index - 1) && (current->nNode != NULL); i++) {
current = current->nNode;
}
if (index == 0) {
p->nNode = head;
head->pNode = p; // Make link between next node's previous node and the current one.
head = p;
} else {
if (current->nNode != NULL) {
p->nNode = current->nNode;
}
p->pNode = current; // Make the link between this node and the previous one.
current->nNode = p;
}
}
return head;
}
int Sum(Node* head) {
int sum = 0;
Node *p = head;
while(p) {
if ((p->num > 3) && (p->num < 8))
sum += p->num;
p = p->nNode;
}
return sum;
}
void DeleteList(Node *head) {
if (head != NULL) {
DeleteList(head->nNode);
delete head;
}
}
int main() {
int n = 10;
Node *head = NULL, *tail = NULL;
cout << "Enter " << n << " number(s) to the list" << endl << endl;
head = MakeList2(n);
int sum = Sum(head);
head = AddNode(head, 0, sum);
cout << endl << "List: ";
Print(head);
cout << endl;
tail = head;
if (tail) {
while (tail->nNode != NULL)
tail = tail->nNode;
cout << endl << "List reversed: ";
Print(tail, 0);
cout << endl;
}
DeleteList(head);
system("pause");
return 0;
}

Pointers in Linked List [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm new to C++, so my code is not optimal, but at the moment I just want it to work.
I have to write a function that compares every 2 elements (1. and 2., 2. and 3. etc.) of linked list and deletes the first of these two elements if it's smaller than the next one. At the end I have to display the list adter editing. I found some examples of what I'm trying to do, but they make me confused and I have no idea how to adjust them to my case.
Here is my code:
#include <iostream>
#include <cstdlib>
using namespace std;
struct elements {
int value;
elements * next;
};
void f (elements * & start){
if (start == NULL || start -> next == NULL) {
return;
}
elements * p = start, * prevvalue = NULL, * previous = NULL, * del, * before = NULL, * after = NULL;
int first = start -> value;
int second = 0;
for (; p != NULL; p = p -> next){
cout << p -> value << endl;
second = p -> value;
if (first < (p -> value)) {
before = prevvalue;
del = p;
previous = p;
p -> next = after -> next;
after -> next = p;
delete del;
}
first = second;
previous = prevvalue;
}
if (previous == NULL) start = start -> next;
else prevvalue -> next = start -> next;
}
int main (){
elements * start = NULL, * last = NULL, * input;
int count;
cout << "How many elmenets would you like to input? ";
cin >> count;
for (int i = 0; i < count; i++){
cout << "Please input " << i + 1 << ". element: ";
elements * input = new elements;
cin >> input -> value;
input -> next = NULL;
if (last != NULL) {
last -> next = input;
last = input;
}
else start = last = input;
}
f (start);
elements * p = start;
while (p != NULL) {
cout << p -> value << endl;
p = p -> next;
}
input = start;
while (start != NULL){
elements * temp = start;
start = start -> next;
delete temp;
}
return 0;
}
There's something totally wrong with pointers, so it's not working.
Can someone please edit my code and throw out everything that's unnecessary?
Thank you in advance!
After some cleanup something like this
#include <iostream>
#include <cstdlib>
using namespace std;
struct elements {
int value;
elements* next;
};
elements* funkcija(elements *start) {
if (start == NULL || start->next == NULL) {
return start;
}
elements *list = start;
elements *current = start;
elements *previous = nullptr;
while(current) {
elements* next = current->next;
if (!next) {
break;
}
if (current->value < next->value) {
if (previous) {
delete current;
previous->next = next;
}
else {
list = next;
delete current;
}
current = nullptr;
}
else {
previous = current;
}
current = next;
}
return list;
}
int main() {
elements *start = NULL, *last = NULL;
int count;
cout << "How many elmenets would you like to input? ";
cin >> count;
for (int i = 0; i < count; i++) {
cout << "Please input " << i + 1 << ". element: ";
elements* input = new elements;
cin >> input->value;
input->next = NULL;
if (last != NULL) {
last->next = input;
last = input;
}
else {
start = last = input;
}
}
start = funkcija(start);
elements * p = start;
while (p != NULL) {
cout << p->value << endl;
p = p->next;
}
while (start != NULL) {
elements * temp = start;
start = start->next;
delete temp;
}
return 0;
}

Level Order Traversal: Deleting a Subtree

#include <iostream>
using namespace std;
struct node {
int item;
node* l;
node* r;
node (int x) {
item = x;
l = 0;
r = 0;
}
node(int x, node* l, node* r) {
item = x;
this->l = l;
this->r = r;
}
};
typedef node* link;
class QUEUE {
private:
link* q;
int N;
int head;
int tail;
public:
QUEUE(int maxN) {
q = new link[maxN + 1];
N = maxN + 1;
head = N;
tail = 0;
}
int empty() const {
return head % N == tail;
}
void put(link item) {
q[tail++] = item;
tail = tail % N;
}
link get() {
head = head % N;
return q[head++];
}
};
link head = 0; // Initial head of the tree
link find(int x) {
if (head == 0) {
cout << "\nEmpty Tree\n";
return 0;
}
link temp = head;
// To find the node with the value x and return its link
QUEUE q(100);
q.put(temp);
while (!q.empty()) {
temp = q.get();
if (temp->item == x) {
return temp;
}
if (temp->l != 0) q.put(temp->l);
if (temp->r != 0) q.put(temp->r);
}
return 0;
}
void print(link temp) {
QUEUE q(100);
q.put(temp);
while (!q.empty()) {
temp = q.get();
cout << temp->item << ", ";
if (temp->l != 0) q.put(temp->l);
if (temp->r != 0) q.put(temp->r);
}
}
void deleteAll(link h) {
// This deletes the entire binary tree
QUEUE q(100);
q.put(h);
while (!q.empty()) {
h = q.get();
if (h->l != 0) q.put(h->l);
if (h->r != 0) q.put(h->r);
delete h;
}
}
int main() {
link temp = 0;
char c;
int n1, n2;
cout << "\n\nPlease enter the input instructions (X to exit program) : \n\n";
do {
cin >> c;
switch (c) {
case 'C': cin >> n1;
if (head == 0) {
head = new node(n1);
cout << "\nRoot node with item " << n1 << " has been created\n\n";
} else {
cout << "\nError: Tree is not empty\n\n";
}
break;
case 'L': cin >> n1 >> n2;
temp = find(n1);
if (temp != 0) {
if (temp->l == 0) {
temp->l = new node(n2);
cout << "\nNode with item " << n2 << " has been added\n\n";
}
else {
cout << "\nError: The specified node already has a left child\n\n";
}
}
else {
cout << "\nError: The specified node doesn't exist\n\n";
}
break;
case 'R': cin >> n1 >> n2;
temp = find(n1);
if (temp != 0) {
if (temp->r == 0) {
temp->r = new node(n2);
cout << "\nNode with item " << n2 << " has been added\n\n";
}
else {
cout << "\nError: The specified node already has a right child\n\n";
}
}
else {
cout << "\nError: The specified node doesn't exist\n\n";
}
break;
case 'P': cin >> n1;
temp = find(n1);
if (head != 0) {
cout << "\nLevel-order traversal of the entire tree: ";
print(temp);
}
else {
cout << "\nError: No elements to print\n\n";
}
break;
case 'D': cin >> n1;
temp = find(n1);
deleteAll(temp);
temp = 0;
break;
case 'X': cout << "\nExiting Program\n\n";
break;
default: cout << "\nInvalid input entered. Try again.\n\n";
}
} while (c != 'X');
system("pause");
return 0;
}
Sample Input:
C 9
L 9 8
R 9 6
L 8 3
R 8 5
R 6 2
L 3 4
L 4 10
L 5 1
R 5 11
L 1 12
R 1 7
It all works fine until I delete a subtree and print when it prints garbage value before crashing. Please help me figure out the bug because I've been trying in vain for hours now.
It all works fine until I delete a subtree and print when it prints garbage value before crashing. Please help me figure out the bug because I've been trying in vain for hours now.
Try the recursive function:
void Delete(link h)
{
if(h)
{
if(h->l) Delete(h->l);
if(h->r) Delete(h->r);
delete(h);
}
}
When you delete a node, you call deleteAll(temp) which deletes temp, but it doesn't remove the pointer value from the l or r of temp's parent node.
This leaves you with a invalid pointer, causing garbage printing and crashing.
Unfortunately, the way your find works currently, you don't know what the current temp node's parent is when you get around to checking its value.
One way to fix it is to have a different type of find (called something like remove) that looks in l and r at each iteration for the value and sets l or r to NULL before returning the pointer. You might have to have a special case for when the value is found in the root.
Edit (sample code added):
I am assuming you are not using recursion for some reason, so my code uses your existing queue based code. I only changed enough to get it working.
findAndUnlink find the node with the value given and "unlinks" it from the tree. It returns the node found, giving you a completely separate tree. Note: it is up to the caller to free up the returned tree, otherwise you will leak memory.
This is a drop in replacement for find in your existing code, as your existing code then calls deleteAll on the returned node.
link findAndUnlink(int x) {
if (head == 0) {
cout << "\nEmpty Tree\n";
return 0;
}
link temp = head;
if (temp->item == x) {
// remove whole tree
head = NULL;
return temp;
}
// To find the node with the value x and remove it from the tree and return its link
QUEUE q(100);
q.put(temp);
while (!q.empty()) {
temp = q.get();
if (temp->l != NULL) {
if (temp->l->item == x) {
link returnLink = temp->l;
temp->l = NULL;
return returnLink;
}
q.put(temp->l);
}
if (temp->r != NULL) {
if (temp->r->item == x) {
link returnLink = temp->r;
temp->r = NULL;
return returnLink;
}
q.put(temp->r);
}
}
return 0;
}