Linked list insert with recursion trouble - c++

So I'm trying to create a linked list in C++ out of a list container class. The list class contains the member head (a node), and insert function, and the node class contains the data I need to add (first name, last name, age). However, I don't think I'm actually creating a list, rather just writing over p during the input loop
Here's the while-loop in the main program that reads data from a file
while(!infile.eof())
{ infile >> first >> last >> age;
// Process if okay
if(infile.good())
a.insert(first,last,age);
};
Here's the actual insert function definition
void list::insert(string first, string last, int age)
{
node *p;
p = new node;
p->first = first;
p->last = last;
p->age = age;
if (head == NULL)
{
head = p;
head->put(cout);
} else
{
if (head->next != NULL)
{
head->put(cout);
insert((p->next)->first, (p->next)->last, (p->next)->age);
} else
{
p->next = p;
p->put(cout);
}
}
}
I can't change the Node header or main program, so I need to use these parameters in the function calls. Any ideas?

Inserting a node in a linked list is simple when the list is empty.
head = p;
and you are done.
Inserting a node at the end of linked list is a little bit involved when the list is not empty. Pictorially, let's say you have:
and you want to add a new node at the end. You will need to make the link between the last node of the existing linked list and the new node so that you will end up with:
In order to be able to do that you have to walk the linked list to get to the last node. Then, you can use:
lastNode->next = p;
and you are done.
Ignoring the calls to create the output, here's what your function would look like.
void list::insert(string first, string last, int age)
{
node *p = new node;
p->first = first;
p->last = last;
p->age = age;
if (head == nullptr)
{
// The simple case
head = p;
}
else
{
// Gather the last node in the linked list.
node* lastNode = head;
while ( lastNode->next != nullptr )
{
lastNode = lastNode->next;
}
lastNode->next = p;
}
}
Using recursion for inserting a node looks very different. Here's an untested suggestion:
void list::insert(node*& ptr, node* p)
{
if ( ptr == nullptr )
{
ptr = p;
}
else
{
insert(ptr->next, p);
}
}
void list::insert(string first, string last, int age)
{
node *p = new node;
p->first = first;
p->last = last;
p->age = age;
insert(head, p);
}
I don't recommend using the recursive method. It not only takes away the readability of the algorithm but is also more expensive at run time. If you have a linked list with a large number of items in it, it might even lead to stack overflow.

Related

Counting occurrence in singly linked list by nodes

