-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrial.c
177 lines (155 loc) · 4.51 KB
/
trial.c
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct player{
// int playId;
char name[20];
// float height;
};
// Function Prototypes
struct stackNode {
struct player p;
struct stackNode *nextPtr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
void push(StackNodePtr *topPtr,struct player info);
struct player pop(StackNodePtr *topPtr);
int isEmpty(StackNodePtr topPtr);
void printStack(StackNodePtr currentPtr);
void instructions(void);
//int isDuplicate(StackNodePtr topPtr,int value);
int main(void)
{
StackNodePtr stackPtr = NULL;
int choice;
// int ide;
char nam[20];
// float height;
instructions(); // user menu
printf("\nPlease Enter you Choice Command: ");
scanf("%d", &choice); // allow user to enter choice
while(choice != 5) {
switch(choice) {
case 1:
//option to View value at top of stack
/*if(!isEmpty(stackPtr))
{
struct player v_top = stackPtr->p;
printf("\nData of the Player Recently Added:");
printf("\n\nPlayer ID: %d\nPlayer Name: %s\nPlayer Height: %f",v_top.playId,v_top.name,v_top.height);
}
else{
printf("------------------------\nNo DATA to Show\n------------------------");
}
break;*/
case 2:
//option to push value onto stack
/*printf("\nEnter a ID: ");
scanf("%d",&ide);
if(isDuplicate(stackPtr,ide))
{*/
printf("Enter a name: ");
scanf("%s",nam);
/*printf("Enter a height: ");
scanf("%f",&height);*/
struct player pl;
//pl.playId=ide;
strcpy(pl.name,nam);
//pl.height=height;
push(&stackPtr,pl);
//}
//printStack(stackPtr);
break;
case 3:
// option to pop value from stack
// ensure to check that stack is non-empty
if(!isEmpty(stackPtr))
{
struct player r_play=pop(&stackPtr);
printf("\nDetails of Player deleted:");
printf("Player Name: %s\n",r_play.name);
}
else
{
printf("\nNo DATA to be Deleted\n");
}
break;
case 4:
//option to print all the values in the stack
printStack(stackPtr);
break;
default:
printf("Invalid choice.\n\n");
instructions();
break;
}
printf("\n\nPlease Enter you Choice Command: ");
scanf("%d",&choice);
}
printf("\nExit Successful!! Good Bye!!\n");
exit(0);
}
void instructions(void) {
printf(
"Welcome to Player Regestration!! \nEnter choice:\n 1). View the Recent Player Added\n 2). Add a New Player\n 3). Delete the Player on TOP \n 4). Print all the data \n 5). Exit the Stack");
}
void push(StackNodePtr *topPtr, struct player info) {
StackNodePtr newPtr;
newPtr = malloc(sizeof(StackNode));
if(newPtr != NULL) {
newPtr->p = info;
newPtr->nextPtr = *topPtr;
*topPtr = newPtr;
printf("\nPlayer Data Inserted Successfully!!\n");
} else {
printf("not inserted. No memory available.\n");
}
}
struct player pop(StackNodePtr *topPtr) {
StackNodePtr tempPtr;
struct player popValue;
tempPtr = *topPtr;
popValue = (*topPtr)->p;
*topPtr = (*topPtr)->nextPtr;
free(tempPtr);
return popValue;
}
void printStack(StackNodePtr currentPtr) {
StackNodePtr currnPtr = currentPtr;
if(currnPtr == NULL) {
printf("There is no Data.\n\n");
} else {
printf("Info of all Players\n-----------------------------------------\n");
while(currnPtr != NULL)
{
struct player d_play =currnPtr->p;
printf("Player Name: %s\n",d_play.name);
currnPtr = currnPtr->nextPtr;
}
printf("-----------------------------------------");
}
}
int isEmpty(StackNodePtr topPtr) {
return topPtr == NULL;
}
/*int isDuplicate(StackNodePtr topPtr,int value){
StackNodePtr checkPtr = topPtr;
int retVal=1;
if(isEmpty(checkPtr))
{
retVal=1;
}else
{
while(checkPtr!=NULL){
if(checkPtr->p.playId==value)
{
retVal=0;
printf("\n------------------------------\n!!Player ID Already exist!!\n------------------------------");
}
checkPtr = checkPtr->nextPtr;
}
}
return retVal;
}
*/