Suraj-Ajjampur / DSA

0 stars 0 forks source link

Stacks using LL #5

Open Suraj-Ajjampur opened 5 months ago

Suraj-Ajjampur commented 5 months ago

Structure

struct Node{
int data;
struct Node *next;
}

Empty Condition

If Top points to NULL

Full Condition

If heap is full i.e if malloc fails and assigned pointer is NULL

Push Inserting an element in the stack

void push(int x)
{
Node *t =(struct Node*)malloc(sizeof(struct Node));
if(t == NULL)
     printf("Stack Overflow");
else
{
t->data=x;
t->next=top;
top=t;
}
}

Pop

int pop()
{
struct Node *t;
  int x=-1;
  if(top==NULL)
    printf("Stack is Empty\n");
  else
  {
    t=top;
    top=top->next;
    x=t->data;
    free(t);
  }
  return x;
}
Suraj-Ajjampur commented 5 months ago

Initial code in commit https://github.com/Suraj-Ajjampur/DSA/commit/6c471508c62f5f0a0b9651202f05ef75eb407e79