链表基本操作(建立、修改,插入、删除、打印)
2013-08-17 16:19
691 查看
#include <stdio.h> #include <stdlib.h> struct list { int data; struct list *next; }; //头插法建立链表 struct list *headcreate() { struct list *head, *p; int N,i; head=NULL; printf("输入要建立的链表结点个数N="); scanf("%d",&N); printf("输入%d个数:",N); for(i=0;i<N;i++) { p=(struct list *)malloc(sizeof(struct list)); scanf("%d",&p->data); p->next=head; head=p; } return head; } //尾插法建立链表 struct list *tailcreate() { struct list *head, *p, *q; int N,i; head=NULL; printf("输入要建立的链表结点个数N="); scanf("%d",&N); printf("输入%d个数:",N); for(i=0;i<N;i++) { p=(struct list *)malloc(sizeof(struct list)); scanf("%d",&p->data); p->next=NULL; if(head==NULL) head=p; else q->next=p; q=p; } return head; } //打印链表 void print(struct list *head) { struct list *p; p=head; while(p) { printf("%d ",p->data); p=p->next; } printf("\n"); } //修改链表结点 void Modify(struct list *head, int X) { struct list *p; p=head; printf("将所有结点为5的修改为%d\n",X); while(p) { if(p->data==5) p->data=X; p=p->next; } } //插入节点 void Insert(struct list *head, int Z) { struct list *p,*r; p=head; printf("将所有结点为10的后面插入%d\n",Z); while(p) { if(p->data==10) { r=(struct list *)malloc(sizeof(struct list)); r->data=Z; r->next=p->next; p->next=r; } p=p->next; } } //删除结点 struct list *Delete(struct list *head, int Y) { struct list *p, *q; p=head; while(p) { if(p->data==Y) { if(p==head) head=p->next; else q->next=p->next; } else q=p; p=p->next; } return head; } int main() { struct list *head; head=headcreate(); printf("头插法建立链表:"); print(head); head=tailcreate(); printf("尾插法建立链表:"); print(head); Modify(head,10); printf("修改结点后的链表:"); print(head); Insert(head,20); printf("插入结点后的链表:"); print(head); head=Delete(head,10); printf("删除结点后的链表:"); print(head); return 0; }
相关文章推荐
- 链表的基本操作,建立,测长,删除,打印,插入
- C-链表的一些基本操作【创建-删除-打印-插入】
- 单链表的基本操作:建立,求长度,输出,排序,插入,删除,逆置
- 编程实现顺序表的以下基本操作:建立顺序表,修改顺序表,插入顺序表,删除顺序表。
- c语言链表基本操作(带有创建链表 删除 打印 插入)
- 链表建立、删除、插入基本操作
- 链表的基本操作(C语言版):建立,插入,删除,查找,输出
- c++之链表篇1:单向链表的创建,打印,删除,插入,销毁等基本操作
- 线性表的链式存储格式基本操作:创建链表、插入、删除、查找、求表长、打印链表
- 程序员面试宝典_链表基本操作,建立,求长,删除和插入特定值的结点
- java语言编写链表的基本操作(链表的创建,插入,删除,打印,排序)
- 单链表的建立,查找,插入,删除,测长,打印,逆置操作实现
- 【程序员面试宝典】数据结构基础二单链表循环链表的操作 建立|插入删除|打印|合并
- C++链表的创建、插入、删除、查找、合并、排序、修改等操作的实现
- c++实现单链表创建,删除,遍历,插入,修改操作
- 双链表的建立、测长、打印、删除、插入
- 单链表的建立、插入、删除等简单操作
- ASP.NET对数据库的基本操作——插入,删除,修改(上)
- Qt5.9Creator读写Mysql5.7.17数据库详细例子(数据库基本操作:创建表,查询,插入,删除,修改)
- 单链表 建立, 查找, 删除, 插入 操作