/*****************************************************************
* Copyright (C) 2018 SU_QIAN Ltd. All rights reserved.
*
* 文件名称:linklist.c
* 创 建 者:ls
* 创建日期:2018年12月02日
* 描 述:
*
*****************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR -1
typedef char Elementtype;
typedef struct node
{
Elementtype data;
struct node* next;
}Node;
typedef Node* llist;
int Insertlist(llist *l,int loc,Elementtype e);//可指定位置插入
int Deletelist(llist *l,int loc,Elementtype e);//可指定位置删除
int Traverselist(llist l);//遍历
int listlength(llist l);//求链表的长度
int Initlist(llist *l);//初始化链表
int Isempty(llist L);//判断链表是否为空
int Reviselist(llist *l,int loc,Elementtype e);//可指定位置修改
char Getlist(llist l,int loc);//获取某个位置的链表上的信息
int Locatelist(llist l,Elementtype* ch);//定位
int main(int argc,char *argv[])
{
Elementtype dat,rev;
int t;
char ch='a';
llist l;
Initlist(&l);
printf("%d\n",listlength(l));
printf("第一个(头插)data\n");
scanf("%c",&dat);
Insertlist(&l,1,dat);
printf("%d\n",listlength(l));
printf("第二个(头插)data\n");
scanf(" %c",&dat);
Insertlist(&l,1,dat);
printf("%d\n",listlength(l));
printf("第三个(尾插)data\n");
scanf(" %c",&dat);
Insertlist(&l,listlength(l)+1,dat);
printf("%d\n",listlength(l));
Traverselist(l);
printf("第四个(插在第二个)data\n");
scanf(" %c",&dat);
Insertlist(&l,2,dat);
printf("%d\n",listlength(l));
Deletelist(&l,1,dat);
printf("删除第一个\n");
Traverselist(l);
printf("修改第三个数据:\n");
scanf(" %c",&rev);
Reviselist(&l,3,rev);
Traverselist(l);
printf("获取第三个数据:\n");
rev=Getlist(l,3);
printf("%c\n",rev);
printf("寻找a的位置:\n");
t=Locatelist(l,&ch);
printf("%d",t);
return 0;
}
int Initlist(llist *l)
{
*l=NULL;
return OK;
}
int listlength(llist l)
{
llist q;
int count=0;
q=l;
while(q != NULL)
{
q=q->next;
count++;
}
return count;
}
int Isempty(llist L)
{
if(NULL = = L)
return 1;
else
return 0;
}
int Insertlist(llist *l,int loc,Elementtype e)
{
int len,start=1;
llist p,q;
p=(llist)malloc(sizeof(Node));
if(p = = NULL)
{
return ERROR;
}
q=(*l);
len=listlength(*l);
if(loc>0 && loc <=(len+1))
{
if(loc = = 1)
{
p->data=e;
p->next=NULL;
if(Isempty(*l))
{
*l=p;
}
else
{
p->next=*l;
*l=p;
}
}
else
{
while( start <(loc-1) )
{
q=q->next;
start++;
}
p->data=e;
p->next=q->next;
q->next=p;
}
return OK;
}
else
{
return ERROR;
}
}
int Deletelist(llist *l,int loc,Elementtype e)
{
int len,start=1;
llist p,q;
q=p=*l;
len=listlength(*l);
if(*l = = NULL)
{
return ERROR;
}
if(loc>0 && loc<=len)
{
if(loc==1)
{
e=p->data;
q=p->next;
*l=q;
free(p);
}
else
{
while(start < (loc-1))
{
start++;
q=q->next;
}
if(q->next == NULL)
{
e=q->data;
*l=NULL;
free(q);
printf("pop1\n");
}
else
{
q=p->next;
e=q->data;
p->next=q->next;
free(q);
}
}
return OK;
}
else
{
return ERROR;
}
}
int Traverselist(llist l)
{
llist q=l;
printf("**\n");
if(l == NULL)
{
return ERROR;
}
else
{
do
{
printf("%c\n",q->data);
q=q->next;
}while(q != NULL);
}
return OK;
}
int Reviselist(llist *l,int loc,Elementtype e)
{
int start=1;
llist p,q;
p=*l;
if(loc>0 && loc <=listlength(*l))
{
while(start<loc)
{
p=p->next;
start++;
}
p->data=e;
return OK;
}
else
return ERROR;
}
Elementtype Getlist(llist l,int loc)
{
int start=1;
char ch;
llist p=l;
if(loc>0 && loc <=listlength(l))
{
while(start<loc)
{
p=p->next;
start++;
}
return (p->data);
}
else
return ERROR;
}
int Locatelist(llist l,Elementtype* ch)
{
llist p;
p=l;
int start=1;
while(strcmp(&(p->data),ch)&& start<=listlength(l))
{
start++;
p=p->next;
}
if(start<=listlength(l))
{
return start;
}
else
{
printf("未查到%c!\n",*ch);
return ERROR;
}
无头链表实现插入删除修改等功能
最新推荐文章于 2023-06-22 16:51:56 发布