链表-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

 

posted @ 2016-05-03 18:47  Wally的博客  阅读(424)  评论(0编辑  收藏  举报