- 论坛徽章:
- 0
|
刚学习编程 在网上找了个数据结构的例子 但编译后运行总是出现段错误 不知其解 希望高手指点怎么调试程序 有时我加一些打印语句跟踪 但在main开始就加打印语句但什么也打印不出来 希望高手指点一下调试心得 不胜感激! 我的源程序是:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
//#include <conio.h>
#define ERROR 0
#define OK 1
#define EQUAL 1
struct STU
{
char name[20];
char stuno[10];
int age;
int score;
}stu[50];
typedef struct STU ElemType;
struct LIST
{
ElemType elem[50];
int length;
};
typedef struct LIST List;
void init(List **L)
{
*L = (List *)malloc(sizeof(List));
(*L)->length = 0;
}
int ListLength(List *L)
{
return L->length;
}
void GetElem(List L,int i,ElemType *e)
{
*e = L.elem[i];
}
int EqualList(ElemType *e1,ElemType *e2)
{
if(strcmp(e1->name,e2->name))
return 0;
if(strcmp(e1->stuno,e2->stuno))
return 0;
if(e1->age != e2->age)
return 0;
if(e1->score != e2->score)
return 0;
return 1;
}
int LocateElem(List *La,ElemType e,int type)
{
int i;
switch(type)
{
case EQUAL:
for(i=0;i<La->length;i++)
if(EqualList(&La
->elem[i],&e))
return 1;
break;
default:
break;
}
return 0;
}
void UnionList(List *La,List *Lb)
{
int i;
int La_len,Lb_len;
ElemType e;
La_len = ListLength(La);
Lb_len = ListLength(Lb);
for (i=0;i<Lb_len;i++)
{
GetElem(*Lb,i,&e);
if(!LocateElem(La,e,EQUAL))
ListInsert
(La,++La_len,e);
}
}
void PrintList(List L)
{
int i;
printf("name stuno
age score\n");
for(i=0;i<L.length;i++)
printf("%-10s\t%s\t%d\t%
d\n",L.elem[i].name,L.elem[i].stuno,L.elem
[i].age,L.elem[i].score);
printf("\n");
}
int ListInsert(List *L,int i,ElemType e)
{
ElemType *q,*p;
if(i<1 || i>L->length+1)
return ERROR;
q = &(L->elem[i-1]);
for(p=&(L->elem[L->length-1]);p>=q;p--)
*(p+1) = *p;
*q = e;
++(L->length);
return OK;
}
int main()
{
struct STU e;
List *La,*Lb;
// La = NULL;
// Lb = NULL;
printf("\n\n----------List Demo is
running...-----------");
init(&La);
strcpy(e.name,"zhang");
strcpy(e.stuno,"1001");
e.age = 22;
e.score = 78;
ListInsert(La,1,e);
strcpy(e.name,"wang");
strcpy("e.stuno","1002");
e.age = 23;
e.score = 88;
ListInsert(La,2,e);
PrintList(*La);
printf("List A length now is:%d\n",La-
>length);
init(&Lb);
strcpy(e.name,"li");
strcpy(e.stuno,"1003");
e.age = 21;
e.score = 90;
ListInsert(Lb,1,e);
strcpy(e.name,"zhao");
strcpy(e.stuno,"1004");
e.age = 19;
e.score = 66;
ListInsert(Lb,2,e);
PrintList(*Lb);
printf("Union..........\n");
UnionList(La,Lb);
printf("Now is:\n");
PrintList(*La);
} |
|