Saturday, June 19, 2021
Home coding Write a program to implement stack using linkedlist.

Write a program to implement stack using linkedlist.

Given a Linkedlist ,we have to implement stack using linkedlist.

Stack has two operation:

1-push operation.(push the element)

2-pop operation.(pop the element)

Logic:

STEP 1:implement push operation.
STEP 2:implement pop operation.

Code

void push(Node* root, int x)
{
    Node* temp = new Node(data);
    Node->next = *root;
    *root = temp;
    cout << data << " is pushed to stack"<<endl;
}
 
int pop(Node* root)
{
    if (isEmpty(*root))
    {
        return -1;
    }
    Node* temp = root;
    root = root->next;
    int popped = temp->data;
    free(temp);
 
    return popped;
}

Ouput:

pushed
popped

Time Complexity:O(N)

Space Complexity:O(1)

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments