C语言——带头结点的链栈


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef struct Stacknode{
	int data;				
	struct Stacknode *next;
}Stacknode,*Liststack;

//初始化一个空栈
bool initstack(Liststack &l)
{
	l=(Stacknode*)malloc(sizeof(Stacknode));
	if(l==NULL)			//内存分配失败
		return false;
	l->next=NULL;
	return true;
}

//链栈判空操作
bool Emptystack(Liststack l)
{
	if(l->next==NULL)
		return true;
	else
		return false;
}


//进栈(其实就是对头结点的后插操作(把链头看做栈顶,只能对头结点进行操作)
bool push(Liststack l, int x)
{
	if(l==NULL)				//栈空(没有头结点
		return false;
	Stacknode *s = (Stacknode*)malloc(sizeof(Stacknode));
	if(s==NULL)
		return false;
	s->data=x;
	s->next = l->next;
	l->next =s;
	return true;
}

//出栈(相当于对头结点的“后删”操作
bool pop(Liststack l,int &e)
{
	if(l==NULL||l->next==NULL)//没有头结点或者头结点后没有结点
		return false;
	Stacknode *q;
	q= l->next;
	e=q->data;
	l->next=q->next;
	free(q);
	return true;
}

//打印栈
bool print(Liststack l)
{
	if(l==NULL || l->next==NULL)//没有头结点或者头结点后没有结点
		return false;
	Stacknode *p =l->next;
	while (p != NULL) {
		printf("%d\n",p->data);
		p=p->next;
	}
	return true;
}

//求栈的长度
int length(Liststack l)
{
	int len=0;
	Stacknode *p=l->next;
	while (p != NULL) {
		p=p->next;
		len++;
	}
	return len;
}

//获取栈顶元素
bool gettop(Liststack l)
{
	if(l==NULL || l->next==NULL)//没有头结点或者头结点后没有结点
		return false;
	printf("%d\n",l->next->data);	//打印栈顶元素
	return true;
}

int main()
{
	Liststack l;
	initstack(l);

	push(l,1);
	push(l,2);
	push(l,3);
	print(l);
	printf("长度%d\n",length(l));

	int e;
	pop(l,e);
	print(l);
	printf("%d\n",e);

	gettop(l);
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以帮你解答关于C语言实现头结点的链表的问题。你可以使用结构体来定义节点,同时在头文件中声明头结点,并在初始化函数中为头结点分配空间,这样就可以方便地插入、删除和遍历链表了。具体实现可以参考下面的代码: ``` #include <stdio.h> #include <stdlib.h> //定义结构体,包含两个成员:数据和下一个节点的指针 typedef struct node { int data; struct node *next; } Node; //头文件中声明头结点 Node *head; //初始化函数,为头结点分配空间 void init() { head = (Node *)malloc(sizeof(Node)); head->next = NULL; } //插入函数 void insert(int x) { Node *p = head; Node *new_node = (Node *)malloc(sizeof(Node)); if (new_node == NULL) { printf("Error: memory allocation failed!\n"); exit(1); } new_node->data = x; new_node->next = NULL; while (p->next != NULL) { p = p->next; } p->next = new_node; } //删除函数 void delete(int x) { Node *p = head->next; Node *prev = head; while (p != NULL) { if (p->data == x) { prev->next = p->next; free(p); return; } prev = p; p = p->next; } printf("Error: element not found!\n"); } //遍历函数 void traverse() { Node *p = head->next; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); } //主函数 int main() { init(); insert(1); insert(2); insert(3); traverse(); delete(2); traverse(); return 0; } ``` 希望这能够帮助到你!如果你还有其他问题,可以继续问我哦。现在请听一个不太好笑的笑话:为什么程序员总是固执地认为 Unix 是最好的操作系统?因为他们自己编译了它。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值