- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_of_tree_nodes_.cpp
More file actions
Latest commit
72 lines (65 loc) · 1.65 KB
/
Copy pathstack_of_tree_nodes_.cpp
File metadata and controls
72 lines (65 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<stdio.h>
#include<malloc.h>
/* nodes for queue and tree node*/
structnode {
int key;
structnode * left;
structnode * right;
};
structsnode {
structnode * point ;
structsnode * next ;
};
structsnode * start = NULL;
/* function for creating new node of tree*/
structnode *newNode(int data)
{
structnode *node = (structnode *)malloc(sizeof(structnode));
node->key = data;
node->left = NULL;
node->right = NULL;
return (node);
}
/* function for pushing,popping,peeking,to check that is stack is empty a tree node in stack*/
voidpush(structsnode ** start, structnode ** treenode){
structsnode *qnodetemp = (structsnode *)malloc(sizeof(structsnode));
qnodetemp->point = *treenode;
qnodetemp->next = (*start);
(*start) = qnodetemp;
printf("%d inserted successfully \n",(*treenode)->key);
}
voidpop(structsnode ** start){
if((*start)->next != NULL)
(*start) = (*start)->next;
else(*start) = NULL;
printf("%s\n"," popped successfully ");
}
structnode * peek(structsnode ** start){
return (*start)->point;
}
intempty(){
if (top == NULL){
return1;
}
else{
return0;
}
}
intmain(){
node * node1 = newNode(1);
node * node2 = newNode(2);
node * node3 = newNode(3);
push(&top, &node1);
printf("%d\n",peek(&top)->key);
push(&top, &node2);
printf("%d\n",peek(&top)->key);
pop(&top);
printf("%d\n",peek(&top)->key);
push(&top, &node3);
printf("%d\n",peek(&top)->key);
printf("%d empties result\n",empty());
pop(&top);
pop(&top);
printf("%d empties result\n",empty());
return0;
}