c链表结点的删除和添加
2015-04-24 13:02
239 查看
#include<stdio.h> #include<stdlib.h> typedef char datetype;/*定义新的数据类型名*/ typedef struct node { datetype date; struct node *next; }listnode; typedef listnode *linklist; int delete(linklist h,int num)/*删除结点*/ { linklist p=h; listnode *q=NULL; int i=num; int n=1; while(n<i)/*寻找删除位置*/ { q=p;/*该结点和前后两个结点*/ p=p->next; n++; } if(p==NULL)/*该结点不存在*/ printf("No Found Node!"); else { q->next=p->next; free(p); } } void output(linklist head)/*链表遍历*/ { linklist p=head; while(p!=NULL) { printf("%c",p->date); p=p->next; } } int rear_creat(linklist head,int index0,int m)/*插入新结点,链表头,结点位置,结点date*/ { linklist k,g; int i=index0,n; k=head; n=1; while(n<i)/*寻找结点位置*/ { n++; k=k->next; } g=(listnode *)malloc(sizeof(listnode)); g->next=k->next; g->date=m; k->next=g; } int main() { char ch; int index,index0; char m; linklist head=NULL; listnode *p,*r; ch=getchar(); while(ch!='\n') { p=(listnode *)malloc(sizeof(listnode)); p->date=ch; if(head==NULL) head=p; else r->next=p; r=p; ch=getchar(); } output(head); printf("\ndelete a node:\n"); scanf("%d",&index); if(index==1) head=head->next; else delete(head,index); output(head); printf("\ncreat a node:\n"); scanf("%d%c",&index0,&m); rear_creat(head,index0,m); output(head); return 0; }
#include<stdio.h>/*尾插法*/ #include<stdlib.h> typedef char datetype; typedef struct node { datetype date; struct node *next; }listnode; typedef listnode *linklist; linklist r=NULL,head=NULL; void rear_creat(datetype ch) { linklist p=NULL; p=(listnode *)malloc(sizeof(listnode)); p->date=ch; if(head==NULL) { head=p; } else { r->next=p; } r=p; } void output(linklist head) { while(head!=NULL) { printf("%c ",head->date); head=head->next; } } void insert_rear(int index,int num) { listnode *p=NULL,*u; u=head; int n=1; p=(listnode *)malloc(sizeof(listnode)); p->date=index; while(num>n) { u=u->next; n++; } p->next=u->next; u->next=p; } void delete(int num) { int n=1; linklist p=head,q; while(num>n) { n++; q=p; p=p->next; } q->next=p->next; } int main() { datetype index,ch; int num; ch=getchar(); while(ch!='\n') { rear_creat(ch); ch=getchar(); } output(head); scanf("%c%d",&index,&num); insert_rear(index,num); output(head); scanf("%d",&num); delete(num); output(head); }
相关文章推荐
- 头结点链表的添加结点,删除结点,链表逆序,删除指定数据等操作的实现
- Java双向链表的创建结点,清空链表,添加结点,删除结点
- 双向链表添加节点和删除结点
- 剑指Offer学习总结-链表添加尾结点-删除结点
- 链表的添加、查找删除、反向打印c++代码
- 《剑指offer》面试题57 删除链表中重复的结点 C++ 实现 以及 错误总结 (指针问题)!!
- C语言--单链表创建、求长度、查找前驱结点、删除结点
- 删除链表中某结点
- LinkLists 删除链表中的一个给定指针的结点 @CareerCup
- 单链表创建,删除节点,添加节点,排序
- 【链表】C++链表操作大全:创建链表、遍历链表、删除链表结点、寻找环入口、链表加法、判断回文等
- 链表:创建、清空、插入、添加、删除节点
- 双向链表内结点的删除(4)
- 实现php删除链表中重复的结点
- C语言实现单链表节点的删除(带头结点)
- 【编程之美】读书笔记:从无头单链表中删除结点
- 删除链表结点
- 删除链表中重复的结点
- java建立双向链表,插入结点,删除节点
- 剑指offer面试题13:O(1)删除链表结点