pzq25 2016-09-13 04:34 采纳率: 0%
浏览 897

c语言,CreateCollection函数

#include
#include
#include
#include
#define OK 0
#define Err_Memory -1
#define Err_InvalidParam -2
#define Err_Overflow -3
#define Err_IllegalPos -4
#define Err_NoResult -5
typedef char ElemType ;
typedef struct node{
ElemType data;
struct node *next;
}ListNode,*LinkList;
typedef int Status;
Status InitList (LinkList L)
{
if (!L) return Err_InvalidParam ;
L->next=NULL;
return OK;
}
int LocateList (ElemType e,LinkList L)
{
int i=1;
ListNode *p;
if (!L) return Err_InvalidParam ;
p=L->next;
while (p&&strcmp (p->data,e)!=0)
{
i++;
p=p->next;
}
if (!p) return 0;
return i;
}
Status InsertList (LinkList L,int i,ElemType e)
{
ListNode *p,*s;
int k=1;
if (!L) return Err_InvalidParam ;
p=L;
while (k {
k++;
p=p->next;
}
if (k>i||!p) return Err_IllegalPos;
s=(ListNode *)malloc(sizeof(ListNode));
if (!s) return Err_Memory;
s->data=e;
s->next=p->next;
p->next=s;
return OK;
}
Status DeleteList (LinkList L,int i,ElemType *e)
{
ListNode *p,*q;
int k=1;
if (!L) return Err_InvalidParam ;
p=L;
while (k {
k++;
p=p->next;
}
if (k>i||!p) return Err_IllegalPos;
q=p->next;
p->next=q->next;
*e=q->data;
free(q);
return OK;
}
Status CreateCollection (LinkList C)
{
int count=1;
ListNode *s,*p;
ElemType e;
if (InitList(C)!=OK) return Err_NoResult ;
p=C;
while (1)
{
s=(ListNode *)malloc(sizeof(ListNode));
if (!s) return Err_Memory;
e=(ElemType)malloc(11*sizeof(char));
if (!e) return Err_Memory;
printf("请输入第%d个元素:",count++);
gets(e);
if (strcmp(e,"#")==0) break ;
if (LocateList(e,C)==0)
{
if (InsertList(p,1,e)!=OK) return Err_NoResult ;
p=p->next;
}
}
return OK;
}
int main()
{
int k=0;
ListNode *p;
LinkList A,B;
ElemType a;
a=(ListNode
)malloc(sizeof(char));
A=(ListNode *)malloc(sizeof(ListNode));
B=(ListNode *)malloc(sizeof(ListNode));
printf ("请输入给定值:");
scanf("%c",&a);
printf("请输入集合A的元素(输入#结束):\n");
if (CreateCollection(A)!=OK)
{ printf("集合A创建错误!"); getch();return ;}
p=A->next;
while (p)
{
if (strcmp(p->data,a)==0) k++;
p=p->next;
}
printf("结点数为:%d\n",k);
getch();
return 0 ;
}

运行结果:图片说明
麻烦帮忙看一下,为什么会这样?

  • 写回答

1条回答 默认 最新

  • zqbnqsdsmd 2016-10-04 15:50
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了