#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
/* data */
int data;
struct Node* next;
} Node;
Node* initStack();
void push(Node* statck, int value);
int pop(Node* statck);
length(Node* list);
void printList(Node* list);
int main(int argc, char const* argv[]) {
Node* statck = initStack();
push(statck, 10);
push(statck, 11);
push(statck, 12);
push(statck, 13);
push(statck, 14);
printList(statck);
int poped = pop(statck);
printList(statck);
printf("pop数据 = %d\n", poped);
int poped2 = pop(statck);
printList(statck);
printf("pop数据 = %d\n", poped2);
int poped3 = pop(statck);
printList(statck);
printf("pop数据 = %d\n", poped3);
return 0;
}
Node* initStack() {
Node* statck = (Node*)malloc(sizeof(Node));
statck->data = 0;
statck->next = NULL;
return statck;
}
void push(Node* statck, int value) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = value;
node->next = statck->next;
statck->next = node;
statck->data++;
}
int pop(Node* statck) {
Node* node = statck->next;
statck->data--;
statck->next = statck->next->next;
return node->data;
}
// 工具方法
int length(Node* list) {
return list->data;
}
void printList(Node* list) {
if (list->data == 0)
{
printf("List is empty");
return;
}
list = list->next;
printf("[ ");
while (list->next)
{
printf("%d,", list->data);
list = list->next;
}
printf("%d", list->data);
printf(" ]\n");
}
自己备份一下