简体   繁体   中英

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. 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.

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); a pointer to an integer (I'm assuming) "int *data" and and integer tracking the top of the stack "int t"

The funcitons we have to write are the constructor, destructor, pop, push and size functions. 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

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.

It looks like your Stack class implementation is designed to be a wrapper around the LList class you already have.

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).
  2. Your push function should simply insert to the tail of your linked list.
  3. Your pop function should remove the last element from the tail of your linked list
  4. Your ~Stack implementation should delete the LList object you created; you can skip this if you use smart pointers, or use the delete keyword if you manually created an object using new .

More code will help refine this answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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