链表的删除
来源:互联网 发布:淘宝手工食品放哪类目 编辑:程序博客网 时间:2024/06/05 12:01
给定一个元素,删除链表中所有这个元素
#include<stdio.h> #include<stdlib.h> typedef struct _Node { int data; struct _Node *next; }Node; typedef struct _list{ Node *head; }List; void add(List *pList,int number) { Node *p=(Node*)malloc(sizeof(Node)); p->data=number; p->next=NULL; Node *last=pList->head; if(last) { while(last->next) { last=last->next; } last->next=p; } else pList->head=p; } void add2(List *pList,int number) { Node *p=(Node*)malloc(sizeof(Node)); p->data=number; p->next=pList->head; pList->head=p; } void add3(List *pList,int number) { Node *p=(Node*)malloc(sizeof(Node)); p->data=number; p->next=NULL; if(pList->head==NULL||p->data<pList->head->data) { p->next=pList->head; pList->head=p; } else { Node *q=pList->head; while(q->next&&p->data>q->next->data) { q=q->next; } if(q->next==NULL) { q->next=p; } else { p->next=q->next; q->next=p; } } }void deletel(List *pList,int m){if(pList->head->data==m){pList->head=pList->head->next;}Node *p=pList->head;while(p->next){if(p->next->data==m){Node *q;q=p->next;p->next=q->next;free(q);}else{p=p->next;}}}void showL(List *pList) { Node *p; for(p=pList->head;p;p=p->next) { if(p->next==NULL)printf("%d\n",p->data);else printf("%d ",p->data); } } int count(List *pList){int t=0; Node *p; for(p=pList->head;p;p=p->next) { t++; } return t;} int main() { List L; L.head=NULL; int n,temp,t; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%d",&temp); add(&L,temp); } int m; scanf("%d",&m); t=count(&L); printf("%d\n",t); showL(&L); deletel(&L,m); t=count(&L);printf("%d\n",t); showL(&L); }
阅读全文
0 0
- 链表的删除
- 链表的删除
- 链表的删除
- 链表的删除
- 链表的删除
- 数据结构 链表的删除
- 链表的删除操作
- 链表的插入、删除
- 链表的删除算法
- 链表结点的删除
- 删除链表的结点
- 链表结点的删除
- 链表的删除功能
- 链表的插入、删除
- 链表的单个节点的删除
- 删除单向链表的部分节点
- 链表的建立、插入和删除
- 单向链表的节点删除
- 轻量级的Canvas类库zrender使用笔记 :简单自定义图件开发
- 关于spark UI界面的解释,非常清晰
- Caused by: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.LoopTag
- 老外总结的高频PCB版的布线经验,值得一看!
- 一个经典的多线程同步问题
- 链表的删除
- xhr的jQuery方法
- 1.安装nginx
- redis源码分析(二)、sds动态字符串学习总结
- go语言--爬虫--对字符串的处理
- 为整个tomcat配置404页面
- 笔记:imooc-vue 音乐app
- dubbo实现SOA的管理
- php+nginx高并发大流量站点nginx优化