forked from dlalsrn/infinite_calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-string.c
More file actions
71 lines (61 loc) · 1.21 KB
/
Copy pathstack-string.c
File metadata and controls
71 lines (61 loc) · 1.21 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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
typedef struct Node {
char* data;
struct Node* next;
}string_node;
string_node* new_node(char* data)
{
string_node* temp_node = (string_node*)malloc(sizeof(string_node));
temp_node->data = data;
temp_node->next = NULL;
return temp_node;
}
int empty(string_node* head)
{
if(!head){
printf("stack is empty\n");
return 1;
}
return 0;
}
void push(string_node** head, char* data)
{
string_node* temp_node = new_node(data);
temp_node->next = *head;
*head = temp_node;
printf("PUSH: [%s]\n", data);
}
char* pop(string_node** head)
{
if (empty(*head)){
return "empty";
}
string_node* temp = *head;
*head = (*head)->next;
char* element = temp->data;
free(temp);
printf("POP: [%s]\n", element);
return element;
}
char* top(string_node* head)
{
if(empty(head))
return "empty";
return head->data;
}
void display(string_node* head){
if(empty(head)){
return;
}
string_node* cursor = NULL;
cursor = head;
printf("STACK:");
while (cursor != NULL) {
printf(" [%s]", cursor->data);
cursor = cursor->next;
}
printf("\n");
return;
}