Help with implementing linked list

So recently, I started to learn linked list in class and I am confused. She did a whole explanation and some examples, but it doesn't make sense. So for example I have a header file for the class Node and class AnyList.
I have a few functions that I need to implement, but it's a bit difficult for me. One of my functions is suppose to delete the last node of the list.

So this is the Header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef ANYLIST_H
#define ANYLIST_H

#include<iostream>
#include <string>				
using namespace std;

class Node
{
public:
    Node() { next = nullptr; }
    Node(const string& theData, Node *nextNode) : data(theData), next(nextNode){}
    Node* getNext( ) const { return next; }
    string getData( ) const { return data; }
    void setData(const string& theData) { data = theData; }
    void setNext(Node *nextNode) { next = nextNode; }
	~Node(){}
private:
    string data;		
    Node *next;		//pointer that points to next node
};


class AnyList
{
public:
	AnyList();	

	void insertBack(const string& newData);

	void print() const;

	void destroyList();

	~AnyList();

	/*************************************************************************/

	// Declaration of function getFirstElement
	Node getFirstElement() const;

	// Declaration of function deleteFirstNode
	string deleteFirstNode(const AnyList& list);

	// Declaration of function deleteLastNode
	string deleteLastNode(const AnyList& list) const;

	// Declaration of function replaceData
	string replaceData(const AnyList& list) const;


private:
	Node *first;			//pointer to point to the first node in the list
	int numOfItems;		//keeps track of number of nodes in the list
};

#endif 



And this is the implement file (.cpp):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
	Randy Nguyen, Febuary 19 2017, Lab 4: More SLL
*/

#include "AnyList.h"
#include <string>


// Definition of function getFirstElement
Node AnyList::getFirstElement() const
{
	return *first;
}

// Definition of function deleteFirstNode
string AnyList::deleteFirstNode(const AnyList& list)
{

}

// Definition of function deleteLastNode
string AnyList::deleteLastNode(const AnyList& list) const
{

}

// Definition of function replaceData
string AnyList::replaceData(const AnyList& list) const
{

}
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/209043/
http://www.cplusplus.com/forum/general/209044/
Help please?
Last edited on
closed account (48T7M4Gy)
Your first step is to choose one of your triplicate posts and closeoff/greentick the others so that if anyone is decides to provide you with help their comments/help aren't wasted by duplicating effort. You only have to ask once, not 3 times.
Typically to delete the last one you have to handle the odd cases... totally empty (nothing to do), only 1 (delete it). If those are not true, you can muddle thru the list in a loop holding current and current's next (c, and cn). when cn-> next is null, you delete cn which is done via c: delete c->next; c-> next = null ...

So would it be something like this for deleteLastNode?

1
2
3
4
5
6
	Node* current = first;
	Node* cn = current;
	while (cn == nullptr)
		cn = current->getNext();
	delete cn;
	cn = nullptr;
You seem to have the code skeleton but otherwise I can't see much of your efforts.

So would it be something like this for deleteLastNode?

The way you delete the last node is wrong. It could be something like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Node* cn = first;
while (cn && cn->getNext() && cn->getNext()->getNext()) cn = cn->getNext();

if(cn == nullptr)
{
   // Do something
}
else if(cn->getNext())
{
    // Delete the pointer we are checking
    // Make cn point to null
}
else
{
    // Delete the head pointer
}
Topic archived. No new replies allowed.