2 pzq25 pzq25 于 2016.09.13 12:34 提问

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
zqbnqsdsmd   2016.10.04 23:50
Csdn user default icon
上传中...
上传图片
插入图片