在c ++中实现push函数 [英] Implementing a push function in c++

查看:239
本文介绍了在c ++中实现push函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建推送功能时遇到了一些麻烦(在列表的前面添加一个节点)。
我知道只使用一个节点,你可以使用push函数给出的push函数push_front(),但是我能在这里使用吗? :

  struct Node {
string val;
Node * next;
Node * prev;
};

struct Stew {
Node * first;
Node * last;
};

其中Stew结构定义为有两个特殊指针,一个指向第一个元素,一个指向到最后。
Node结构在两个方向都有链接。



我是新的在C ++中使用这些类型的结构,所以任何类型的帮助/提示将大大赞赏。我试过它:

  void push(Stew& q,string val){

* inserted = new Node(); //创建一个新节点插入到前面

q.first - > prev = inserted;
inserted - > next = q.first;
inserted - > val = q.first-> val;


}

问题:我需要初始化头这个?这样:

  Node * head = new Node 
head - > next = NULL;
head - > val = val;感谢您提供任何帮助。

>解决方案

我不知道你在说什么头。



当然数据成员类型 Stew 的第一个最后必须初始化为 nullptr NULL



例如

  Stew q = {}; 

函数可以采用以下方式:

  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;
}

这将是很清楚你如何在这里的功能是一个简单的例子
EDIT:我向示例中添加了函数 reverse_display

  #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-& )
{
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;
}

输出为

  ZYXWVUTSRQPONMLKJIHGF EDCBA 
ABCDEFGHIJKLMNOPQRSTU VWXYZ

忘记写入将删除节点的所有已分配内存的函数。


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.

解决方案

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.

这篇关于在c ++中实现push函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