慢慢练习,试试


错了一条的代码,都是指针学的不深惹的祸


#include<iostream>
#include<stack>
#include<queue>
#include<cstdio>
#include<cstring>
#include<string> 
#include<cstdlib>
#include<algorithm>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEAIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
typedef struct LIST
{
	ElemType element;
	struct LIST *prior;
	struct LIST *next;
} *PList, List;
typedef struct STACK 
{
	PList base;
} *PStack;
Status InitStack(PStack &s)
{
	/*PList只是一个指针,内存为4,而List为结构体变量;需要看具体情况测量大小 
	cout<<sizeof(List)<<"  "<<sizeof(PList)<<endl;*/
	s=(PStack)malloc(sizeof(List)); 
	if( s==NULL ) return ERROR;
	cout<<"step\n";
	s->base->prior=s->base->next=s->base;
	s->base->element=0;
	return OK;
}
Status Push(PStack &s, ElemType &elem)
{
	PStack p;
	cout<<"steppush"<<endl;
	p=(PStack)malloc(sizeof(List));
	if( p==NULL ) return ERROR;
	cout<<"steppush  new\n";
	p->base->element=elem;
	cout<<p->base->element<<endl;//
	//将p连接到循环链表上 
	cout<<s->base->prior<<endl;//
	p->base->prior=s->base->prior;
	cout<<p->base->prior<<"  "<<s->base->prior<<endl;
	p->base->next=s->base;
	cout<<p->base->next<<"  "<<s->base<<endl;
	s->base->prior->next=p->base;
	cout<<s->base->prior->next<<"  "<<p->base<<endl;
	s->base->prior=p->base;
	cout<<s->base->prior<<"  "<<p->base<<endl;
	//p->base->prior=p->base->next=NULL;//安全处理 
	s->base->element++;//元素个数加1 
	return OK;
}
Status Top(PStack &s, ElemType &elem)
{
	if( s->base->prior=s->base->next ) return ERROR;
	elem=s->base->prior->element;
	return OK;
}
Status Pop(PStack &s, ElemType &elem)
{
	if( s->base->prior==s->base->next ) return ERROR;
	elem=s->base->prior->element;
	PStack p;
	p->base=s->base->prior;
	p->base->prior->next=s->base;
	s->base->prior=p->base->prior;
	s->base->element--;
	free(p);
	p=NULL;
}
int main(int argc, char *argv[])
{
	cout<<sizeof(List)<<"  "<<sizeof(PList)<<"  "<<sizeof(PStack)<<endl;
	PStack s;
	InitStack(s);
	//int a[10];
	int a, b;
	cin>>a;
	cout<<"a="<<a<<endl;
	Push(s, a);
	a=0;
	Top(s, a); 
	cout<<a<<endl;//1
	cin>>b;
	Push(s, b);
	b=0;
	Top(s, b);
	cout<<endl;//2
	/*for(int i=0; i<10; i++) scanf("%d", a+i);
	for(int i=0; i<10; i++) Push(s, a[i]);
	a[0]=0;
	for(int i=0; i<10; i++) printf("%d ", Pop(s, a[0]));*/
	cout<<endl;
	return 0;
}




最后调了一天调成功的代码,还有其他功能没做,其他基本也就是这样了


#include<iostream>
#include<stack>
#include<queue>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
using namespace std;

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEAIBLE -1
#define OVERFLOW -2

typedef void Status;
typedef int ElemType;

typedef struct LIST
{
	ElemType element;
	struct LIST *prior;
	struct LIST *next;
} *PList, List;
typedef struct STACK
{
	PList base;
} *PStack;

Status InitStack(PStack &s)
{
	/*PList代表一个指针,内存为4,而List为结构体变量;需要看具体情况测量大小
	cout<<sizeof(List)<<"  "<<sizeof(PList)<<endl;*/
	/*
	这里不能是
	PStack p;
	p=(PStack)malloc(sizeof(List));
	因为这样是PStack分配了一个大小为sizeof(List)的内存而又转化为PStack类型
	但是PStack结构体就一个变量,造成内存泄漏; 
	*/
	PList p;
	p=(PList)malloc(sizeof(List));
	if( p==NULL ) { cout<<"cuowu==\n"; return; }// ERROR;
	//cout<<"step\n";
	p->next=p->prior=p;
	p->element=0;
	/*
	每个需要使用的指针都要分配内存才能使用
	因为不是很理解,改了一天才改出来 
	*/
	s=(PStack)malloc(sizeof(PStack));
	s->base=p;
	return;// OK;
}
Status Push(PStack &s, ElemType &elem)
{
    //cout<<"push s="<<s<<endl;
	PList p;
	p=(PList)malloc(sizeof(List));
	cout<<"分配的p的地址:"<<p<<endl;
	if( p==NULL ) { cout<<"没有分配内存\n"; return; }// ERROR;
	//cout<<"steppush  new\n";
	p->element=elem;
	//cout<<p->element<<endl;//
	//将p连接到循环链表上
	//cout<<s->base->prior<<endl;//
	p->prior=s->base->prior;
	//cout<<p->prior<<"  "<<s->base->prior<<endl;
	p->next=s->base;
	//cout<<p->next<<"  "<<s->base<<endl;
	s->base->prior->next=p;
	//cout<<s->base->prior->next<<"  "<<p<<endl;
	s->base->prior=p;
	//cout<<s->base->prior<<"  "<<p<<endl;
	s->base->element++;//元素个数加1
	//cout<<s->base->element<<endl;
	return;// OK;
}
Status Top(PStack &s, ElemType &elem)
{
    //cout<<s->base->element<<endl;
	if( !s->base->element ) { cout<<"cuowu\n"; return; }//return ERROR;
	elem=s->base->prior->element;
	return;// OK;
}
Status Pop(PStack &s, ElemType &elem)
{
	if( !s->base->element ) { cout<<"cuowu\n"; return; }//return ERROR;
	elem=s->base->prior->element;
	PStack p;
	/*
	需要使用的指针都需要分配内存 
	这里为什么是PStack就是因为栈结构体里面就是一个PList类型的变量
	所以这里其实使用struct STACK 是多余的,但这是为了便于理解这是设计
	的一个先进后出的栈结构而多出来的结构体,所以sizeof(PList)=sizeof(PStack) 
	(PStack)malloc(sizeof(PStack)) = (PStack)malloc(sizeof(PList))
	但是这里类型一定是转化为PStack,因为我们定义的P是PStack类型的,而不是PList
	类型的虽然他们某种程度上一样(PStack只有一个成员变量就是PList) 
	*/ 
	p=(PStack)malloc(sizeof(PStack));
	p->base=s->base->prior;
	p->base->prior->next=s->base;
	s->base->prior=p->base->prior;
	s->base->element--;
	free(p);
	p=NULL;//这是一个安全考虑的代码,释放该指针指向的内存之后使其指向空防止后面误用 
	return;//
}
int main()
{
	//cout<<sizeof(List)<<"  "<<sizeof(PList)<<"  "<<sizeof(PStack)<<endl;
	PStack s;
	int a[10], b;
	InitStack(s);
	for(int i=0; i<10; i++) scanf("%d", a+i);
	for(int i=0; i<10; i++) Push(s, a[i]);
	for(int i=0; i<10; i++) Pop(s, b), printf("%d ", b);
	cout<<endl;
	while(1);
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值