简体   繁体   English

C ++中堆栈的链表实现

[英]Linked List Implementation of a stack in C++

So I guess my question is a bit noobish but we've just started linked lists, stacks and queues and I'm feeling a bit lost in terms of syntax. 所以我想我的问题有点笨拙,但是我们才刚刚开始链接列表,堆栈和队列,并且在语法方面我感到有些迷茫。

I have a working linked list structure with adding to back and front, pushing, popping, deleting etc.. but I get crazy lost when it comes to calling those functions in the stack implementation. 我有一个工作的链表结构,可以前后添加,推入,弹出,删除等。但是当我在堆栈实现中调用这些函数时,我却迷失了方向。 The Linked List class declaration is in a header file with function definitions in a .cpp file. 链接列表类声明位于.cpp文件中的函数定义的头文件中。 The stack is exactly the same story. 堆栈是完全一样的故事。 We have been given the header file with function declarations and we need to write our own definitions in a separate .cpp file. 给我们的头文件带有函数声明,我们需要在单独的.cpp文件中编写我们自己的定义。

My problem lies in calling the linked list functions in the stack functions. 我的问题在于在堆栈函数中调用链表函数。 The stack includes an "LList data" (the linked list structure is called LList); 堆栈包括一个“ LList数据”(链接列表结构称为LList)。 a pointer to an integer (I'm assuming) "int *data" and and integer tracking the top of the stack "int t" 指向整数(我假设)“ int * data”和整数的指针,它们跟踪堆栈的顶部“ int t”

The funcitons we have to write are the constructor, destructor, pop, push and size functions. 我们必须编写的功能是构造函数,析构函数,pop,push和size函数。 I'd give source code but it really isn't anything significant to work with at all. 我会提供源代码,但实际上根本没有任何意义。

I hope my question can be understood >_< Thanks in advance Cameron 希望我的问题可以理解> _ <预先感谢Cameron

A stack is just a linked list with different interface functions. 堆栈只是具有不同接口功能的链表。 You only need to be able to push elements to the "top" of the stack, and pop from the top. 您只需要能够push元素push堆栈的“顶部”,然后从顶部pop

It looks like your Stack class implementation is designed to be a wrapper around the LList class you already have. 看起来您的Stack类实现被设计为已存在的LList类的包装器。

Without going into the details (which is kinda hard without looking at the code), 没有详细介绍(不看代码就很难),

  1. You have a Stack class with a LList as a member variable (preferably private). 您有一个将LList作为成员变量(最好是私有)的Stack类。
  2. Your push function should simply insert to the tail of your linked list. 您的push功能应仅插入到链接列表的末尾。
  3. Your pop function should remove the last element from the tail of your linked list 您的pop功能应从链接列表的末尾删除最后一个元素
  4. Your ~Stack implementation should delete the LList object you created; ~Stack的实现应该删除LList你创建的对象; you can skip this if you use smart pointers, or use the delete keyword if you manually created an object using new . 如果使用智能指针,则可以跳过此步骤;如果使用new手动创建对象,则可以使用delete关键字。

More code will help refine this answer. 更多代码将有助于完善此答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM