dooroomi
dooroomi

Reputation: 15

C: take global pointer as parameter for linked list reverse function

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?

Upvotes: 0

Views: 116

Answers (1)

stark
stark

Reputation: 13189

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;
}

Upvotes: 1

Related Questions