Write a program to insert an element in stack(Push operation only).
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int v;
struct Node * next;
} Node;
void Traverse(Node *head) {
while (head) {
printf("%d ", head->v);
head = head->next;
}
}
// insert an element in stack(Push operation only).
Node* Push(Node* head, int v) {
Node *newNode = (Node*)malloc(sizeof(Node));
newNode->v = v;
newNode->next = head;
return newNode;
}
int main()
{
Node * head = NULL;
for (int i = 0; i < 5; i++) head = Push(head, i + 1);
Traverse(head);
return 0;
}
Comments
Leave a comment