问题
I'm having some trouble with creating a push function (adding a node to the front of the list). I know that with just a node, you could use the push function given with c++ as push_front(), but would I be able to use that here as well? :
struct Node {
string val;
Node* next;
Node* prev;
};
struct Stew {
Node* first;
Node* last;
};
Where the Stew structure is defined as having two special pointers, one pointing to the first element and one pointing to the last. The Node structure has links in both directions.
I'm new to using these types of structures in C++ so any sort of help/hints would be greatly appreciated. I have attempted it as:
void push (Stew& q, string val) {
Node *inserted = new Node(); // create a new node to be inserted in the front
q.first -> prev = inserted;
inserted -> next = q.first;
inserted -> val = q.first->val;
}
Question: Do I need to initialize a head for this? Such that:
Node *head = new Node();
head -> next = NULL;
head -> val = val;
Thanks for any of the help.
回答1:
There are several issues with your code:
void push (Stew& q, string val) {
Node *inserted = new Node();
// q.first can be NULL, so check before dereferencing it
q.first -> prev = inserted; // Looses any node that prev was
// pointing to before
inserted -> next = q.first;
inserted -> val = q.first->val; // you probably meant:
// inserted -> val = val;
}
回答2:
I do not see where there is the head you are speaking about.
Of course data members first
and last
of an object of type Stew
have to be initialized to nullptr
or NULL
.
For example
Stew q = {};
The function could look the following way
void push( Stew &q, const string &val )
{
Node *tmp = new Node { val, q.first, nullptr };
if ( q.first != nullptr ) q.first->prev = tmp;
q.first = tmp;
if ( q.last == nullptr ) q.last = q.first;
}
That it would be clear for you how the function works here is a simple example
EDIT: I added function reverse_display
to the example.
#include <iostream>
#include <string>
struct Node {
std::string val;
Node* next;
Node* prev;
};
struct Stew {
Node* first;
Node* last;
};
void push( Stew &q, const std::string &val )
{
Node *tmp = new Node { val, q.first, nullptr };
if ( q.first != nullptr ) q.first->prev = tmp;
q.first = tmp;
if ( q.last == nullptr ) q.last = q.first;
}
void display( const Stew &q )
{
for ( Node *tmp = q.first; tmp; tmp = tmp->next )
{
std::cout << tmp->val << ' ';
}
}
void reverse_display( const Stew &q )
{
for ( Node *tmp = q.last; tmp; tmp = tmp->prev )
{
std::cout << tmp->val << ' ';
}
}
int main()
{
Stew q = {};
for ( char c = 'A'; c <= 'Z'; c++ )
{
push( q, std::string( 1, c ) );
}
display( q );
std::cout << std::endl;
reverse_display( q );
std::cout << std::endl;
return 0;
}
The output is
Z Y X W V U T S R Q P O N M L K J I H G F E D C B A
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Do not forget to write function that will delete all allocated memory for nodes.
回答3:
You need to set q.first
to the first node:
void push (Stew& q, string val) {
Node *inserted = new Node(); // create a new node to be inserted in the front
q.first -> prev = inserted;
inserted -> next = q.first;
inserted -> val = q.first->val; //although you maybe meant inserted -> val = val;
q.first=inserted;
}
回答4:
When you create an instance of Stew, you should set the head and tail to NULL.
Stew s;
s.first = NULL;
s.last = NULL;
If you used a class instead of a struct, this would be done in the constructor. The push function can then be
void push (Stew& q, string val) {
Node *inserted = new Node(); // create a new node to be inserted in the front
inserted->val = val;
if (q.first == NULL) { // This list is currently empty
// Make the inserted element the only element
inserted->prev = NULL;
inserted->next = NULL;
q.first = inserted;
q.last = inserted;
}
else { // The list has elements
q.first->prev = inserted;
inserted->next = q.first;
inserted->prev = NULL;
q.first = inserted;
}
}
This code can be simplified a bit (redundant code removed) to
void push (Stew& q, string val) {
Node *inserted = new Node(); // create a new node to be inserted in the front
inserted->val = val;
inserted->prev = NULL;
inserted->next = q.first;
q.first = inserted;
if (q.first == NULL) { // This list is currently empty
// Make the inserted element the only element
q.last = inserted;
}
}
来源:https://stackoverflow.com/questions/22075263/implementing-a-push-function-in-c