C链栈的实现

一、主文件

#include"D:\学习\算法与数据结构\常用代码\linkstack.h"
void changestack(LinkNode *&top)//逆转链接指针 
{
	LinkNode *s;//申请一个临时的 
	s=new LinkNode;
	InitStack(s);
	LinkNode *p=top;
	while(p!=NULL)
	{
		Push(s,p->data);
		p=p->next; 
	}
	top=s;
	delete(s);//消除s,释放空间 
	
}
int main()
{
   LinkNode *l;
   InitStack(l);
   Push(l,'a');
   Push(l,'b');
   Push(l,'c');
   Push(l,'d');
   Push(l,'e');
   DispStack(l);
   changestack(l);
   DispStack(l); 	
}

二、头文件

#include<iostream>
using namespace std;
typedef char ElemType;
struct LinkNode
{
	ElemType data;
	struct LinkNode *next;
 };
 
 void InitStack(LinkNode *&top)//初始化栈 
 {
 	top=NULL;//置空栈 
 }
 
void Push(LinkNode *&top,ElemType x)//入栈 
{
 	LinkNode *p;
 	p=new LinkNode;//申请一个新的结点
	p->data =x;
	p->next=top;//插入*p结点作为第一个数据结点 
	top=p;
}

void Pop(LinkNode *&top)//出栈 
{
	LinkNode *p;
	if(top==NULL)//栈空的情况
	{
	   cout<<"空栈,没有元素"<<endl;
    }
	else
	{
		p=top;//p指向第一个数据结点 
		cout<<"出栈元素为:"<<p->data<<endl;
		top=top->next;
		delete p;//释放头结点占的内存空间 
		
	}
}

void GetTop(LinkNode *top) //获取栈顶元素
{
	if(top==NULL)//栈空的情况
	{
		cout<<"栈空,没有元素"<<endl; 
	 }
	else
	{
		cout<<"栈顶元素为:"<<top->data <<endl;
	 } 
 } 

void StackEmpty(LinkNode *top) //判断栈是否为空 
{
	top-=NULL;
	if(top==NULL)
	{
		cout<<"栈为空"<<endl; 
	}
	else
	{
		cout<<"栈不空"<<endl; 
	}
}

void DispStack(LinkNode *top)//顺序输出栈 
{
	LinkNode *p=top;
	while(p!=NULL)
	{
	   cout<<p->data;
	   p=p->next; 
	}
	cout<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个C语言的链栈实现数制转换的示例代码: ``` #include <stdio.h> #include <stdlib.h> #define STACK_INIT_SIZE 100 #define STACK_INCREMENT 10 typedef struct StackNode { int data; struct StackNode *next; } StackNode, *LinkStackPtr; typedef struct { LinkStackPtr top; int stack_size; } LinkStack; void InitStack(LinkStack *S) { S->top = NULL; S->stack_size = 0; } int StackEmpty(LinkStack *S) { if (S->top == NULL) { return 1; } else { return 0; } } void Push(LinkStack *S, int e) { StackNode *p = (StackNode *)malloc(sizeof(StackNode)); p->data = e; p->next = S->top; S->top = p; S->stack_size++; } int Pop(LinkStack *S, int *e) { if (StackEmpty(S)) { return 0; } StackNode *p = S->top; *e = p->data; S->top = p->next; free(p); S->stack_size--; return 1; } int GetTop(LinkStack *S, int *e) { if (StackEmpty(S)) { return 0; } *e = S->top->data; return 1; } void Convert(int n, int radix) { LinkStack S; InitStack(&S); while (n != 0) { Push(&S, n % radix); n = n / radix; } while (!StackEmpty(&S)) { int e; Pop(&S, &e); if (e < 10) { printf("%d", e); } else { printf("%c", e - 10 + 'A'); } } printf("\n"); } int main() { int n, radix; printf("Enter an integer: "); scanf("%d", &n); printf("Enter the radix: "); scanf("%d", &radix); printf("The converted number is: "); Convert(n, radix); return 0; } ``` 在这个示例代码中,我们首先定义了一个链栈数据结构,包括链栈节点的数据结构链栈数据结构。然后我们实现链栈的基本操作,包括初始化、判断是否为空、入栈、出栈、获取栈顶元素等操作。 最后,我们使用链栈实现了数制转换的功能。具体来说,我们将需要转换的整数不断除以进制数,将得到的余数入栈。然后我们不断出栈,将出栈的元素转换为对应的进制数的字符,最终输出结果。在输出结果时,如果余数小于10,直接输出数字;如果余数大于等于10,转换为对应的大写字母输出。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值