user-0 提问: 求大神指点迷津

回复:
user: user-0

user: user-0
content: [code=c][/cod] #include<stdio.h> #include<malloc.h> typedef char ElemType; typedef struct LNode { ElemType data;   struct LNode *next; }LinkList;  void IntiList(LinkList *&s)//初始化单链表 	{s=(LinkList*)malloc(sizeof(LinkList *)); 	s->next =NULL; 	}  void CreatList(LinkList *&s,ElemType a[],int n)//插入元素  {	LinkList *r,*m;     int i;     s=(LinkList*)malloc(sizeof(LinkList *)); 	r=s; 	for(i=0;i<n;i++) 		{	m=(LinkList*)malloc(sizeof(LinkList *));	 			m->data=a[i]; 			r->next =m; 			r=m; 		}  		r->next =NULL; 	} 	void DispList(LinkList *s)//输出单链表 	{ LinkList *p=s->next; 	  while(p!=NULL) 		 { printf("%c ",p->data); 		   p=p->next; 		} 	       printf("\n"); 	} 	void Length(LinkList *s)//求单链表长度 	{  LinkList *p=s->next; 	   int n=0; 	 while(p!=NULL) 		 {  n++; 	        p=p->next ; 		 }       printf("%d",n); 	  printf("\n"); 	} 	bool Listw(LinkList *s)//判断单链表是否为空 	{return(s->next ==NULL); 	}   bool GetElem(LinkList *&s,int i,ElemType &e)//求线性表中某个元素值   { 	  LinkList *p=s ; 	  int n=0; 	while(p!=NULL && n<i) 	{  n++; 	  p=p->next ; 	}      if(p==NULL) 	    		return false; 	  	 else 	 { e=p->data ;        return true;       printf("%c",e); 	 }     } int Located(LinkList *s,ElemType e)//查找元素地址   { LinkList *p=s->next;     int n=0; 	while(p!=NULL && p->data!=e) 	{  n++; 	   p=p->next; 	} 	if(p==NULL) 			return(0); 	else 		return(n); 	      } bool charu(LinkList *&s,int i,ElemType e)//插入元素 {  LinkList *p=s,*m;int j=0;    while(p->next !=NULL &&j<i-1) 		 { j++; 			p=p->next ; 		 }   if(p->next==NULL) 		{ 	     return false; 		} else 			{ 				m=(LinkList *)malloc(sizeof(LinkList *)); 				m->data =e; 				m->next =p->next ; 				p->next =m; 				return true; 			}  } bool ListDelete(LinkList *&s,int i,ElemType &e)//删除元素 {  LinkList *q,*p=s; int j=0;    while(p!=NULL &&j<i-1) 			{  j++;                p=p->next ;  			}    if(p==NULL ) 	   return false;    else    { q= p->next  ;      e=q->data ; 	 p->next =q->next ; 	free(q);     return true;    }  } void DestroyList(LinkList *&s)//销毁线性表 { LinkList *p=s,*q=p->next ;   while(q!=NULL)   { free(p);     p=q; 	q=q->next ;        }   free(p);  }  void main()  {  LinkList *s;    ElemType e;   char a[5]={'a','b','c','d','e'};    printf("  (1)初始化单链表h\n");   IntiList(s);   printf("  (2)依次采用尾插法插入a,b,c,d,e元素\n");   CreatList(s,  a,5);   printf("  (3)输出单链表h:");   DispList(s);    printf("  (4)输出单链表h的长度:");   Length(s);   printf("  (5)判断单链表h为%s\n",(Listw(s)?"空":"非空"));   printf("  (6)单链表h的第三个元素:"); GetElem(s,3,e);  printf("  (7)输出元素a的位址:%d\n",Located(s,'a')); printf("  (8)在第4个位置插入元素f\n"); charu(s,4,'f'); printf("  (9)输出单链表h:"); DispList(s); printf("  (10)删除h的第三个元素\n");  ListDelete(s,3,e); printf("  (11)输出单链表h:");   DispList(s);   printf("  (12)释放单链表h");   }[/size]

