没有完全实现,不知道错哪里了,不过指针还是难以理解啊
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct LNode
{
ElemType data;
struct LNode * next;
}LNode,*LinkList;
//初始化链表
int InitList_link(LinkList *head)
{
*head =(LinkList)malloc(sizeof(LNode));
if(NULL==*head)
return -1;
(*head)->next=NULL;
return 0;
}
//判空
int ListEmpty_link(LinkList head)
{
if(NULL==head->next)
return 1;
return 0;
}
//尾插法创建单链表
int CreateList_link_tail(LinkList head,int n)
{
int i;
ElemType temp;
LNode *p=NULL,*q=head;
if(n<=0)
return -1;
for(i=1;i<=n;i++)
{
p=(LNode *)malloc(sizeof(LNode));
if(NULL==p)
return -2;
printf("please input no. %d data\'s value: \n",i);
scanf("%d",&temp);
getchar();
p->data=temp;
p->next=NULL;
q->next=p;
q=p;
}
return 0;
}
//求长运算
int ListLength_link(LinkList head)
{
int len=0;
LNode *p=head->next;
while(NULL!=p)
{
len++;
p=p->next;
}
return len;
}
//查找数据元素
int LocateElem_link(LinkList head,ElemType e)
{
int count =1;
LNode *p=head->next;
while(NULL!=p&&p->data!=e)
{
p=p->next;
count++;
}
if(NULL==p)
return 0;
return count;
}
//基于带头结点的单链表实现插入运算
int ListInsert_link(LinkList head,int i,ElemType e)
{
int count =0;
LNode *p=head,*q=NULL;
while(count<i-1&&NULL!=p)
{
p=p->next;
count++;
}
if(count>i-1||NULL==p)
return -1;
q=(LNode *)malloc(sizeof(LNode));
if(NULL==q)
return -2;
q->data=e;
q->next=p->next;
p->next=q;
return 0;
}
//删除运算
int ListDelete_link(LinkList head,int i,ElemType *e)
{
int count=0;
LNode *p=head,*q=NULL;
if(1==ListEmpty_link(head))
return -1;
while(count<i-1 && NULL!=p->next)
{
p=p->next;
count++;
}
if(count>i-1||NULL==p->next)
return -2;
q=p->next;
*e=p->data;
p->next=q->next;
free(q);
return 0;
}
通过调试,结果如下
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct LNode
{
ElemType data;
struct LNode * next;
}LNode,*LinkList;
//初始化链表
int InitList_link(LinkList *head)
{
*head =(LinkList)malloc(sizeof(LNode));
if(NULL==*head)
return -1;
(*head)->next=NULL;
return 0;
}
//判空
int ListEmpty_link(LinkList head)
{
if(NULL==head->next)
return 1;
return 0;
}
//尾插法创建单链表
int CreateList_link_tail(LinkList head,int n)
{
int i;
ElemType temp;
LNode *p=NULL,*q=head;
if(n<=0)
return -1;
for(i=1;i<=n;i++)
{
p=(LNode *)malloc(sizeof(LNode));
if(NULL==p)
return -2;
printf("please input no. %d data\'s value: \n",i);
scanf("%c",&temp);
getchar();
p->data=temp;
p->next=NULL;
q->next=p;
q=p;
}
return 0;
}
//求长运算
int ListLength_link(LinkList head)
{
int len=0;
LNode *p=head->next;
while(NULL!=p)
{
len++;
p=p->next;
}
return len;
}
//查找数据元素
int LocateElem_link(LinkList head,ElemType e)
{
int count =1;
LNode *p=head->next;
while(NULL!=p&&p->data!=e)
{
p=p->next;
count++;
}
if(NULL==p)
return 0;
return count;
}
//基于带头结点的单链表实现插入运算
int ListInsert_link(LinkList head,int i,ElemType e)
{
int count =0;
LNode *p=head,*q=NULL;
while(count<i-1&&NULL!=p)
{
p=p->next;
count++;
}
if(count>i-1||NULL==p)
return -1;
q=(LNode *)malloc(sizeof(LNode));
if(NULL==q)
return -2;
q->data=e;
q->next=p->next;
p->next=q;
return 0;
}
//删除运算
int ListDelete_link(LinkList head,int i,ElemType *e)
{
int count=0;
LNode *p=head,*q=NULL;
if(1==ListEmpty_link(head))
return -1;
while(count<i-1 && NULL!=p->next)
{
p=p->next;
count++;
}
if(count>i-1||NULL==p->next)
return -2;
q=p->next;
*e=q->data;
p->next=q->next;
free(q);
return 0;
}
void DispList(LinkList head)
{
LNode *p=head->next;
while (NULL!=p)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
void DestroyList(LinkList head)
{
LNode *p=head,*q=p->next;
while (q!=NULL)
{
free(p);
p=q;
q=p->next;
}
free(p);
}
int main()
{
int i=0;
ElemType e;
LinkList L;
InitList_link(&L);
ListInsert_link(L,1,'a');
ListInsert_link(L,2,'b');
ListInsert_link(L,3,'c');
ListInsert_link(L,4,'d');
ListInsert_link(L,5,'e');
// CreateList_link_tail(L,5);
// printf("%d\n",ListLength_link(L));
DispList(L);
// ListInsert_link(L,2,'a');
// ListDelete_link(L,4,&e);
// printf("%c\n",e);
printf("%d\n",LocateElem_link(L,'d'));
DestroyList(L);
printf("ok!\n");
return 0;
}
调试成功,问题是%d,和%c的输入问题,还有删除操作时返回的删除元素的值出现偏差,将p改为q即可