#include
#include
#define N 8
struct LNode {
char data; //char-datatype
struct LNode* next;//LNode* List*(结构体的指针型)
}node;
LNode* creatlist(char* a)
{
LNode *head, *p1, *p2; int n = 1, p;
head = (struct LNode*)malloc(sizeof(struct LNode));//动态内存分配
p1 = (struct LNode*)malloc(sizeof(struct LNode));
if (p1) {
for (p = 0; p < N; p++){
p1->data = a[p];
if (p == 0)
head->next = p1;
p2 = p1;//将p1当前结点地址赋值给p2,然后让p1去申请新的内存。
p1 = (struct LNode*)malloc(sizeof(struct LNode));
p2->next = p1;//把新申请的结点地址赋值给上一个结点的next指针(p2->next),实现链表连接。
}
p2->next = NULL;//把最后一个有值的结点的next赋值为空。
free(p1);
return head;
}
}
void outlist(LNode* h)
{
LNode* p; p = h->next;
if (p == NULL) printf("\nThe list is NULL!\n");
else
{
printf("\nHead");
do {
printf("->%c", p->data);
p = p->next;
} while (p != NULL);
printf("->End\n");
}
}
int _find(LNode* h, char ch)
{
LNode* p1;
int c = 1;
p1 = h->next;
while (p1) {
if (p1->data == ch)
return c;
else {
p1 = p1->next;//遍历链表
c++;//计数器
}
}
return 0;//没找到
}
LNode* _delete(LNode* h)
{
char ch; int get, i=0; LNode *p1=NULL,*p2=h->next;
printf("Enter a letter to delete:");
getchar();//上次输入结束的回车
ch = getchar();
while (p2->data != ch && p2->next)//遍历
{
p1 = p2;//p1指向p2(要删除结点)的前一个结点
p2 = p2->next;
}
if (p2->data == ch)//成功找到要删除的结点
{
if (p2->data == h->next->data)//头结点
return h->next;
else if (p2->next == NULL)//尾结点
{
p1->next = NULL;
return h;
}
else//中间结点
{
p1->next = p2->next;
return h;
}
}
return NULL;//没找到
}
LNode* _insert(LNode* h)
{
char ch; LNode * p1, * p2 = h->next, * p3 = NULL; int locate, i=1;
printf("Enter a letter to insert:");
getchar();
ch = getchar();
printf("%c was entered. Enter a number to locate(Eorror:0):",ch);
scanf_s("%d", &locate);
if (locate)//确认输入正确
{
p1=(struct LNode*)malloc(sizeof(struct LNode));
p1->data = ch;
p1->next = NULL;
if (locate == 1)//头结点
{
p1->next = p2;
h->next = p1;
return h;
}
else
{
while (p2->next && i < locate)//遍历
{
i++;
p3 = p2;
p2 = p2->next;
}
if (i == locate)//中间结点
{
p3->next = p1;
p1->next = p2;
return h;
}
else if (i == locate - 1)//尾结点
{
p2->next = p1;
return h;
}
}
}
return NULL;
}
LNode* _change(LNode* h)
{
char change, ch; LNode *p2 = h->next;
printf("Enter a letter to change:");
getchar();
ch = getchar();
while(p2->next != NULL && p2->data != ch)
{
p2 = p2->next;
}
if (p2->data == ch)
{
printf("%c was found. Enter a number to change it(Eorror:0):", ch);
getchar();
p2->data = getchar();
return h;
}
else return NULL;
}
LNode* head; int get, option; char ch;
char a[N] = { 'A','B','C','D','E','F','G','H' }; //element
head = creatlist(a);//创建
if (head) outlist(head);//输出
printf("Find? 1/0:");//查询
scanf_s("%d", &option);
if (option)
{
printf("Enter a letter to find:");
getchar();
ch = getchar();
get = _find(head, ch);
if (get == 0) printf("\nNot found!\n");
else printf("The sequence number is : %d\n", get);
}
printf("Delete? 1/0:");//删除
scanf_s("%d", &option);
if (option)
{
head = _delete(head);
if (head) outlist(head);
else printf("\nDeleted failed");
}
printf("Insert? 1/0:");//插入
scanf_s("%d", &option);
if (option)
{
head = _insert(head);
if (head) outlist(head);
else printf("\nInsert failed");
}
printf("Change? 1/0:");//改变
scanf_s("%d", &option);
if (option)
{
head = _change(head);
if (head) outlist(head);
else printf("\nChange failed\n");
}
free(head);
system("pause");
结果:
编译器:Vs 2019.
1.关于编译器
可上官网上免费下载,我自己的是2017更新到2019的。
2.关于运行代码
有必字的是必须要有的代码函数,增删改查功能函数均可独立运行。
3.之前弄错了,缺的是更改函数现已更新,代码篇完结了啦~
不要着急讲解篇将于之后更新。
有不懂或者异议的地方可以评论留言。
感谢支持,看完记得点赞收藏哟~