user: user-1
content: 不客气，说起来那本书上的源码下载地址我是找不到源码，只好自己敲了

user: user-0
content: 恩恩。谢谢。

user: user-1
content: lz这个是李春葆的数据结构上机指导吧！我也只是个新手，前两天刚把这个敲出来，这个代码没问题，lz可以自己对比一下，看哪里有问题# include <stdio.h> # include <malloc.h> # define MaxSize 50 typedef char ElemType; typedef struct { 	ElemType data[MaxSize]; 	int length; } SqList; void InitList(SqList * &L) { 	L = (SqList *)malloc(sizeof(SqList)); 	L->length = 0; } void DestroyList(SqList * L) { 	free(L); } int ListEmpty(SqList * L) { 	return (L->length == 0); } int ListLength(SqList * L) { 	return (L->length); } void DispList(SqList * L) { 	int i; 	if(ListEmpty(L)) 		return; 	for(i=0; i<L->length; i++) 		printf("%c", L->data[i]); 	printf("\n"); } int GetElem(SqList * L, int i, ElemType &e) { 	if(i<1 || i>L->length) 		return 0; 	e = L->data[i-1]; 	return 1; } int LocateElem(SqList * L, ElemType e) { 	int i = 0; 	while(i<L->length && L->data[i]!=e) 		i++; 	if(i>L->length) 		return 0; 	else 		return i+1; } int ListInsert(SqList * &L, int i, ElemType e) { 	int j; 	if(i<1 || i>L->length+1) 		return 0; 	i--; 	for(j=L->length; j>i; j--) 		L->data[j] = L->data[j-1]; 	L->data[i] = e; 	L->length++; 	return 1; } int ListDelete(SqList * &L, int i, ElemType &e) { 	int j; 	if(i<1 || i>L->length+1) 		return 0; 	i--; 	e = L->data[i]; 	for(j=i; j<L->length; j++) 		L->data[j] = L->data[j+1]; 	L->length--; 	return 1; }  # include <stdio.h> # include <malloc.h> # define MaxSize 50 typedef char ElemType; typedef struct { 	ElemType data[MaxSize]; 	int length; } SqList; extern void InitList(SqList * &L); extern void DestroyList(SqList * L); extern int ListEmpty(SqList * L); extern int ListLength(SqList * L); extern void DispList(SqList * L); extern int GetElem(SqList * L, int i, ElemType &e); extern int LocateElem(SqList * L, ElemType e); extern int ListInsert(SqList * &L, int i, ElemType e); extern int ListDelete(SqList * &L, int i, ElemType &e); int main(void) { 	SqList * L; 	ElemType e; 	printf("(1)初始化顺序表L\n"); 	InitList(L); 	printf("(2)依次采用尾插法插入a, b, c, d, e元素\n"); 	ListInsert(L, 1, 'a'); 	ListInsert(L, 2, 'b'); 	ListInsert(L, 3, 'c'); 	ListInsert(L, 4, 'd'); 	ListInsert(L, 5, 'e'); 	printf("(3)输出顺序表L:"); 	DispList(L); 	printf("(4)顺序表L长度 = %d\n", ListLength(L)); 	printf("(5)顺序表L为%s\n", (ListEmpty(L)?"空":"非空")); 	GetElem(L, 3, e); 	printf("(6)顺序表L的第3个元素 = %c\n", e); 	printf("(7)元素a的位置 = %d\n", LocateElem(L, 'a')); 	printf("(8)在第4个元素位置上插入f元素\n"); 	ListInsert(L, 4, 'f'); 	printf("(9)输出顺序表L:"); 	DispList(L); 	printf("(10)删除L的第3个元素\n"); 	ListDelete(L, 3, e); 	printf("(11)输出顺序表L:"); 	DispList(L); 	printf("(12)释放顺序表L\n");  	DestroyList(L);  	return 0; } 这是两个.cpp文件

user: user-1
content: 网络不好，看不到图片 可以贴出代码看看

user: user-2
content: 最后的删除操作 应该有有错误的！贴出来瞧瞧

user: user-3
content: 代码贴出来看看