链表表示法
赋值通过ba进行测试因为懒得测试所以只进行ba两个字符进行测试 可能有许多的bug因为查错功能太少
#include<stdio.h>
#include<stdlib.h>
#define size sizeof(node)
enum BOOL {error,ok};
typedef struct node{
char data;
node *next;
}*LinkList;
void greatlist(LinkList &,int);//建立一个链表头
BOOL listInsert(LinkList &,int,char);//插入字符
BOOL show(LinkList);//显示链表元素
BOOL listdel(LinkList &,char);//删除元素
BOOL list_keychar(LinkList, char);//查找元素
BOOL list_keynum(LinkList, int);//通过下标查找元素
void main()
{
LinkList l;
int flag;
greatlist(l, 2);
show(l);
listInsert(l, 3, 'c');
show(l);
listdel(l, 'a');
show(l);
if((flag=list_keychar(l, 'c')))
{
printf("ok
");
}
else printf("error
");
list_keynum(l, 2);
}
void greatlist(LinkList &L,int n)//头插法
{
int i;
LinkList p;
L = (LinkList)malloc(size);
L->next = NULL;
for (i = 0; i < n; i++)
{
p = (LinkList)malloc(size);
scanf("%c", &p->data);
p->next = L ->next;
L-> next = p;
}
}
/*尾插法
void greatlist(LinkList &L,int n)//尾插法用q记录p前一个结构地址
{
int i;
LinkList p,q;
q=L = (LinkList)malloc(size);
q->next = NULL;
for (i = 0; i < n; i++)
{
p = (LinkList)malloc(size);
scanf("%c", &p->data);
p->next = q ->next;
q-> next = p;
q = p;
}
}
*/
BOOL list_keynum(LinkList L, int n)
{
int i;
LinkList p;
i = 0;
char ch;
p = L;
while (p != NULL&&i < n) {
p = p->next;
i++;
}printf("%c
", p->data);
if(i>n&&!p)return error;
}
BOOL list_keychar(LinkList L, char ch)
{
LinkList p;
p = L->next;
while (p != NULL)
{
while (p->data == ch)
return ok;
p = p->next;
}
return error;
}
BOOL listdel(LinkList &L,char ch)
{
LinkList p,q;
q = L;
p = L->next;
while (p != NULL)
{
while (p -> data == ch)
{
q->next = p->next;
free(p);
return ok;
}
q = p;
p = p->next;
}
return error;
}
BOOL listInsert(LinkList &L,int n, char ch)
{
int i;
i = 0;
LinkList p;
p = L;
while (p&&i < n-1 ) { p = p->next; i++; }
LinkList q;
if (!p&&i > n - 1)return error;
q = (LinkList)malloc(size);
q->data = ch;
q->next = p->next;
p->next = q;
return ok;
}
BOOL show(LinkList L)
{
LinkList p;
p = L->next;
while (p != NULL)
{
printf("%c ", p->data); p = p->next;
}
printf("
");
return ok;
if (p == NULL) {
printf("this list is null");
return error;
}
}