|
|
#2
寒风中的细雨2010-10-11 17:17
#include <stdio.h>
#include <malloc.h> #define null 0 typedef struct node //定义链队列 { int data; struct node *next; }link; typedef struct { link *front; link *rear; }linknode; linknode *cas() //链队列入队 { linknode *s = (linknode*)malloc(sizeof(linknode)); s->front = s->rear = null; link *p; int x,tag; int flag = 0; printf("请输入结束符"); scanf("%d",&tag); printf("请输入数据:"); scanf("%d",&x); while(x != tag) { p=(link*)malloc(sizeof(link)); p->data = x; p->next = null; if(flag == 0) { s->rear = s->front = p; flag = 1; } else { s->rear = s->rear->next = p; } printf("请输入数据:"); scanf("%d",&x); } return s; } void de(linknode *s,int *c) //链队列出队 { link *p; c = (int *)malloc(sizeof(int)); *c = 0; int i=1; while(s->front!=0) { p=s->front; s->front=p->next; c = (int *)realloc(c, (i+1)*sizeof(int)); *(c+i-1) = p->data; *(c+i) = 0; ++i; free(p); } for(i=0; *(c+i) != 0; ++i) printf("%d ",*(c+i)); printf("\n"); } void main() { int *c; linknode *s; s=cas(); de(s,c); } |
以下程序是我花了很长时间编的, 请大虾们帮我看看哪里错了 实现的主要是输入一组整数然后输出。
#include<stdio.h>
#include <malloc.h>
#define null 0
typedef struct node //定义链队列
{ int data;
struct node *next;
}link;
typedef struct
{link *front;
link *rear;
}linknode;
linknode *cas() //链队列入队
{linknode *s;
link *p;
int x,tag;
p=(link*)malloc(sizeof(link));
printf("请输入结束符");
scanf("%d",&tag);
while(x!=tag)
{scanf("%d",&x);
p->data=x;
p->next=null;
s->rear->next=p;
s->rear=p;
}
return s;
}
void de(linknode *s,int *c) //链队列出队
{link *p;
while(s->front!=s->rear)
{ p=s->front->next;
s->front->next=p->next;
*c=p->data;
c++;}
printf("%d",c);
free(p);
}
void main()
{int *c;
linknode *s;
s=cas();
de(s,c);
}
高手们帮我看看 这个那里错了