I've started to experiment (no previous experience with the language) with c++, I'm using Dev C++ ver. 5.11. I'm trying to implement a stack, I don't want to use the ready Stack library because I want to practice. However with no errors the program halts after printing out the stack items and further statements aren't executed. What is going wrong?
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
struct node{
int data;
struct node *next;
};
struct node *push(struct node *root, int ikey){
struct node *ptr;
ptr=(struct node *)malloc(sizeof(struct node));
ptr->data=ikey;
ptr->next=root;
root = ptr;
return root;
}
struct node *pop(struct node *root){
struct node *ptr;
ptr = root;
root = ptr->next;
return root;
}
void Print_Stack(struct node *root){
struct node *n;
n = root;
do{
printf("Node -> %d \n",n->data);
n = n->next;
}while (n != NULL);
}
int main(){
struct node *stoiva, *tmp;
int x;
tmp = (struct node *)malloc(sizeof(struct node));
do{
printf("Give x :");
scanf("%d",&x);
if (x > 0){
stoiva = push(stoiva, x);
}
}while ( x > 0);
tmp = stoiva;
do{
cout << tmp->data << endl;
tmp = tmp->next;
}while (tmp->next != NULL);
printf ("-----------------\n");
return 0;
}