10.4单链表基本运算

来源:互联网 发布:中国联通软件开发待遇 编辑:程序博客网 时间:2024/06/01 19:08
//10.4单链表基本运算#include <stdio.h>#include <stdlib.h>typedef struct node{char data;                               //data为节点的数据信息struct node *next;                       //next为指向后继节点的指针}LNode;                                      //单链表节点类型LNode *CreateLinkList()                      //在表尾生成单链表{LNode *head, *p, *q;char x;head = (LNode *)malloc(sizeof(LNode));   //生成头节点head->next = NULL;p = head;q = p;                                   //指针q始终指向链尾节点printf("Input any char string:\n");scanf("%c", &x);                         //读入节点数据while (x != '\n'){p = (LNode *)malloc(sizeof(LNode));  //生成待插入节点的存储空间p->data = x;                         //将读入的数据赋给待插入节点*pp->next = NULL;                      //待插入节点*p作为链尾节点时其后继指针为空q->next = p;                         //在链尾插入*p节点q = p;                               //q继续指向新的链尾节点*pscanf("%c", &x);}return head;                             //返回单链表表头指针}int Length_LinkList(LNode *head)             //求单链表的长度{LNode *p = head;                         //p指向单链表头节点int i = 0;                               //i为节点计数器while (p->next != NULL){p = p->next;i++;}return i;}LNode *Get_LinkList(LNode *head, int i){                                //在单链表head中按序号查找第i个节点LNode *p = head;int j = 0;while (p != NULL && j < i)   //由第一个数据节点开始查找{p = p->next;j++;}return p;                    //找到则返回指向i节点的指针值,找不到则p为空,返回空值}LNode *Locate_LinkList(LNode *head, char x){                                //在单链表中查找结点值为x的节点LNode *p = head->next;while (p != NULL && p->data != x)     //由第一个数据节点开始查找{p = p->next;}return p;}void Insert_LinkList(LNode *head, int i, char x){ //在单链表head的第i个位置上插入值为x的元素LNode *p, *s;p = Get_LinkList(head, i - 1);       //查找第i-1个节点if (p == NULL){printf("Error!\n");              //第i-1个位置不存在而无法插入}else{s = (LNode *)malloc(sizeof(LNode));   //申请节点空间s->data = x;s->next = p->next;               //s->next 中存放的是原来p的后继节点p->next = s;                     //s成为p的后继节点}}void Del_LinkList(LNode *head, int i){//删除单链表head上的第i个数据节点LNode *p, *s;p = Get_LinkList(head, i - 1);      //查找第i-1个节点if (p == NULL){printf("第i-1个节点不存在!\n");                                //待删节点的前一个节点不存在,也就没有待删节点}else{if (p->next != NULL){printf("第i个节点不存在!\n");   //待删节点不存在}else{s = p->next;                //s指向第i个节点p->next = s->next;          //从链表中删除第i个节点 s->next 就是p->next->next;free(s);                    //系统回收第i个节点的存储空间}}}void print(LNode *h)                    //输出单链表{LNode *p;p = h->next;while (p != NULL){printf("%c", p->data);p = p->next;}printf("\n");}int main(){LNode *h, *p;int i;char x;h = CreateLinkList();                //生成一个单链表print(h);                            //输出单链表i = Length_LinkList(h);              //求单链表的长度printf("Length = %d\n", i);          //输出单链表的长度值printf("Input order and search to element:\n");   scanf("%d", &i);                     //输入要查找元素的序号p = Get_LinkList(h, i);              //按序号在顺序表中查找if (p != NULL)        {printf("Element is %c\n", p->data);    //找到则输出该元素的值}else{printf("Search fail!\n");              //未找到}printf("Input value of element and search to element:\n");getchar();scanf("%c", &x);                           //输入要查找元素的值p = Locate_LinkList(h,  x);                //按值在顺序表中查找if (p != NULL){printf("Element is %c\n", p->data);    //找到则输出该元素的值}else{printf("Search fail!\n");              //未找到}printf("Insert a element, Input site and value of element:\n");scanf("%d%c", &i, &x);                     //输入要插入元素的位置值i和元素值xInsert_LinkList(h, i, x);                  //在单链表中插入该元素print(h);                                  //输出单链表printf("Delete a element, Input site of element:\n");scanf("%d", &i);                           //输入要删除元素的位置值iDel_LinkList(h, i);                        //在单链表中删除该位置上的元素print(h);                                  //输出单链表return 0;}

原创粉丝点击