I am writing a simple app that gets a list and saves the objects as nodes in a singly linked list and we can add(), remove(), copy(), etc. each node depending on the given data set. each node has a char value which is our data and an int count which counts the occurrence of the related char.
e.g. for a list like
a, a, b, b, c, a
there would be three nodes (since there are three different characters) which are:
[a,3,*next] -> [b,2,*next] -> [c,1,*next] -> nullptr
bool isAvailable() checks if the data is already in the list or not.
Q: When inserting a data there are two options:
The data has not been entered: so we have to create a newNodewith the given data, count=1and *next=NULL.
The data is already entered: so we have to count++ the node that has the same data.
I know if the given data is available or not, but how can I point to the node with same data?
Here's the code:
#include "stdafx.h"
#include<iostream>
using namespace std;
class Snode
{
public:
char data;
int count;
Snode *next;
Snode(char d, int c)
{
data = d;
count = c;
next = NULL;
}
};
class set
{
private:
Snode *head;
public:
set()
{
head = NULL;
tail = NULL;
}
~set();
void insert(char value);
bool isAvailable(char value);
};
set::~set()
{
Snode *t = head;
while (t != NULL)
{
head = head->next;
delete t;
}
}
bool set::isAvailable(char value)
{
Snode *floatingNode = new Snode(char d, int c);
while(floatingNode != NULL)
{
return (value == floatingNode);
floatingNode->next = floatingNode;
}
}
void set::insert(char value)
{
Snode *newNode = new Snode(char d, int c);
data = value;
if (head == NULL)
{
newNode->next = NULL;
head = newNode;
newNode->count++;
}
else
{
if(isAvailable)
{
//IDK what should i do here +_+
}
else
{
tail->next= newNode;
newNode->next = NULL;
tail = newNode;
}
}
}
I know if the given data is available or not, but how can I point to the node with same data?
You'll need to start at the head of the list and iterate along the list by following the next pointers until you find the node with the same data value. Once you've done that, you have your pointer to the node with the same data.
Some other notes for you:
bool set::isAvailable(char value)
{
Snode *floatingNode = new Snode(char d, int c);
while(floatingNode != NULL)
{
return (value == floatingNode);
floatingNode->next = floatingNode;
}
}
Why is this function allocating a new Snode? There's no reason for it to do that, just initialize the floatingNode pointer to point to head instead.
This function always returns after looking at only the first node in the linked list -- which is not the behavior you want. Instead, it should return true only if (value == floatingNode); otherwise it should stay inside the while-loop so that it can go on to look at the subsequent nodes as well. Only after it drops out of the while-loop (because floatingNode finally becomes NULL) should it return false.
If you were to modify isAvailable() slightly so that instead of returning true or false, it returned either floatingPointer or NULL, you'd have your mechanism for finding a pointer to the node with the matching data.
e.g.:
// Should return either a pointer to the Snode with data==value,
// or NULL if no such Snode is present in the list
Snode * set::getNodeWithValueOrNullIfNotFound(char value) const
{
[...]
}
void set::insert(char value)
{
Snode * theNode = getNodeWithValueOrNullIfNotFound(value);
if (theNode != NULL)
{
theNode->count++;
}
else
{
[create a new Snode and insert it]
}
}
You had a lot of problems in your code, lets see what are they:
First of all, Snode doesn't need to be a class, rather you can go with a simple strcut; since we need everything public.(not a mistake, but good practice)
You could simple initialize count = 1 and next = nullptr, so that no need of initializing them throw constructor. The only element that need to be initialized through constructor is Snod's data.
Since c++11 you can use keyword nullptr instead of NULL, which denotes the pointer literal.
Member function bool set::isAvailable(char value) will not work as you think. Here you have unnecessarily created a new Snode and cheacking whether it points to nullptr which doesn't allow you to even enter the loop. BTW what you have written in the loop also wrong. What do you mean by return (value == floatingNode); ? floatingNode is a Snode by type; not a char.
Hear is the correct implementation. Since we don't wanna overwrite the head, will create a Node* pointer and assign head to it. Then iterate through list until you find a match. If not found, we will reach the end of the isAvailable() and return false.
inline bool isAvailable(const char& value)
{
Node *findPos = head;
while(findPos != nullptr)
{
if(findPos -> data == value) return true;
else findPos = findPos->next_node;
}
return false;
}
In void set::insert(char value), your logic is correct, but implementation is wrong. Following is the correct implementation.(Hope the comments will help you to understand.
void insert(const char& value)
{
if(head == nullptr) // first case
{
Node *newNode = new Node(value);
newNode->next_node = head;
head = newNode;
}
else if(isAvailable(value)) // if node available
{
Node *temp = head;
while(temp->data != value) // find the node
temp = temp->next_node;
temp->count += 1; // and count it by 1
}
else // all new nodes
{
Node *temp = head;
while(temp->next_node != nullptr) // to find the null point (end of list)
temp = temp->next_node;
temp = temp->next_node = new Node(value); // create a node and assign there
}
}
Your destructor will not delete all what you created. It will be UB, since your are deleting newly created Snode t ( i.e, Snode *t = head;). The correct implementation is as bellow.(un-comment the debugging msg to understand.)
~set()
{
Node* temp = head;
while( temp != nullptr )
{
Node* next = temp->next_node;
//std::cout << "deleting \t" << temp->data << std::endl;
delete temp;
temp = next;
}
head = nullptr;
}
Last but not least, the naming (set) what you have here and what the code exactly doing are both different. This looks more like a simple linked list with no duplicates. This is however okay, in order to play around with pointers and list.
To make the code or iteration more efficient, you could do something like follows. In the isAvailable(), in case of value match/ if you found a node, you could simply increment its count as well. Then in insert(), you can think of, if node is not available part.
Hope this was helpful. See a DEMO
#include <iostream>
// since you wanna have all of Node in public, declare as struct
struct Node
{
char data;
int count = 1;
Node* next_node = nullptr;
Node(const char& a) // create a constrcor which will initilize data
: data(a) {} // at the time of Node creation
};
class set
{
private:
Node *head; // need only head, if it's a simple list
public:
set() :head(nullptr) {} // constructor set it to nullptr
~set()
{
Node* temp = head;
while( temp != nullptr )
{
Node* next = temp->next_node;
//std::cout << "deleting \t" << temp->data << std::endl;
delete temp;
temp = next;
}
head = nullptr;
}
inline bool isAvailable(const char& value)
{
Node *findPos = head;
while(findPos != nullptr)
{
if(findPos -> data == value) return true;
else findPos = findPos->next_node;
}
return false;
}
void insert(const char& value)
{
if(head == nullptr) // first case
{
Node *newNode = new Node(value);
newNode->next_node = head;
head = newNode;
}
else if(isAvailable(value)) // if node available
{
Node *temp = head;
while(temp->data != value) // find the node
temp = temp->next_node;
temp->count += 1; // and count it by 1
}
else // all new nodes
{
Node *temp = head;
while(temp->next_node != nullptr) // to find the null point (end of list)
temp = temp->next_node;
temp = temp->next_node = new Node(value);
}
}
void print() const // just to print
{
Node *temp = head;
while(temp != nullptr)
{
std::cout << temp->data << " " << temp->count << "\n";
temp = temp->next_node;
}
}
};
int main()
{
::set mySet;
mySet.insert('a');
mySet.insert('a');
mySet.insert('b');
mySet.insert('b');
mySet.insert('c');
mySet.insert('a');
mySet.print();
return 0;
}

trying to make a simpler insertion at end of linked list program.Help needed with minor issues

I have managed to create a simple insertion at beginnning of linked list program but now i am struggling with insertion at end of linked list.
The program seems to be able to take values from user but the output list is not coming correct.Could you help me out?
If possible keep along the lines of my program as i am beginner and won't be able to understand a completely different method.
Logic i used-
If list is empty then insert value at beginning else if list is not empty then travel along the list till the next value being pointed at is NULL and then enter the new value in place of NULL.
#include<iostream>
using namespace std;
struct node
{
int data;
node *next;
};
node *start=NULL;
void insertend(int x)
{
node* temp=new node;
if(start==NULL)
temp->data=x;
else
{
while(temp!=NULL)
{
temp=temp->next;
}
temp->next=x;
}
}
void display()
{
node* disp=new node;
while(disp!=NULL)
{
cout<<disp->data<<endl;
disp=disp->next;
}
}
int main()
{
int x;
char ch;
do
{
cout<<"Enter data";cin>>x;
cout<<endl;
insertend(x);
cout<<"Do you want to continue?(y/n)";cin>>ch;
cout<<endl;
}while(ch=='y');
cout<<"Your list:"<<endl;
display();
}
The entry point to your list is the variable start. But you never set it. Take for example the first item a user inputs. You will call insertend(), it will check that start == NULL, but then it never sets start. You must set start = temp or something similar. You have the same problem in the else section -- you loop through the nodes starting with temp, but you should be starting with start. And again in the function display(), you create a pointer to a node and start looping from it, but it will have no data -- you should use start as the starting point of your loop.
struct node{
int data;
node* next;
};
node *first = NULL, *last = NULL;
void insert(int x){
if(first == NULL){
first = new node;
first->data = x;
first->next = NULL;
}else if(last == NULL){
last = new node;
last->data = x;
first->next = last;
last->next = NULL;
}else{
node *n = new node;
n->data = x;
n->next = NULL;
last->next = n;
last = n;
}
}
As you can see I am keeping track of first and last node in the list. Insert function checks if there is anything in the list with if(first == NULL)part. If there isn't it creates the first node. Similar thing happens with the else if. Finally in the else block we create a new node with data x. Then point the node stored in variable last to our new node and set last to be that node.
Here is the display function:
void display()
{
node *disp =first;
while(disp->next != NULL){
cout << disp->data << " ";
disp = disp->next;
}
cout << disp->data;
}
I also recommend that you do a cleanup after your program is finished running since you are creating new nodes.
void cleanup(node* n)
{
if(n->next == NULL)return delete n;
cleanup(n->next);
delete n;
}
and then at the end of main call cleanup(first)
Hope this makes sense :) Have a nice day!

C++| Merge Linked Lists

So I've been doing a home assignment on data structures and for more than a couple hours I'm in a dead end. To explain, I have to text files ListJava and ListDS where i take information in the format : Name Surname NUM grade . Both of the files contain the same names but not the same order. The assignment basically wants us to merge sort the files.
These are my structures:
typedef struct student
{
string name;
string surname;
int am;
int grade;
}StudentFile;
typedef struct node {
StudentFile element;
struct node* next;
}Node;
typedef struct stud
{
string name;
string surname;
int am;
int grade;
int grade2;
struct stud* next;
}Student;
And here is my function where I merge them:
/*Merge Lists into one*/
Student* MergeLists(Node* headDS, Node* headJava, Student* head)
{
bool flag = false;
Student *a = new Student;
Student *prev = NULL;
Student *temp = NULL;
Node *tempDS = headDS;
Node *tempJava = headJava;
Node *prevJava = NULL;
if (head == NULL)
{
head = a; //mermory alocation for head<Student>
temp = head;
// temp->next = NULL;
}
while (tempDS != NULL)
{
if(head != NULL)
{
if (tempDS->element.surname.compare(tempJava->element.surname) == 0) // if surnames are equal
{
prev = temp;
temp->name = tempDS->element.name;
temp->surname = tempDS->element.surname;
temp->am = tempDS->element.am;
temp->grade = tempDS->element.grade;
temp->grade2 = tempJava->element.grade;
tempJava = tempJava->next;
tempDS = tempDS->next;
temp = temp->next;
flag = false; //meaning that prevJava can get a new value again.
}
else // if DS > Java
{
/*Keep tempJava in mermory while iterating through the next nodes to find the temp that is equal to DS*/
if (flag == false)
{
prevJava = tempJava;
tempJava = tempJava->next;
flag = true;
}
else
{
tempJava = tempJava->next;
}
}
/*temp = temp->next;
tempJava = tempJava->next;
tempDS = tempDS->next;*/
}
prev->next = a;
}
a->next = NULL;
return a;
}
The problem is on temp = temp->next line. Although the first run is perfectly fine and then correctly searches for the ListJava to find an equal name to ListDS temp value is 0xcdcdcdcd {...} and it throws me an exception:
Exception thrown at 0x00C38EF0 in Exercise3_zitima2.exe: 0xC0000005: Access violation reading location 0xCDCDCDE5.
How can i counter this error, I have really searched around tried things here and there but nothing seems to work out. I know this isn't a place to ask for someone to solve my assignment of course, just need a tad guidance.
Your Student struct doesn't have a constructor, so when you allocate one and assign it to a in MergeLists, a->next will have garbage in it. (The 0xCDCDCDCD is what MSVC fills allocated memory with so you can see these sorts of uninitialized usage.)
You either need to have a constructor set the next pointer to NULL or manually set it to NULL after you allocate it.
A merge list function is normally used to merge two already sorted lists. No allocation of nodes is involved. The first node from one list is compared to the first node of the other list. The smaller node is removed from it's list and appended to what is an initially empty list that will end up with the merged nodes. The only node members that are changed are the next pointers. The process is repeated until the end of one of the lists is reached, and then the next pointer of the last node added to the merged list is set to point to the first node of the remainder of the other list, and the merge is done.

