菜,,,,到爆
参考:
栈的链表实现一直失败,蓝瘦,,,,
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//#define OK 1
//#define ERROR 0
#define STACK_INIT_SIZE 10
#define STACK_INCREMENT_SIZE 20
#define MAXSIZE 100
typedef int ElemType;
typedef struct {
ElemType data[MAXSIZE];
int top;
}qStack;
int printStack(qStack* S)
{
printf("\n\n--------------------------------\n");
for(;S->top >=0;)
{
printf("S->data[%d]:%d\n",S->top,S->data[S->top]);
S->top--;
}
return 1;
}
int Push(qStack* s,ElemType* data)
//int Push(qStack*& s,ElemType*& data)
{
printf("\n\n--------------------------------\n");
if(s->top>=MAXSIZE)
{
printf("getting overflow!");
//resizestack();
}
s->top++;//move cursor
s->data[s->top]=*data;
printf("s->top:%d,s->data[s->top]:%d\n",s->top,s->data[s->top]);
}
int Pop(qStack* s,ElemType* E)
{
printf("\n\n--------------------------------\n");
if(s->top==-1)
{
printf("get the bottom of stack!");
return -1;
}
*E=s->data[s->top];
printf("s->top:%d,s->data[s->top]:%d\n",s->top,s->data[s->top]);
s->data[s->top]=NULL;//clear data
s->top--;//move cursor
}
typedef int ElemType_L;
typedef struct StackNode{
ElemType_L data;
StackNode* next;
}StackNodeL;
StackNodeL* createStackNode()
{
StackNodeL* head=(StackNodeL*)malloc(sizeof(StackNode));
head->next=NULL;
return head;
}
StackNodeL* PushL(StackNodeL* phead,ElemType_L mdata)
{
StackNodeL* temp=createStackNode();
temp->data=mdata;
temp->next=phead;
phead=temp;
printf("%d\n",phead->data);
return temp;
}
StackNodeL* PopL(StackNodeL* phead,ElemType_L* poped_data)
{
if(!phead)
{
printf("stack empty!\n");
return 0;
}
poped_data=phead->data;
//printf("the data going to be poped:%d\n",poped_data);
printf("poped_data:%d\n",poped_data);
StackNodeL* temp=phead->next;
free(phead);
//phead=temp;
//return phead->data;
//return phead;
return temp;
}
int main()
{
/*
* ElemType e=88;
* qStack S={ {6,2,7,8,1},4 };
*
* Push(&S,&e);//why
* printf("e:%d\n",e);
*
* ElemType E;
* for(int i=0;i<5;i++)
* {
* Pop(&S,&E);
* printf("E:%d\n",E);
* }
* printStack(&S);
*/
printf("\n--------------------------------\n");
StackNodeL* s=NULL;
//s=createStackNode();
for(int i=5;i--;)
{
s=PushL(s,i);
//printf("%d\n",i);
}
printf("\n--------------------------------\n");
ElemType_L poped;
for(int i=5;i--;)
{
s=PopL(s,&poped);
printf("the data poped:%d\n",poped);
}
}
这是为什么,啦啦啦啦啦绿
版本2:
原因是没有用引用的,疑问:为什么链表单元free掉之后还可以用,
#include <iostream>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//#define OK 1
//#define ERROR 0
#define STACK_INIT_SIZE 10
#define STACK_INCREMENT_SIZE 20
#define MAXSIZE 100
typedef int ElemType;
typedef struct {
ElemType data[MAXSIZE];
int top;
}qStack;
int printStack(qStack* S)
{
printf("\n\n--------------------------------\n");
for(;S->top >=0;)
{
printf("S->data[%d]:%d\n",S->top,S->data[S->top]);
S->top--;
}
return 1;
}
int Push(qStack* s,ElemType* data)
//int Push(qStack*& s,ElemType*& data)
{
printf("\n\n--------------------------------\n");
if(s->top>=MAXSIZE)
{
printf("getting overflow!");
//resizestack();
}
s->top++;//move cursor
s->data[s->top]=*data;
printf("s->top:%d,s->data[s->top]:%d\n",s->top,s->data[s->top]);
}
int Pop(qStack* s,ElemType* E)
{
printf("\n\n--------------------------------\n");
if(s->top==-1)
{
printf("get the bottom of stack!");
return -1;
}
*E=s->data[s->top];
printf("s->top:%d,s->data[s->top]:%d\n",s->top,s->data[s->top]);
s->data[s->top]=NULL;//clear data
s->top--;//move cursor
}
typedef int ElemType_L;
typedef struct StackNode{
ElemType_L data;
StackNode* next;
}StackNodeL;
StackNodeL* createStackNode()
{
StackNodeL* head=(StackNodeL*)malloc(sizeof(StackNode));
head->next=NULL;
return head;
}
StackNodeL* PushL(StackNodeL* phead,ElemType_L mdata)
{
StackNodeL* temp=createStackNode();
temp->data=mdata;
temp->next=phead;
phead=temp;
printf("%d\n",phead->data);
return temp;
}
StackNodeL* PopL(StackNodeL* phead,ElemType_L& poped_data)
{//引用与指针的区别注意
if(!phead)
{
printf("stack empty!\n");
return 0;
}
poped_data=phead->data;
//printf("poped_data:%d\n",poped_data);
StackNodeL* temp=phead->next;
//printf("phead->next:%p\n",phead->next);
free(phead);
//printf("phead:%p phead->next:%p phead->data:%d\n",phead,phead->next,phead->data);
//return phead->next;//为什么free掉后还可以?
return temp;//返回头节点
}
int main()
{
/*
* ElemType e=88;
* qStack S={ {6,2,7,8,1},4 };
*
* Push(&S,&e);//why
* printf("e:%d\n",e);
*
* ElemType E;
* for(int i=0;i<5;i++)
* {
* Pop(&S,&E);
* printf("E:%d\n",E);
* }
* printStack(&S);
*/
printf("\n--------------------------------\n");
StackNodeL* s=NULL;
//s=createStackNode();
for(int i=5;i--;)
{
s=PushL(s,i);
//printf("%d\n",i);
}
printf("\n--------------------------------\n");
ElemType_L poped;
for(int i=5;i--;)
{
s=PopL(s,poped);//NOTE: not &poped
//PopL(&s,poped);//NOTE: not &poped
printf("the data poped:%d\n",poped);
}
}
结果正常: