1

Can someone help me understand whats happening in the following functions? Specifically the use of s1->top? And what is the movement of s1->top in the functions push &pop & display? Because if in function push, s1->top moves to the right whenever a number is pushed? then why in the display function, it says s1->top is first in the traversing, while in push s1->top is n the right, while in printing the values, we need to firstly be in the left and then traverse..why?

Stack createStack() {
  Stack s1;
  s1 = (Stack) malloc(sizeof(Stack_Head));
  s1 - > count = 0;
  s1 - > top = NULL;
  return s1;
}

Nodeptr createNode(dataitem item) {
  Nodeptr temp;
  temp = (Nodeptr) malloc(sizeof(Node));
  temp - > data = item;
  temp - > next = NULL;
  return temp;
}

void push(Stack s1, dataitem item) {
  Nodeptr temp = createNode(item);
  temp - > next = s1 - > top;
  s1 - > top = temp;
  s1 - > count++;
}

void display(Stack s1) {
  Nodeptr ptr = s1 - > top;
  while (ptr1 = NULL) {
    printf("%d", ptr - > data);
    ptr = ptr - > next;
  }
  printf("\n");
}

void pop(Stack s1) {
    Nodeptr temp;
    if (isEmpty(s1))
      printf("List is Empty");
    else {
      temp = s1 - > top;
      s1 - > top = temp - > next;
      temp - > next = NULL;
      free(temp);
      s1 - > count;
    }

    int isEmpty(Stack s1) {
      return s1 - > top == NULL;
    }  
  • 1
    You want to have a look at the definition of the type `Stack`. – alk Apr 04 '17 at 11:53
  • 1
    I did not read the code only because of the horrible indentation. – Sourav Ghosh Apr 04 '17 at 11:55
  • Do you mean the `->` operator? You don't know what is it? Maybe you should read [What is the difference between the dot (.) operator and -> in C++?](http://stackoverflow.com/questions/1238613/what-is-the-difference-between-the-dot-operator-and-in-c) (which probably should be voted a duplicate of this if that is the main question). – Some programmer dude Apr 04 '17 at 11:56
  • in the function push , why do it has to have temp->next which is s1->top rather than directly saying s1->top = temp? am i right that whenever you push a number, you'll also move s1->top? – Patrick Esguerra Apr 04 '17 at 11:59
  • Maybe your problem is understanding basic single-linked lists? Draw it out on paper, and on paper try to add a node to the head of the list and also remove the head from the list. – Some programmer dude Apr 04 '17 at 12:03
  • s1->top should be at the new added number right? but when in the display function, why is it that ptr is pointed to s1->top ? while you need to traverse it to display from the first number – Patrick Esguerra Apr 04 '17 at 12:03
  • right @Someprogrammerdude, i need some time – Patrick Esguerra Apr 04 '17 at 12:04
  • s1->top should be at the new added number right? but when in the display function, why is it that ptr is pointed to s1->top ? while you need to traverse it to display from the first number – Patrick Esguerra Apr 04 '17 at 12:12
  • i also wonder what is the function of the s1->top – Patrick Esguerra Apr 04 '17 at 12:16
  • regarding this kind of line: `s1 = (Stack) malloc(sizeof(Stack_Head));`. 1) In C, the returned type from any of the heap allocation functions (malloc, calloc, realloc), the type is `void *` which can be assigned to any other pointer. Casting just clutters the code, making it more difficult to understand, debug, etc. 2) Always check (!=NULL) the returned value to assure the operation was successful. – user3629249 Apr 05 '17 at 14:18
  • I wonder whats the movement of s1->top in the push function..please help – Patrick Esguerra Apr 06 '17 at 10:58

2 Answers2

1

this is an implementation of a stack data structure.

Think of a lot of books that have been placed one on top of the other.
That would be a 'stack' of books.

Continuing with the stack of books analogy:
We start with an study table with nothing on it. We can call this state as "There is no stack" Then we write "Book Stack" on a blank paper and place it on the table. We can call this state as "The stack is empty" Lets 'Put' a Mathematics book on the stack. Now the stack has one item, the Mathematics book, which is on top of the stack. We 'Put' another book, Geography on the stack. Now, we have two books on the stack and the Geogrphy book is the one on top. Next, we 'Remove' the Geography book from the stack. Again the Mathematics book will be on the top.

Ok. The table is your computer. The paper with Book Stack written on it is a stack. Books are elements that can be placed on stack. 'Put' is called 'Push' 'Remove' is called 'Pop'

Adwait Patil
  • 171
  • 1
  • 10
1

This stack structure is a LIFO, s1->top is the top of the stack, i.e. the last pushed element. Each element points to the next element in the stack.

For example, here, the push function creates a new element pointing to the data, makes this new element point to the last inserted element (which is its next element in the stack), and puts the new element at the top of the stack (s1->top = new_node, where the new node here is called temp).

Cakeisalie5
  • 384
  • 3
  • 10
  • if s1->top is the last pushed element.. then why is the function display starting printing in the s1->top, if s1->top is the last pushed element? well if we display, we need to printf from the first inserted element – Patrick Esguerra Apr 06 '17 at 11:21
  • if s1->top is the newest inserted element, then why in display, s1->top is the first in traversal? its weird. while the correct printing is you need to start with the first inserted..not the last inserted, which you called s1->top – Patrick Esguerra Apr 06 '17 at 11:24
  • Sorry, I saw the notification, but forgot to answer. Well, the code indeed starts displaying the last inserted element. In order to display from the first inserted element to the next one, either you use recursivity and display the current element after the recursive call, or you make a double-linked list -- if you do that, you can keep a `stack->start` in your `Stack` pointer, so you don't have to look for the first element at each display. – Cakeisalie5 Apr 10 '17 at 11:05