0

I'm trying to make the linked list reverse function by using stack:

1)insert elements into the linear linked list.

2)transfer first~end elements into the stack. //Reverse function included both push and pop. //While pushing linked list elements into the stack, the order of linked list becomes reversed.

3)put elements back to the linear linked list from the stack.

I made 3 typedef struct:

typedef struct Node{
    int data;
    struct Node* next;
}Node;
//this is for linear linked list.

typedef struct LinkedList{
    int curCount;
    Node head;
}LinkedList;
//this is for global pointer for linear linked list node.

typedef struct StackNode{
    int data;
    struct StackNode* next;
}StackNode;
//this is for stack node. 

I assigned all the elements into the LinkedList by moving the global pointer head; so all the elements are saved in the LinkedList data nodes. In the main, I made StackNode* top for pointing to the topmost node in the stack.

My question is: Reverse function takes (LinkedList* pList, StackNode** top) as parameter. When I'm trying to allocate the memory for StackNode;

void reverseList(LinkedList* pList, StackNode** top){

Node *pNode = NULL;
pNode = &(pList->head); 

StackNode* sNode = NULL; //new stack list

sNode = (StackNode*)malloc(sizeof(StackNode));
**sNode->data = (???);**
sNode->next = NULL;

}

I'm not sure what to type in (???).

Usually push function takes (Node** top, int data), I am not sure how to assign elements in the linear linked list into the stack. I searched about push function, but it usually takes int or char data type so I'm not sure how I should handle with linked list node as a parameter.

+++++++++++++++++++++++additional question

I would like to know how to extract data from * LinkedList* pList node and assign them into StackNode sNode.

void push(int data, StackNode** top){
    StackNode *sNode = NULL;
    sNode = (StackNode*)malloc(sizeof(StackNode);
    sNode->data = data;
    sNode->next = NULL;

    if(*top == NULL){
    *top = sNode;
    }else{
        sNode->next = *top;
        *top = sNode;
}

Above code is the example that I woudld like to practice. Instead of passing int data in parameter, I'm trying to pass pList head global pointer that pointing the data node.

---below code is what I'm confused with---

sNode = (StackNode*)malloc(sizeof(StackNode);
    sNode->data = data;
    sNode->next = NULL;

Let's say the data node next to the head node in linked list is:

pNode = pList->head.next;

I tried to assign the linked list data into stack node like this:

sNode = (StackNode*)malloc(sizeof(StackNode);
    sNode->data = pNode->data;
    sNode->next = NULL;

but this way saves only the first data node.

Is there any way to connect or copy linked list node to stack node?

3
  • 1
    pointers are a type like any other. Write a stack for type foo, then change foo to Node * Commented Apr 9, 2022 at 12:13
  • You don't need to allocate space for data, sNode = (StackNode*)malloc(sizeof(StackNode));already allocates space for the entirety of the struct. Also you should be able to write it like this StackNode* sNode = (StackNode*)malloc(sizeof(StackNode)); Commented Apr 9, 2022 at 12:15
  • @stark Thank you for replying l! I still have a question about this code, so I added a question to the post. Could you check out my updated post? It would be really great to get some extra advice :> Commented Apr 9, 2022 at 18:07

1 Answer 1

1

I think what you are trying to do is to push the nodes of the list onto the stack. To do that, the stack must be a stack of pointers, not ints.

typedef struct StackNode{
    struct Node * data;
    struct StackNode* next;
}StackNode;

To push a node, or list of nodes, onto the stack:

void push(struct Node * data, StackNode** top){
    StackNode *sNode = NULL;
    sNode = (StackNode*)malloc(sizeof(StackNode);
    sNode->data = data;
    sNode->next = NULL;

    if(*top == NULL){
        *top = sNode;
    }else{
        sNode->next = *top;
        *top = sNode;
}

Note that the code can be made slightly simpler, since top must be set to NULL when the stack is empty:

void push(struct Node * data, StackNode** top){
    StackNode *sNode = (StackNode*)malloc(sizeof(StackNode);
    sNode->data = data;
    sNode->next = *top;
    *top = sNode;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ohhhh got it! I modified the node struct as you mentioned. Also I made push/pull function outside of reverse function, called them into reverse function, and it worked! Thank you ☺️

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.