链表-1 链表中数据的删除

http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1068

这是一个在链表中删除特定字符的代码,学习链表后第一次编写的一个代码。

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <math.h>
 6 using namespace std;
 7 struct sa
 8 {
 9     char ch;
10     struct sa *next;
11 };
12 int show(sa *t)//打印链表
13 {
14     while(t->next!=NULL)
15     {
16         cout<<t->ch<<endl;
17         t=t->next;
18     }
19     return  0;
20 }
21 int del(sa *t,char data)//清除指定的单字符
22 {
23     while(t->next!=NULL)
24     {
25         if(t->next->ch==data)
26         {
27             t->next=t->next->next;
28             break;
29         }
30         t=t->next;
31     }
32     return 0;
33 }
34 int main()
35 {
36     sa *p,*q;
37     char data;
38     int n;
39     while(cin>>n)
40     {
41         p=q=(sa *)malloc(sizeof(sa));//请求一个空间
42         for(int i=0;i<n;i++)
43         {
44             cin>>p->ch;//赋值
45             p->next=(sa *)malloc(sizeof(sa));//将不同的空间链到一起
46             p=p->next;
47         }
48         p->next=NULL;//空间链结束的标志
49         cin>>data;
50         del(q,data);
51         show(q);
52     }
53     return 0;
54 }
View Code

 

转载于:https://www.cnblogs.com/wang-ya-wei/p/5456172.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值