数据结构---单链表(建立,节点删除,节点插入)
来源:互联网 发布:asp.net 字符串转json 编辑:程序博客网 时间:2024/05/17 06:37
//UNIX环境2011-02-03
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct student
- {
- int num;
- char name[16];
- struct student *next;
- }node;
- node *creat()//创建链表
- {
- int x;
- int cycle = 1;
- char stu_name[16];
- node *head,*p,*s;
- head =(node*)malloc(sizeof(node));
- p = head;
- while(cycle)
- {
- printf("please input the student number:");
- scanf("%d",&x);
- if(x != 0)
- {
- printf("please input the student name:");
- scanf("%s",stu_name);
- // printf("STU_NAME:%s/n", stu_name);
- s = (node *)malloc(sizeof(node));
- if(NULL == s)
- {
- exit(1);
- }
- s->num = x;
- memcpy(s->name, stu_name, 16);
- p->next = s;
- p = s;
- }
- else
- cycle = 0;
- }
- head = head->next;
- p->next = NULL;
- return head;
- }
- int length(node *head)//链表测长
- {
- int n = 0;
- node *p;
- p = head;
- while(p != NULL)
- {
- n++;
- p = p->next;
- }
- return (n);
- }
- node *insert(node *head, int num, int length)//链表插入,NUM表示在第几个位置插入
- {
- int n=num;
- int i = 1;
- node *p0,*p1,*p2;
- p1 = head;
- p0 = (node *)malloc(sizeof(node));
- printf("please input the student number:");
- scanf("%d", &p0->num);
- printf("please input the student name:");
- scanf("%s", &p0->name);
- if(n == 1)//插入表头
- {
- p0->next = p1;
- head = p0;
- return (head);
- }
- while(i < n-1)//找到要插入的前置节点
- {
- p1 = p1->next;
- i++;
- }
- p2 = p1->next;
- p1->next = p0;
- p0->next = p2;
- if(n == length+1)//插入表尾
- {
- p1->next = p0;
- p0->next = NULL;
- }
- return (head);
- }
- node *delete(node *head, int location, int length)//删除链表节点
- {
- int n = location;
- int i = 1;
- node *p1,*p2;
- p1 = head;
- if(n == 1)
- {
- head = p1->next;
- free(p1);
- return (head);
- }
- while(i < n-1)//找到要删除的节点的前置节点
- {
- p1 = p1->next;
- i++;
- }
- if(n < length)
- {
- p2 = p1->next;
- p1->next = p2->next;
- free(p2);
- }
- if(n == length)
- {
- p2 = p1->next;
- p1->next = NULL;
- free(p2);
- }
- return (head);
- }
- void print(node *head)
- {
- while(head != NULL)
- {
- printf("students:%d/n", head->num);
- printf("student name: %s/n", head->name);
- head = head->next;
- }
- }
- node *invert(node *head)//链表逆置
- {
- node *p1,*p2,*p3;
- p1 = head;
- p2 = p1->next;
- while(p2)
- {
- p3 = p2->next;
- p2->next = p1;
- p1=p2;
- p2=p3;
- }
- head->next = NULL;
- head = p1;
- return (head);
- }
- int main(int argc, char **argv)
- {
- int len,insert_num,del_num;
- node *stu_node;
- stu_node = creat();
- print(stu_node);
- len = length(stu_node);
- printf("there are %d node/n", len);
- printf("what dou you want to do?/n[a]打印链表 [b]逆置链表 [c]删除节点 [d]插入节点: ");
- char ch,c;
- c = getchar();//用于清除缓冲区中的/n字符
- scanf("%c",&ch);
- switch (ch)
- {
- case 'a': print(stu_node);
- break;
- case 'b': stu_node = invert(stu_node);
- print(stu_node);
- break;
- case 'c':
- printf("which node dou you want to detele?:");
- scanf("%d", &del_num);
- stu_node = delete(stu_node, del_num, len);
- print(stu_node);
- break;
- case 'd':
- printf("which location dou you want to insert:");
- scanf("%d",&insert_num);
- stu_node = insert(stu_node , insert_num, len);
- print(stu_node);
- break;
- }
- return 0;
- }
0 0
- 数据结构---单链表(建立,节点删除,节点插入)
- 数据结构---单链表(建立,节点删除,节点插入)
- 单链表的建立、测长、打印、删除节点、插入节点
- 单链表的插入节点与删除节点
- 数据结构——2 单链表插入和删除节点
- 链表:检测是否有环,是否相交,删除节点,插入节点,打印节点,建立节点,节点排序,节点逆序,
- 单链表的建立、测长、打印、删除节点、插入节点、排序、逆转
- 单链表的建立、测长、打印、删除节点、插入节点、排序、逆置操作。
- 数据结构 -- 单链表之删除节点
- 数据结构-单链表节点的删除
- 数据结构-单链表节点的删除
- 双链表删除/插入节点
- 双链表删除/插入节点
- java建立双向链表,插入结点,删除节点
- 双向链表的建立、删除/插入节点
- 数据结构——单链表的创建、删除、遍历以及节点的插入、删除等操作
- 单链表插入节点、删除节点、倒置、去重
- 单链表的插入与删除节点
- A and B and Interesting Substrings cf294
- mmap那些事之android property实现之二
- 2015/3/19 文献阅读笔记
- layoutSubviews总结
- canvas.drawRoundRect方法,绘制圆角矩形
- 数据结构---单链表(建立,节点删除,节点插入)
- 【Error】SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
- android 中 获取 文字宽度
- postgres 数据库常用sql语句
- 临沂市蔬菜研究所科技部生产研发
- Cloneable接口使用
- kmap的实现分析与实验
- Git常用命令
- 内核态与用户态