主题:各位老兄,帮忙看看这个链表的C程序哪里出现问题好吗?
#include<stdio.h>
#include<stdlib.h>
#define LIST_INIT_SIZE 80
#define ElemType int
#define LISTINCRESMENT 10
typedef struct sq{
int *elem;
int length;
int listsize;
}LinkList;
status InitList_L(LinkList &L);
status ListInsert_L(LinkList &L,int i, ElemTpe e);
status ListDelete_L(LinkList &L,int i,ElemType &e);
int LocateElem_L(LinkList,ElemType e);
void printList(LinkList &L);
Status (*compare)(ElemType,ElemType);//在静态单链线性表L中查找元素
void main()
{
LinkList L;
int a;
int item;
int loc;
if(InitList_L(L)!=1)
return 0;
insertruction();/*显示菜单*/
printf("?");
scanf("%d",a);
while(a!=4){
switch(a){
case 1:
printf("Enter two integer:");
scanf("%d,%d",&loc,&item);
if(ListInsert_L(L,loc,item)==1)
printf("ListInsert OK\n");
else
printf("LinkInsrt error\n");
printList(L);
break;
case 2:
printf("Enter an integer(Location):");
scanf("%d",&loc);
if(ListDelete_L(L,loc,item)==1)
printf("ListDelete is %d\n",item);
else
printf("Delete error\n");
printList(L);
break;
case 3:
printf("Enter a value to be found:\n");
scanf("%d",&item);
if("LocateElem_L(L,item)==0)
printf("not found\n");
else
printf("%d is locate %d.\n",item LocateElem_L(L,item));
break;
default:
printf("Invalid choice:\n\n");
insertruction();
break;
}
printf("?");
scanf("%d",&a);
}
void insertruction(void)
{printf("Enter your choice:\n"
"1 to insert an element into the list:\n"
"2 to delete an element into the list:\n"
"3 to find an element in the list :\n"
}
Status InitList_L(LinkList &L) {
L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L.elem){
printf("Linklist not Init. No memory available.\n");
exit(OVERFLOW);
}
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
} // InitList_Link
int LocateElem_L(LinkList L, ElemType e){
ElemType i,*p;
i=1;
p=L.elem;
//i=S[0].cur;
while(i<=L.length && !(*compare)(*p++,e)) ++i;
if(i<=L.length)
return i;
else return 0;
} // LocateElem_Link
Status ListInsert_L(LinkList &L, int i, ElemType e) {
ElemType *newbase, *q,*p;
if (i<1||i>L.length+1) return ERROR;
if (L.length>=L.listsize) {
newbase= (ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if (!newbase) exit(OVERFLOW);
L.elem=newbase;
L.listsize += LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p= &(L.elem[L.length-1]); p>=q;--p)*(p+1)=*p;
*q=e;
++L.length;
return OK;
}// ListInsert_Link
Status ListDelete_L(LinkList &L, int i, ElemType &e) {
ElemType *p, *q;
if ((i<1)||(i>L.length)) return ERROR;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for (++p;p<=q;++p)*(p-1)=*p;
--L.length;
return OK;
} // ListDelete-Sq
/*打印线性顺序表*/
void printList(LinkList &L) {
ElemType *q;/*int k;*/
if (L.length>0){
printf ("LinkList is: ");
/*for (k=0;k<L->length;k++){
q=&(L->elem[k]);
printf("%d,",*q);}
printf("\n");*/
for (q=L.elem;q<L.elem+L.length;q++){
printf("%d,",*q);
}
printf("\n");
}
else
printf ("List is empty.\n\n");
}
#include<stdlib.h>
#define LIST_INIT_SIZE 80
#define ElemType int
#define LISTINCRESMENT 10
typedef struct sq{
int *elem;
int length;
int listsize;
}LinkList;
status InitList_L(LinkList &L);
status ListInsert_L(LinkList &L,int i, ElemTpe e);
status ListDelete_L(LinkList &L,int i,ElemType &e);
int LocateElem_L(LinkList,ElemType e);
void printList(LinkList &L);
Status (*compare)(ElemType,ElemType);//在静态单链线性表L中查找元素
void main()
{
LinkList L;
int a;
int item;
int loc;
if(InitList_L(L)!=1)
return 0;
insertruction();/*显示菜单*/
printf("?");
scanf("%d",a);
while(a!=4){
switch(a){
case 1:
printf("Enter two integer:");
scanf("%d,%d",&loc,&item);
if(ListInsert_L(L,loc,item)==1)
printf("ListInsert OK\n");
else
printf("LinkInsrt error\n");
printList(L);
break;
case 2:
printf("Enter an integer(Location):");
scanf("%d",&loc);
if(ListDelete_L(L,loc,item)==1)
printf("ListDelete is %d\n",item);
else
printf("Delete error\n");
printList(L);
break;
case 3:
printf("Enter a value to be found:\n");
scanf("%d",&item);
if("LocateElem_L(L,item)==0)
printf("not found\n");
else
printf("%d is locate %d.\n",item LocateElem_L(L,item));
break;
default:
printf("Invalid choice:\n\n");
insertruction();
break;
}
printf("?");
scanf("%d",&a);
}
void insertruction(void)
{printf("Enter your choice:\n"
"1 to insert an element into the list:\n"
"2 to delete an element into the list:\n"
"3 to find an element in the list :\n"
}
Status InitList_L(LinkList &L) {
L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L.elem){
printf("Linklist not Init. No memory available.\n");
exit(OVERFLOW);
}
L.length=0;
L.listsize=LIST_INIT_SIZE;
return OK;
} // InitList_Link
int LocateElem_L(LinkList L, ElemType e){
ElemType i,*p;
i=1;
p=L.elem;
//i=S[0].cur;
while(i<=L.length && !(*compare)(*p++,e)) ++i;
if(i<=L.length)
return i;
else return 0;
} // LocateElem_Link
Status ListInsert_L(LinkList &L, int i, ElemType e) {
ElemType *newbase, *q,*p;
if (i<1||i>L.length+1) return ERROR;
if (L.length>=L.listsize) {
newbase= (ElemType *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if (!newbase) exit(OVERFLOW);
L.elem=newbase;
L.listsize += LISTINCREMENT;
}
q=&(L.elem[i-1]);
for(p= &(L.elem[L.length-1]); p>=q;--p)*(p+1)=*p;
*q=e;
++L.length;
return OK;
}// ListInsert_Link
Status ListDelete_L(LinkList &L, int i, ElemType &e) {
ElemType *p, *q;
if ((i<1)||(i>L.length)) return ERROR;
p=&(L.elem[i-1]);
e=*p;
q=L.elem+L.length-1;
for (++p;p<=q;++p)*(p-1)=*p;
--L.length;
return OK;
} // ListDelete-Sq
/*打印线性顺序表*/
void printList(LinkList &L) {
ElemType *q;/*int k;*/
if (L.length>0){
printf ("LinkList is: ");
/*for (k=0;k<L->length;k++){
q=&(L->elem[k]);
printf("%d,",*q);}
printf("\n");*/
for (q=L.elem;q<L.elem+L.length;q++){
printf("%d,",*q);
}
printf("\n");
}
else
printf ("List is empty.\n\n");
}