本文主要是介绍C语言堆栈之链栈,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在Linux下的GCC这个程序是正确的:
#include<stdio.h>
#include<stdlib.h>typedef struct Node{int data;struct Node *next;}Node, *Stack;/* */
void show(Stack S)
{if(S == NULL){printf("Stack NULL\n");return;}printf("show:");do{printf("%d - ",S->data);S = S->next;}while(S!=NULL);printf("\n");
}void Push(Stack *S, int x)
{if(S == NULL){printf("Stack NULL\n");return;}Node *node = (Stack)malloc(sizeof(Node));node->data = x;node->next = *S;*S = node;
}int Pop(Stack *S)
{if(S == NULL){printf("Stack NULL\n");return 0;}printf(" Pop: ");int a = (*S)->data;*S = (*S)->next;printf("%d",a);printf("\n");return a;
}int main(int argc, char*argv[])
{int a[10] = {1,2,3,4,5,6,7,8,9,0};Stack S = (Stack)malloc(sizeof(Node));S->data = a[0];S->next = NULL;show(S);Push(&S, 2);Push(&S, 3);show(S);Pop(&S);show(S);return 0;
}
在win10 的CMD中这个程序是正确的:
#include<stdio.h>
#include<stdlib.h>typedef struct Node{int data;struct Node *next;}Node, *Stack;/* */
void show(Stack S)
{if(S == NULL){printf("Stack NULL\n");return;}printf("show:");do{printf("%d - ",S->data);S = S->next;}while(S!=NULL);printf("\n");
}void Push(Stack &S, int x)
{if(S == NULL){printf("Stack NULL\n");return;}Node *node = (Stack)malloc(sizeof(Node));node->data = x;node->next = S;S = node;
}int Pop(Stack &S)
{if(S == NULL){printf("Stack NULL\n");return 0;}printf(" Pop: ");int a = S->data;S = S->next;printf("%d",a);printf("\n");return a;
}int main(int argc, char*argv[])
{int a[10] = {1,2,3,4,5,6,7,8,9,0};Stack S = (Stack)malloc(sizeof(Node));S->data = a[0];S->next = NULL;show(S);Push(S, 2);Push(S, 3);show(S);Pop(S);show(S);return 0;
}
这篇关于C语言堆栈之链栈的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!