数据结构: 线性存储 、 链表结构 实现栈功能 --------------进制转换

版权声明:禁止copy抄袭,博主很凶的哦,超级凶的那种哦。 https://blog.csdn.net/Strawberry_595/article/details/83181336

1. 写一个程序,将输入的十进制数据n 转换为八进制数据m,将其调试通过.

(1)采用顺序存储结构实现栈.

(2)采用链表结构实现栈. 

(1)采用顺序存储结构实现栈.

//////////////////////////////////线性存储:
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
 
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
using namespace std;
int m, n;
typedef struct
{
    int *base;//在构造之前和销毁之后,base的值为NULL
    int *top;//栈顶指针
    int stacksize;//当前分配的存储空间,以元素为单位
}SqStack;
 
//构造一个空栈
int InitStack(SqStack &S)
{
    S.base = (int*)malloc(STACK_INIT_SIZE*sizeof(int));
    if(!S.base)exit(-1);
    S.top = S.base;
    S.stacksize = STACK_INIT_SIZE;
    return 1;
}
 
//输出栈顶元素
int GetTop(SqStack &S, int &e)
{
    if(S.top == S.base) return 0;
    e = *(S.top-1);
    return 1;
}
 
//元素入栈
int Push(SqStack &S, int e)
{
    if(S.top - S.base >= S.stacksize)
    {
        S.base =(int*)realloc(S.base, (S.stacksize + STACK_INCREMENT)*sizeof(int));
        if(!S.base) exit(-1);
        S.top = S.base+S.stacksize;
        S.stacksize = S.stacksize + STACK_INCREMENT;
    }
    *S.top++ = e;
    return 1;
}
 
//元素出栈
int Pop(SqStack &S, int &e)
{
    if(S.top == S.base) return 0;
    e = *--S.top;
    return 1;
}
 

int main()
{
    SqStack s;
    int e;
    if(InitStack(s)) printf("Init succeed!\n");
	cin >> n >> m;
	int yu = 0;
	int k = 0;
	while(n >= m)
	{
		yu = n % m;
		n = n/m;
		Push(s, yu);
	}
	if(n != 0) Push(s, n);
	while(s.top != s.base){
    if(GetTop(s,e))
	{
		printf("%d",e);
		Pop(s, e);
	}
	}
   return 0;
}

(2)采用链表结构实现栈. 

////////////////////////////////////////////////链表结构

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
 
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef struct Node {
	int data;//结点数据域
	struct Node* next;//结点指针域
}Node,* LinkPoi;
//链栈的数据结构
typedef struct Link {
	LinkPoi top;//栈顶结点
	int count;//元素个数
}Link;
//初始化
int InitLink(Link* stack)
{
	if (!stack)	return	0;
	stack->top = NULL;
	stack->count = 0;
	return 1;
}
//判断链栈是否为空
int EmptyLink(Link* stack) {
	if (!stack) return 0;
	return stack->count == 0 ? 1 : 0;
}
//获取元素个数
int GetLengthLink(Link* stack)
{
	if (!stack ) return	-1;
	return stack->count;
}
int GetTop(Link* stack, Node** stackNode)
{
	if (!stack)	return	0;
	*stackNode = stack->top;//将栈顶元素的指针返回,获取指向可修改栈顶元素内容。
	return 1;
}

/*
压栈
先将压入元素放入到链表表中,然后再将栈顶指针指向压入的元素,然后count+1.
*/
int push(Link* stack,int e)
{
	if (!stack)	return 0;
	Node* node = (Node*)malloc(sizeof(Node));
	node->next = stack->top;//将元素加入链表中
	node->data = e;
	stack->top = node;//栈顶元素指向压入元素
	stack->count++;
	return 1;
}
//元素出栈
int pop(Link* stack,int *e)
{
	if (!stack && stack->count)	return	0;
	Node* node = stack->top;
	*e = node->data;
	stack->top = node->next;//栈顶指针指向新的栈顶元素
	free(node);//释放元素空间
	stack->count--;
	return 1;
}
//输出栈
void PrintfLink(Link* stack)
{
	if (!stack&&stack->count)	return ;
	Node* node = stack->top;
	while (node)
	{
		printf("%d", node->data);
		node = node->next;
	}
	return;
}
//清空数据,释放结点内存,实际上就是pop所有数据
int ClearLink(Link* stack)
{
	if (!stack||!stack->count)	return	0;
	while (stack->count)
	{
		Node* node = stack->top;
		stack->top = node->next;
		free(node);
		stack->count--;
	}
	return 1;
}

int main()
{
	Link stack;
	InitLink(&stack);
	int n , m;
	cin >> n >> m;
	int yu = 0;
	while(n >= m)
	{
		yu = n % m;
		n /= m;
		push(&stack, yu);
	}
	if(n != 0) push(&stack, n);

	PrintfLink(&stack);
	 ClearLink(&stack);
	return 0;
}

用线性存储方式实现栈,栈没有清除内容的方式,就是通过指针的变化来进行元素的入栈和元素的出栈;

链表式的栈就是一种倒插入的链表,然后顶指针top一直指向的都是最新插入的元素,然后输出就通top指针一直next向后跳,直到NULL;

 

猜你喜欢

转载自blog.csdn.net/Strawberry_595/article/details/83181336
今日推荐