C语言链栈

#include<stdio.h>
#include<stdlib.h>

typedef int DataType;
typedef struct node
{
	DataType  data;
	struct node *next;
} Node;

void InitStack(Node *top)
{
	top = (Node *)malloc(sizeof(Node));
	top->next = NULL;
}

int Push(Node *top, DataType x)
{
	Node * p;
	p = (Node *)malloc(sizeof(Node));
	p->data = x;
	p->next = top->next;
	top->next = p;
	return 1;
}

int Pop(Node *top, DataType *x)
{
	Node *p;
	p = top->next;
	if (p == NULL) return 0;
	top->next = p->next;
	*x = p->data;
	free(p);
	return 1;
}

int GetTop(Node *top, DataType *p)
{
	if(top->next == NULL) return 0;
	*p = top->next->data;
	return 1; 
}

int main() {
	Node top;
	InitStack(&top);
	DataType x;
	printf("How many data to push?\n");
	scanf("%d",&x);
	int i = x;
	while(i--){scanf("%d",&x);Push(&top, x);}
	if (GetTop(&top,&x) == 0) printf("Failed to get top data\n");
	else printf("Top data: %d\n",x);
	if(Pop(&top, &x) == 0) printf("Failed to pop\n");
	else printf("Pop data: %d\n", x);
	if (GetTop(&top,&x) == 0) printf("Failed to get top data\n");
	else printf("Top data: %d\n",x);
	return 0;
}

运行结果:

How many data to push?
5
1 2 3 4 5
Top data: 5
Pop data: 5
Top data: 4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是C语言链栈的基本操作代码展示: ```c #include <stdio.h> #include <stdlib.h> // 定义链栈结构体 struct node { int data; struct node *next; }; // 初始化链栈 void initStack(struct node **top) { *top = NULL; } // 判断链栈是否为空 int isEmpty(struct node *top) { return top == NULL; } // 入栈操作 void push(struct node **top, int data) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); if (newNode == NULL) { printf("内存分配失败"); exit(1); } newNode->data = data; newNode->next = *top; *top = newNode; } // 出栈操作 int pop(struct node **top) { if (isEmpty(*top)) { printf("链栈为空"); exit(1); } int data = (*top)->data; struct node *temp = *top; *top = (*top)->next; free(temp); return data; } // 获取栈顶元素 int peek(struct node *top) { if (isEmpty(top)) { printf("链栈为空"); exit(1); } return top->data; } // 输出链栈 void display(struct node *top) { if (isEmpty(top)) { printf("链栈为空"); return; } struct node *temp = top; while (temp != NULL) { printf("%d->", temp->data); temp = temp->next; } printf("NULL"); } int main() { struct node *top; initStack(&top); push(&top, 1); push(&top, 2); push(&top, 3); printf("链栈元素为:"); display(top); printf("\n"); printf("栈顶元素为:%d\n", peek(top)); printf("出栈元素为:%d\n", pop(&top)); printf("链栈元素为:"); display(top); printf("\n"); return 0; } ``` 注:上述代码中,`struct node` 表示链栈的节点,包含 `data` 和 `next` 两个成员变量。`top` 表示链栈的栈顶节点。`initStack` 函数用于初始化链栈,将栈顶节点设置为 `NULL`。`isEmpty` 函数用于判断链栈是否为空。`push` 函数用于入栈操作,将新的节点插入链表头部。`pop` 函数用于出栈操作,从链表头部取出节点并删除。`peek` 函数用于获取栈顶元素。`display` 函数用于输出链栈。`main` 函数中展示了链栈的基本操作,包括初始化、入栈、获取栈顶元素、出栈和输出链栈等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值