0

Hi so I have been trying for so long to figure that out. I created a Stack data struct of linked list with this form:

typedef struct Stack
{
    unsigned int data;
    Stack* next;
} Stack;

I have been trying to do a push and pop function so many times so many different YouTube videos but nothing seems to work. I tried to create my push function like this:

void push(Stack* s, unsigned int element)
{
    Stack* newNode;
    newNode = new Stack();
    newNode->data = element;
    newNode->next = s;

    if (s == NULL)
    {
        s = newNode;
    }
    else
    {
        newNode->next = s;
        s = newNode;
    }
}  

but whenever I'm trying to use it im getting s->data = a lot of random numbers like this: 3452816845 Thank you so much of you paying attention I would really be grateful if you can help me fix it. Thanks!

SkyBear
  • 3
  • 5
  • 2
    Assigning to a function's (non-reference) argument has no effect outside that function. There is nothing special about pointers. – molbdnilo Nov 04 '22 at 09:26
  • Remember that arguments are by default pass *by value*. Which means the value in the call is *copied* into the functions local argument variable. The assignments to `s` will only modify your local variable, not the original that was used in the call. You need to pass by *reference*. – Some programmer dude Nov 04 '22 at 09:26
  • On a different note, the macro `NULL` is a C backward compatibility macro. In C++ you should really use `nullptr` for null pointers. – Some programmer dude Nov 04 '22 at 09:27
  • `typedef struct` has no real benefit in C++. you can just type `struct` and have (almost) the same name lookup mechanism – The Dreams Wind Nov 04 '22 at 09:28
  • 2
    By the way: youtube videos are rubbish. Get yourself one of [these](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Nov 04 '22 at 09:28
  • so how do I fix it? – SkyBear Nov 04 '22 at 09:29
  • You open the index of your text-book and look for the term *reference*. Then you go to the chapters and section mentioned in the index, and learn what a reference is, how it might be useful, and how to use them. I'm sorry but there aren't really any shortcut to learning programming and programming languages, it takes time and effort, and not really something a simple video tutorial can do. – Some programmer dude Nov 04 '22 at 09:33
  • The problem with youtube videos is that many of them are done by guys who learned linked lists yesterday, and really don't know anything more than you already do. You just cannot learn C++ by example code, you have to read up on the rules (like in a book, or at [learncpp.com](https://www.learncpp.com/) ) – BoP Nov 04 '22 at 09:45
  • why this site is so untuneful like I just asked for a simple solution – SkyBear Nov 04 '22 at 09:47
  • The term *reference* have been mentioned multiple times. A major part of any studying and learning is the ability to do research for yourself. What have research about *references* have you made? Yes, we could write your code for you, but what will that teach you? – Some programmer dude Nov 04 '22 at 09:49
  • @SkyBear - Welcome to StackOverflow. One way to earn lots of rep here is to write cool answers that get lots of upvotes. However, an answer saying you should change the parameter `Stack*` into `Stack*&` isn't cool enough. So you get some negative vibes from disappointment. – BoP Nov 04 '22 at 09:54
  • @SkyBear you are not getting helpul (to you) responses because you din't read a book on c++ before asking the question. – Neil Butterworth Nov 04 '22 at 10:11
  • *"`newNode = new Stack();`"* -- read this line. Read it again. Does it make sense for a new **node** to be a new **stack**? If not, your naming scheme needs work. *(In fact, having distinct `Node` and `Stack` classes could make your current problem vanish.)* – JaMiT Nov 05 '22 at 09:10
  • *"im getting s->data = a lot of random numbers like this: 3452816845"* -- your code does not demonstrate this. That is, I cannot copy the code from your question, compile it, and see a lot of random numbers (well, showing one random number would be enough). In the absence of a [mre], I find myself agreeing that this is a duplicate. If the other question does not provide an answer for you, please provide enough information in this question to show how your question is different. – JaMiT Nov 05 '22 at 09:14

0 Answers0