-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp6stack
More file actions
77 lines (76 loc) · 1.85 KB
/
p6stack
File metadata and controls
77 lines (76 loc) · 1.85 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
73
74
75
76
77
/*Q1- Give a suitable typedef for representing a stack of integers using either an array
(b) Write functions for implementing the following stack operations.
init – which constructs and returns an empty stack,
empty – which returns a value indicating whether a given stack is empty or not,
push – which pushes a given integer on to a given stack,
top – which returns the top element of a given stack,
delete – which deletes the top element of a given stack.
(c) Write an iterative function which takes an unsigned integer and prints its representation to the base 5 using a stack; the function should use the data type defined by you for representing a stack and only the functions of Part (b).
*/
//code-
typedef struct node{
int data;
struct node*next;
}node;
typedef struct{
node*top;
}stack;
stack*init(){
stack*sta=(stack*)malloc(sizeof(stack));
sta->top==NULL;
return sta;
}
int empty(stack*sta){
return sta->top==NULL;
}
void push(stack*sta,int data){
node*newnode=(node*)malloc(sizeof(node));
newnode->data=data;
newnode->next=sta->top;
sta->top=newnode;
}
int top(stack*sta){
if(empty(sta)){
printf("The stack is empty\n");
return -1;
}
return sta->top->data;
}
void del(stack*sta){
if(empty(sta)){
printf("The stack is empty\n");
}
node*temp=sta->top;
sta->top=sta->top->next;
free(temp);
}
void printbase5(unsigned int num){
stack*sta=init();
if(num==0){
printf("0\n");
return;
}
while(num>0){
push(sta,num%5);
num/=5;
}
while(!empty(sta)){
printf("%d",top(sta));
del(sta);
}
printf("\n");
free(sta);
}
int main(){
stack*sta=init();
push(sta,3);
push(sta,4);
push (sta,6);
printf("The top element in stack is:%d\n",top(sta));
del(sta);
printf("The top element after deleting in stack is:%d\n",top(sta));
printf("printing the number in base 5:\n");
printbase5(25);
free(sta);
return 0;
}