读书人

!怎么利用链表建立一个堆栈?

发布时间: 2012-09-10 11:02:32 作者: rapoo

求助!!如何利用链表建立一个堆栈??
求助!!如何利用链表建立一个堆栈??下面的程序错在哪了??
1 #include<stdio.h>
2 #include<stdlib.h>
3 struct node{ //建立结构体
4 int num;
5 struct node *next;
6 };
7 int w=0;
8 struct node *push(int i) / /将一个数字压入堆栈
9 {
10 w+=1;
11 struct node *head,*end,*p1,*p2;
12 p1=(struct node *)malloc(sizeof(struct node ));
13 p2=(struct node *)malloc(sizeof(struct node ));
14 head=(struct node *)malloc(sizeof(struct node ));
15 end=(struct node *)malloc(sizeof(struct node));
16 end->next=NULL;
17 p1->num=i;
18 if(w==1)p1=end;
19 else p2=p1;
20 head=p2;
21 return head;
22 }
23 void pop(struct node *p) //出栈
24 {
25 while(p!=NULL)
26 {
27 printf("%d",p->num);
28 p=p->next;
29 }
30
31 }
32 void main() //主函数
33 {
34 int n=0;
35 struct node ww;
36 struct node *p1,*p2,*head;
37 p1=(struct node *)malloc(sizeof(struct node ));
38 p2=(struct node *)malloc(sizeof(struct node ));
39 head=NULL;
40 scanf("%d",&p1->num);
41 while(p1->num!=0){ //建立链表
42 n+=1;
43 if(n==1) head=p1;
44 else p2->next=p1;
45 p2=p1;
46 p1=(struct node *)malloc(sizeof(struct node ));
47 scanf("%d",&p1->num);
48 }
49 p2->next=NULL;
50 p1=head;
51 while(p1!=NULL){ //输出链表
52 head=push(p1->num); //调用压栈函数
53 printf("%d\n",p1->num);
54 p1=p1->next;
55 }
56 pop(head); //调用出栈函数
57 }
58
输入1 2 3 4 5 0时输出 1 2 3 4 5 5
到底是什么原因阿?可以对函数进行大改动,求指教!!!!!!!!!

[解决办法]
可以在压栈/出栈函数处打断点,跟踪下head end 还有p1 p2的值,从先进后出的原则去看各个值,一般就能搞定。
[解决办法]
知道堆栈的属性 就不难建立了
[解决办法]
struct node *push(int i) / /将一个数字压入堆栈
{
struct node *head,*p1;
p1=(struct node *)malloc(sizeof(struct node ));
p1->num=i;
w+=1;
if(w==1)
{
head=p1;
p1->next=NULL;
}
else {p1->next=head;
head=p1;
}
return head;
}

[解决办法]
struct node* pop(struct node *heap) //出栈
{
struct node *p;
p=heap;
if(p!=NULL)
printf("%d\n",p->num);
heap=p->next;
free(p);
return heap;
}
------解决方案--------------------


亲爱的,我利用您的代码帮你写好了完完整整的代码了,花了我好多宝贵时间哦,如果喜欢的话就给分了吧。

C/C++ code
//输入1 2 3 4 5 0时输出 5 4 3 2 1#include<stdio.h>#include<stdlib.h>#define true  1#define false 0typedef struct node{ //建立结构体    int num;      struct node *next;}Node;int push(Node **head, int num) //将一个数字压入以head为头的堆栈{        Node *nextNode;    if(NULL==(nextNode=(struct node *)malloc(sizeof(struct node )))){        return false;    }    nextNode->num = num;        if(NULL == *head){        nextNode->next = NULL;        *head = nextNode;        return true;    }     nextNode->next = *head;//尾插法    *head = nextNode;        return true;}int pop(Node **head,int *num) //出栈{    Node *temp;        if(NULL == *head)return false;        *num = (*head)->num;    temp = (*head)->next;        free(*head);        *head = temp;        return true;}int main(int argc, char* argv[]) //主函数  {    int n=0,num;    Node *head;    head=NULL;        printf("Please input numbers end with 0 :\n");        while(1){ //如果不是0,压栈        scanf("%d",&num);        if(0 == num)break;        push(&head,num);        n++;    }        while(pop(&head,&num)){ //出栈输出        printf("%d ", num);    }    printf("\nTotal : %d\n",n);} 

读书人网 >C语言

热点推荐