how do i append a struct Person in c++

I have a struct Person
struct Person {
string name;
int age;
Person* next;
};
and i need to make a function that appends structs after each other like a linked list. I started off by creating a pointer head which is a nullpointer in the beginning.
using namespace std;
Person* head = nullptr;
void append(Person*& list, const string& name, int age) {
auto p = new Person{name, age, nullptr}; // typ List_Node*
if (list == nullptr) {
list = p;
return;
}
auto last = list;
while (last != nullptr) {
last = last->next;
}
last = p;
}
int main() {
append(head, "First_Person", 21);
append(head, "Second_Person", 22);
// testing the result
head = head->next;
cout << "head->name outside: " << head->name << endl;
return 0;
}
Now the problem i have now is that after the first append the append() function doesn't seem to link the first person to the second person because the program crashes
head = head->next;
Question is: how do i get append() to append more than 1 Person to this linked list?
auto last = list;
while (last != nullptr){
last = last->next;
}
last = p;
When you get out of this loop, last is pointing at nullptr again instead of the last element of the list. You need to check last->next != nullptr instead.
Also, last = p does not do anything useful; it should be last->next = p in order to actually append p to the list.
Your append() is not managing the pointers correctly. Try this instead:
void append(Person*& list, const string& name, int age)
{
auto p = new Person{name, age, nullptr}; // typ List_Node*
if (list == nullptr)
{
list = p;
return;
}
auto last = list;
while (last->next != nullptr)
last = last->next;
last->next = p;
}
If you get rid of the list parameter, you can avoid having to iterate through the entire list on every append:
Person* head = nullptr;
Person* last = nullptr;
void append(const string& name, int age)
{
auto p = new Person{name, age, nullptr};
if (head == nullptr)
head = p;
if (last != nullptr)
last->next = p;
last = p;
}
int main()
{
append("First_Person", 21);
append("Second_Person", 22);
//testing the result
for(Person *p = head; p != nullptr; p = p->next)
std::cout << "name: " << p->name << std::endl;
// free the list
Person *p = head;
while (p != nullptr)
{
Person *next = p->next;
delete p;
p = next;
}
return 0;
}
That being said, you should use std::list or std::forward_list to hold your Person items and let it handle all of these details for you, eg:
#include <list> // or: #include <forward_list>
struct Person
{
string name;
int age;
};
std::list<Person> mylist; // or: std::forward_list<Person> mylist;
void append(const string& name, int age)
{
mylist.push_back(Person{name, age});
}
int main()
{
append("First_Person", 21);
append("Second_Person", 22);
//testing the result
for (auto &person: mylist)
std::cout << "name: " << person.name << std::endl;
return 0;
}
If you are writing this in a non-academic context, then don't reinvent the wheel, and use a standard container like vector or list.
However, assuming the point of the exercise is to learn data structures, then using a builtin container doesn't teach you much.
The problem I see is you are doing nothing but changing the "last" pointer, which doesn't actually append anything to the list. You need to first find the last node (by using the 'last' pointer to move to each node->next until you reach a node which has a null ->next pointer. Then assign that node's ->next to your new node.
Your existing code...
// Already handled empty list case before we get here...
auto last = list; // last pointer set to address list (head)
while (last != nullptr){ // while not at end of list, last points to next node
last = last->next;
}
// after loop, last will always be null, not very useful
last = p; // last points to p (the new node) which doesn't _do_ anything
Consider doing something like:
// Already handled empty list case before we get here...
auto last = list; // last pointer set to address list (head)
while (last && last->next != nullptr){ // while node->next not null, move to next node
last = last->next;
}
// at end of list, last will always point to last node
last->next = p; // link last->next to the new 'p' Person
Keep in mind if you do a lot of list append operations, it is cheaper for your list data structure to keep a head and a tail pointer, and append simply becomes
if(tail)
tail->next = p;
tail = p;
Besides that, it is cheaper to insert items at the head of a list.
p->next = head;
head = p;

Simple Linked List Implementation in C++

I'm a programming student in my first C++ class, and recently we covered linked lists, and we were given an assignment to implement a simple one. I have coded everything but my pop_back() function, which is supossed to return a pointer to the Node that needs to be deleted in Main(). No Node deletion is to be done in the actual function. So my question is:
Would you be willing to help point me in the right direction for my pop_back() function? Also, if you notice anything else that I'm doing wrong, let me know.
Also, this linked list is just to work with strings. In this case, a grocery list, so one string for the quantity of the item(1,2), and one string for the item type. (Milk, Eggs, etc.)
Below I've included my List & Node class implementations, so you can get an idea of what I've done so far.
Node.cpp
Node::Node(void)
{
descrip = " ";
quantity = " ";
previous = NULL;
next = NULL;
}
Node::Node(string q, string d)
{
descrip = d;
quantity = q;
previous = NULL;
next = NULL;
}
Node* Node::GetNext()
{
return next;
}
Node* Node::GetPrevious()
{
return previous;
}
void Node::SetNext(Node * setter)
{
next = setter;
}
void Node::SetPrevious(Node * setter)
{
previous = setter;
}
List.cpp
List::List(void)
{
first = NULL;
last = NULL;
numNodes = 0;
}
Node* List::GetFirst()
{
return first;
}
Node* List::GetLast()
{
return last;
}
void List::SetFirst(Node* setter)
{
first = setter;
}
void List::SetLast(Node* setter)
{
last = setter;
}
int List::GetNumNodes()
{
return numNodes;
}
void List::push_front(Node* item)
{
if (first == NULL)
{
first = item;
last = item;
}
else
{
Node* pFirst = first;
item->SetNext(pFirst);
first = item;
numNodes++;
}
}
void List::push_back(Node * item)
{
if (last == NULL)
{
first = item;
last = item;
}
else
{
last->SetNext(item);
last = item;
numNodes++;
}
}
Node* List::pop_front()
{
Node* temp = first;
first = first->GetNext();
if (first == NULL)
{
temp = first->GetNext();
first = p;
}
if (first == NULL)
{
last = NULL;
}
if (numNodes > 0)
{
numNodes--;
}
return temp;
}
Node* List::pop_back() // this whole function may be wrong, this is just my attempt at it
{
Node* temp;
temp = first;
while((temp->GetNext()) != NULL)
// im stuck here
}
Some pointers:
0x1243bfa3
0x45afc56e
0xdeadbeef
Some more pointers:
You should prefer to initialize your class members in the initialization list, not in the constructor's body.
In C++, unlike C89, we declare and define a function with no parameters as void f();, not void f(void);.
In C++ we commonly reset pointers with 0, not NULL.
See below for what I mean in code.
Good C++ code will try to take advantage of RAII. This implies avoiding primitive pointers for the most part. In this case plain old std::auto_ptr<> would make a perfectly sufficient substitute for the primitve Node* pointers. However, I do reckon part of the exercise here is pointer arithmetics, and so I just leave this as a side-note.
It would be useful for us if you'd attach the class declarations. I assumes all those accessors and mutators, GetFirst() and SetFirst() etc., are there because they are public. That's a bad idea. First, they expose the private pointers, which defeats the whole point of accessor. Second, they don't do anything special so they're just extra code -- which means extra room for bugs. This brings me to the next point.
Your mutators are incorrect. You blindly assign a new value to the private member pointer, without deleting what you had before. That's a memory leak.
Ever tried to pop_front() when the list is empty?
Finally, 8 being a round number it's time we get to the question at hand. pop_back(). My question to you is, why are you traversing the list all the way to the end if you so meticulously maintain a pointer to the last node of your list? Indeed, if you wouldn't bother with maintaining a pointer to the end of the list then you'd have to traverse all the way to the last node in order to pop it. And for that you were in the right direction. Except that ...
When you access members through pointers, as in first->GetNext(), always make sure first isn't a null pointer -- or else state in the function's documentation comment that you assume the pointer is not null.
These should get you started.
Points 1, 2 and 3 in code:
Node::Node()
: descrip(" "), quantity(" "), previous(0), next(0)
{
}
So if I understand this right you just want to run through your linked list until you get to the last node in the linked list and return the pointer to it?
I'm pretty sure what you have there will do it except
Node* List::pop_back() // this whole function may be wrong, this is just my attempt at it
{
Node* temp;
temp = first;
while(temp->GetNext() != NULL)
{
temp = temp->GetNext();
}
return temp;
}
So if I read it right, there it will continually loop around until it gets to the node with none in the line behind it, then return it.
I like the previous posters answer, but one thing you might want to keep in mind is if you have an empty list. Then your first pointer will equal NULL and you would be trying to call NULL->GetNext() basically and Seg Fault. I think you can edit the above code slightly and still get have it work like this:
Node* List::pop_back()
{
Node* temp;
temp = first;
while(temp != NULL && temp->GetNext() != NULL)
{
temp = temp->GetNext();
}
return temp;
}
This will have the function return NULL if there is nothing in the list and still work properly.
It would definitely have helped me if you also had posted your class declaration. I cannot guarantee that the below is correct but it makes sense to me
Node* List::pop_back()
{
Node *temp = NULL;
if(numNodes == 1)
{
temp = first;
// setting the list pointers to NULL
first = NULL;
// setting the list pointers to NULL
last = NULL;
//You should also probably remove the links from your node
//to the next and previous nodes but since you didn't specify
//this it is up to you
numNodes--;
}
else if(numNodes > 1) //more than one element
{
//the pointer you want to return
temp = last;
//For clarity I am creating another variable here
Node *newLast = temp->GetPrevious();
//Setting the new last node to point at nothing so now temp
//is "disconnected from the list"
newLast->next = NULL;
//the last pointer of the list is now pointing at the new last node
last = newLast;
//You should also probably remove the links from your node
//to the next and previous nodes but since you didn't specify this it is up to you
numNodes--; //decrement the counter
}
return temp;
}