user-0 提问: 线性表的初始化，查找，插入，删除的程序错在哪里？？？还请指教

回复:
user: user-1
content: 仅供参考//带表头结点的单向链表 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <time.h> struct NODE {     int          data;     struct NODE *next; } H,*head,*p,*q,*s1,*s2,*s3,*s4,*s; int i,j,k,n,t,m; int main() {     srand(time(NULL));      //填写头节点数据     H.data=-1;     H.next=NULL;     head=&H;      //创建10个节点的单链表     p=head;     for (i=0;i<10;i++) {         q=(struct NODE *)malloc(sizeof(struct NODE));         if (NULL==q) return 1;         q->data=rand()%100;//填写0..99的随机值         q->next=NULL;         p->next=q;         p=q;     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //将值为5的结点插入到单链表的第k个结点前     k=3;     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==n) {             q=(struct NODE *)malloc(sizeof(struct NODE));             if (NULL==q) return 1;             q->data=5;             q->next=p->next;             p->next=q;             break;         }         p=p->next;     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //删除第k个节点     k=5;     n=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         n++;         if (k==n) {             q=p->next;             if (q) {                 p->next=q->next;                 free(q);             }             break;         }         p=p->next;     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //从小到大排序     for (p=head;p!=NULL && p->next!=NULL;p=p->next) {         for (q=p->next;q!=NULL && q->next!=NULL;q=q->next) {             if (p->next->data > q->next->data) {                  //交换data //              printf("swap %02d %02d\n",p->next->data,q->next->data); //              t=p->next->data;p->next->data=q->next->data;q->next->data=t;                  //或者                  //交换next //              printf("swap %02d %02d\n",p->next->data,q->next->data);                 s1=p->next;                 s2=p->next->next;                 s3=q->next;                 s4=q->next->next;                  if (s2!=s3) {                      p->next=s3;                     s3->next=s2;                      q->next=s1;                     s1->next=s4;                 } else {                      p->next=s3;                     s3->next=s1;                            q=s3;                     s1->next=s4;                 }                  //输出整个单链表 //              s=head->next; //              while (1) { //                  if (NULL==s) { //                      printf("\n"); //                      break; //                  } //                  printf("%02d->",s->data); //                  s=s->next; //              } //              getchar();             }         }     }      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //将单链表中前 m 个结点和后 n 个结点进行互换,m+n为链表总长10     m=4;     n=6;     k=0;     p=head;     while (1) {         if (NULL==p) {             break;         }         k++;         if (m+1==k) {             q=p;         }         s=p;         p=p->next;     }     s1=head->next;     head->next=q->next;     s->next=s1;     q->next=NULL;      //输出整个单链表     s=head->next;     while (1) {         if (NULL==s) {             printf("\n");             break;         }         printf("%02d->",s->data);         s=s->next;     }      //释放所有节点     p=head->next;     while (1) {         if (NULL==p) {             break;         }         q=p->next;         free(p);         p=q;     }      return 0; } //18->94->58->17->27->20->43->57->75->78-> //18->94->05->58->17->27->20->43->57->75->78-> //18->94->05->58->27->20->43->57->75->78-> //05->18->20->27->43->57->58->75->78->94-> //43->57->58->75->78->94->05->18->20->27-> //

user: user-2
content: 难道lz不能自己改吗

user: user-0
content: 太感谢你了！！！我想追问一下能不能输出它查找的元素的值？

user: user-3
content: # include<stdio.h> # include<stdlib.h> # define LIST_INIT_SIZE 100 # define LISTINCREMENT 10 # define OVERFLOW -2 # define OK 1 # define ERROR 0 typedef int ElemType;  typedef struct Sqlist { 	ElemType *elem; 	int length; 	int listsize; }Sqlist;  void main() { 	Sqlist *L,S; 	L=&S; 	int i,j,e,n; 	int InitList_Sq(Sqlist *L,int n); 	int LocateELem_Sq(Sqlist *L,int i); 	int ListInsert_Sq(Sqlist *L,int i,ElemType e); 	int ListDelete_Sq(Sqlist *L,int i); 	int PutList(Sqlist *L); 	printf("输入表的长度：\n"); 	scanf("%d",&n); 	InitList_Sq(L,n); 	PutList(L); 	printf("输入查找的位置:\n"); 	scanf("%d",&i); 	LocateELem_Sq(L,i); 	PutList(L); 	printf("输入插入的位置：\n"); 	scanf("%d",&i); 	printf("输入插入的值：\n"); 	scanf("%d",&e); 	ListInsert_Sq(L,i,e); 	PutList(L); 	printf("输入删除的位置：\n"); 	scanf("%d",&j); 	ListDelete_Sq(L,j); 	PutList(L); }  int InitList_Sq(Sqlist *L,int n) { 	int i; 	ElemType *newbase; 	L->elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType)); 	if(!L->elem) 		exit(OVERFLOW); 	L->length=0; 	L->listsize=LIST_INIT_SIZE; 	printf("请输入表中的数据:\n"); 	for(i=0;i<n;i++) 	{  		scanf("%d",&L->elem[i]); 		++L->length; 		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; 		} 	} 	return OK; }  int ListInsert_Sq(Sqlist *L,int i,ElemType e) { 	ElemType *p,*q,*newbase; 	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; } int LocateELem_Sq(Sqlist *L,ElemType e) { 	for(int i=0;i<L->length;i++) 		if(L->elem[i]==e) return i+1; 	printf("查找的元素为:\n%d\n",e); 	return 0; } int ListDelete_Sq(Sqlist *L,int i) { 	ElemType *p,*q; 	int e; 	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; 	printf("删除的元素为：\n%d\n",e); 	return 0; }  int PutList(Sqlist *L) { 	int *p,*q; 	p=L->elem; 	q=L->elem+(L->length-1); 	printf("线性表中元素为：\n"); 	for(p=L->elem;p<=q;++p) 	{ 		printf("%d  ",*p); 	} 	printf("\n表长为：%d  \n",L->length); 	return OK; }改了一下，编译通过了。