ǰλ: ҳ > >

ӵ*CԽ̳̣ջʵ------100дʵʽջ

ʱ:

ջĽ

԰ٶȰٿƵĽܣ



ջstackջһ޵Ա޶ڱβвɾԱһ˱ΪջԵأһ˳ΪջסһջԪֳջջѹջǰԪطŵջԪص棬ʹ֮ΪµջԪأһջɾԪֳջջǰջԪɾʹڵԪسΪµջԪء



ʵǼ򵥵ջĸ
1.ջһִ洢ṹҲ˵洢ݵģ
2.ջһƵԱÿԪرջɾԪҲҪջʼɾһȽFILOݴ洢ṹ


ͺһƹͲdzģǹرյġÿηȡ򣬶ҪǸС

Ҳ˵ҪȡADZȰ˳ȡE,D,C,B򣬲ſԽAȡֱȡAҲƵ˼ջһƵԱ


ջCʵ

CʵջIJҪCеĽṹһʶԽṹȫ˽ĿԲοҵһƪ
ӵ*CԽṹ̳----ְֽ0ʼдѧɼϵͳ룩


ջIJ֣ʽջ˳ջʽջͨʵֵģ˳ջ顣ߵIJʽ𡣼˵ʽջָͨ벻ָһԪصĵַʵջԪصĽ˳ջ˵ΪڴдݿֻҪָԼԼʵջԪصĶȡƪҪܵʽջʵز


ջʲô֮󣬺ջĻջ¼
1.ջһԪشջУ
2.ջԪشջȡ
3.ȡջԪأ
4.жջǷΪգ


#include
#include

struct node { //жڵĽṹ
int data;
struct node* next;
};

struct node* creatnode(int data) //еĴڵ㺯
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = data;
newnode->next = NULL;
return newnode;
}

ڿʼʵջ֮ǰ׼Ϊʽջ漰ĻݡⲿݿԲοҵһƪĶϸܣ׼ֱӽ⡣ǶһջĽṹ壬ʹڵһģһջ


struct stack{
struct node* stacktop; //ջṹ嵱Уһnodeṹջָ
int size; //¼ջԪظ
};

struct stack* creatstack() //ջ
{
struct stack* mystack = (struct stack*)malloc(sizeof(struct stack)); //ڴռ
mystack->stacktop = NULL;
mystack->size = 0; //ʼ
return mystack; //ر
}

ﶨһstack͵Ľṹ壬ṹаһint͵size¼ջԪصĸһnode* ָ͵Ľṹ壬ջָ룬ҲΪıͷ
дջջҪڲһҪĸջڶջֵǶ١


void push(struct stack* mystack,int data)
{
struct node* newnode = creatnode(data);
newnode->next = mystack->stacktop;
mystack->stacktop = newnode;
mystack->size ++;
}

ջʵеͷ巨һģݶǴӱͷ롣ջжһ־λsizeÿջsize+1ջsize-1ͿԼ¼ջԪظڶdzջҲ൱еĽڵɾֻпڵɾջṹУֻɾĽڵ㡣


void pop(struct stack* mystack)
{
if(mystack->size == 0)
{
printf("ջԪΪ ޷ջ");
system("pause"); //ֹ
return;
}
else
{
struct node* nextnode = mystack->stacktop->next; //мһջһڵ
free(mystack->stacktop); //ͷջڴռ
mystack->stacktop = nextnode; //ջһڵ¸ֵջ
mystack->size --;
}
}

ջͳջ֮ʣµIJ־ͱȽϼˣжջǷΪգֻҪжջsizeֵǷΪ0ɣȡջԪأֱӷmystack->stacktop->data


//ȡջԪ
int top(struct stack* mystack)
{
if(mystack->size == 0)
{
printf("ջԪΪ ȡʧ");
return ;
}
else
{
return mystack->stacktop->data;
}
}

//жջǷΪ
int empty(struct stack* mystack)
{
if(mystack->size == 0)
{
return 0; //ʾջΪ
}
else
{
return 1; //ʾջΪ
}
}


ͨЩջͿʵջĹˡֻҪеЩװɣ


int main()
{
struct stack* mystack = creatstack(); //ջ
push(mystack,1); //ջ
push(mystack,2);
push(mystack,3);
while(empty(mystack)) //жջǷΪգΪѭջԪ
{
printf("%d --> ",top(mystack)); //ջԪ
pop(mystack); //ջԪسջ
}

printf("
");
system("pause");
return 0;
}

ǾʵջIJݰ1,2,3˳ջջʱ˳3,2,1

Դʣ߾дIJõĵطӭָ룺


#include
#include


struct node{
int data;
struct node* next;
};
struct node* creatnode(int data)
{
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = data;
newnode->next = NULL;
return newnode;
}
void insertByHead(struct node* headnode,int data)
{
struct node* newnode = creatnode(data);
newnode->next = headnode->next;
headnode->next = newnode;
}
struct stack{
struct node* stacktop; //ջָ
int size; // ݽṹеijò ¼ջԪصĸ
};
struct stack* creatstack() //ջ
{
//ָ---> ̬ڴ
struct stack* mystack = (struct stack*)malloc(sizeof(struct stack));
//ʼ
mystack->stacktop = NULL;
mystack->size = 0;
//ر
return mystack;
}
//ջ
void push(struct stack* mystack,int data)
{
//ջ--->ıͷ
struct node* newnode = creatnode(data);
newnode->next = mystack->stacktop;
mystack->stacktop = newnode;
mystack->size ++; //ջԪؼһ
}
//ȡջԪ
int top(struct stack* mystack)
{
if(mystack->size == 0)
{
printf("ջΪ ޷ȡ
");
system("pause");
return ;
}
return mystack->stacktop->data;
}
//ջ
void pop(struct stack* mystack)
{
if(mystack->size == 0)
{
printf("ջΪ ޷ջ
");
system("pause");
return ;
}
else
{
struct node* nextnode = mystack->stacktop->next;
free(mystack->stacktop);
mystack->stacktop = nextnode;
mystack->size --; //ջԪظ
}
}
int empty(struct stack* mystack)
{
if(mystack->size == 0)
{
return 0; //ʾջΪ
}
else
return 1; //ʾջΪ
}
int main()
{
struct stack* mystack = creatstack();

push(mystack,1);
push(mystack,2);
push(mystack,3);

while(empty(mystack))
{
printf("%d---",top(mystack));
pop(mystack);
}
printf("
");


printf("%d---",top(mystack));
system("pause");
return 0;
}



ӣ