艾泽
V1
2021/05/22阅读:62主题:默认主题
链式栈
#include<stdio.h>
typedef struct list_node list;
struct list_node{
int data;
list* next;
};
typedef struct stack_header stack;
struct stack_header{
list* top;
};
typedef stack* stack_t;
stack* stack_new(){
stack* S = (stack*)malloc(sizeof(stack));
list* dummy = (list*)malloc(sizeof(list));
S -> top = NULL;
return S;
}
bool stack_empty(stack* S){
return S != NULL;
}
bool is_acyclic(list* start){
if(start == NULL) return true;
list* h = start -> next;
list* t = start;
while(h != t){
if(h == NULL || h -> next == NULL) return true;
h = h -> next -> next;
t = t -> next;
}
return false;
}
bool is_segment(list* start, list* end){
for(list* p = start ; p != NULL ; p = p -> next){
if(p == end) return true;
}
return false;
}
bool is_stack(stack* S){
return S != NULL
&& is_acyclic(S -> top);
}
int pop(stack* S){
int x = S -> top -> data;
S -> top = S -> top -> next;
return x;
}
void push(stack* S, int x){
list *p = (list*)malloc(list);
p -> data = x;
p -> next = S -> top;
S -> top = p;
}
int main(int argc, char* argv[]){
return 0;
}
作者介绍
艾泽
V1