C语言中链表和栈

创建了一个C语言链表和栈。包含输入数据在链表中,保存在栈里,再从栈里读取出来。

#include <stdio.h>
#include <string.h>

#define MAX 1000

typedef struct node{

int value;
struct node *next;
}Node,*pNode;

typedef struct Stacks{
int top;
int value[MAX]
}stack;
stack s;
void CreatStack(){
s.top = -1;
}
void Push(int values){
s.value[++s.top] = values;
}
int Pop(){
return s.value[s.top--];
}
int main()
{

pNode head = NULL;
head = (pNode)malloc(sizeof(Node));
pNode tail = head;
tail->next = NULL;
int i ;
CreatStack();
for(i = 0 ; i < 5; i++){

pNode nodes = (pNode)malloc(sizeof(Node));
nodes->value = i ;
nodes->next = tail->next;
tail->next = nodes;
}

pNode nodes2 = (pNode)malloc(sizeof(Node));
nodes2 = head->next;
for(i = 0 ; i< 5 ; i++){
printf("%d",nodes2->value);
Push(nodes2->value);
nodes2 = nodes2->next;
printf("\n");
}

printf("\n");
for(i = 0 ; i < 5; i++){
printf("%d",Pop());
}


return 0;
}

posted @ 2017-07-20 14:35  Cloud_strife  阅读(242)  评论(0编辑  收藏  举报