博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
充电第二天
阅读量:6670 次
发布时间:2019-06-25

本文共 4025 字,大约阅读时间需要 13 分钟。

链表

  1. 从键盘接收10个整数,构成从小到大的有序链表
1:  #include 
2:  using namespace std;
3:   
4:  typedef struct node
5:  {
6:      int data;
7:      struct node *next;
8:  }*Link;
9:   
10:  void insert(Link head, Link p)
11:  {
12:      Link q = head;
13:   
14:      while(q->next!=NULL && q->next->data
data)
15:          q = q->next;
16:      p->next = q->next;
17:      q->next = p;
18:  }
19:  int main()
20:  {
21:      int data;
22:
23:      Link head = (Link)malloc(sizeof(struct node));
24:      if (head = NULL)
25:      {
26:          cout << "memory allocation error\n";
27:          exit(0);
28:      }
29:      head->next = NULL;
30:
31:      for (int i=1; i<11; i++)
32:      {
33:          cout << "please input the " << i << " data:" << endl;
34:          cin >> data;
35:          Link p = (Link)malloc(sizeof(struct node));
36:          if (p = NULL)
37:          {
38:              cout << "memory allocation error\n";
39:              exit(0);
40:          }
41:          p->data = data;
42:          insert(head, p);
43:      }
44:      cout << endl;
45:      for (int i=1; i<11; i++)
46:      {
47:          cout << head->next->data << endl;
48:          head = head->next;
49:      }
50:   
51:      cout << system("pause");
52:      return 0;
53:  }
2.链表的删除
题目:有15个人围成一圈,顺序从1到15编号。从第一个人开始报数,凡报到n的人退出圈子。从键盘输入n的值,输出最后留在圈子里的人的编号
1:  #define MAXN 15
2:  #include 
3:  using namespace std;
4:   
5:  typedef struct node
6:  {
7:      int data;
8:      struct node *next;
9:  }*Link;
10:   
11:  Link  createlist()
12:  {
13:      Link head = (Link)malloc(sizeof(struct node));
14:      if (!head)
15:      {
16:          cout << "memory allocation error!" << endl;
17:          exit(0);
18:      }
19:      head->data = 1;
20:      head->next = head;   //头结点指向自身,形成环形
21:      for (int i=MAXN; i>1; i--)
22:      {
23:          Link p = (Link)malloc(sizeof(struct node));
24:          if (!p)
25:          {
26:              cout << "memory allocation error!" << endl;
27:              exit(0);
28:          }
29:          p->data = i;   //头插,形成环形链表
30:          p->next = head->next;
31:          head->next = p;
32:      }
33:      return head;
34:  }
35:  int main()
36:  {
37:      Link p = createlist();
38:      cout << "please input n: " ;
39:   
40:      int n, k=MAXN;
41:      cin >> n;
42:      int c = 1;
43:      Link q;
44:      while(k>1)
45:      {
46:          if (n-1==c)
47:          {
48:              q = p->next;
49:              p->next = q->next;
50:              free(q);
51:              c = 0;
52:              k--;
53:          }
54:          else
55:          {
56:              c++;
57:              p = p->next;
58:          }
59:      }
60:      cout << "The last number is: " << p->data << endl;
61:   
62:  cout << system("pause");
63:  return 0;
64:  }
/*  链表的删除*   题目:递归删除链表中的重复节点,返回链表头指针*/#include 
#define MAXN 10using namespace std;typedef struct node{ int data; struct node *next;}*Link;Link createlist(){ Link head = (Link)malloc(sizeof(struct node)); if (!head) { cout << "memory allocation error!" << endl; exit(0); } head->data = 2; Link q = head; for (int i=0; i
data = i; q->next = p; q = p; } q->next = NULL; //尾节点切记不要出现野指针 return head;}Link delrep(Link head){ Link p = head, q; while(p->next != NULL) { while(p->next != NULL && head->data != p->next->data) p = p->next; if (p->next == NULL) { head = head->next; delrep(head); } else { q = p->next; p->next = q->next; free(q); } } return head;}void printlist(Link head){ Link p = head; do { cout << p->data; p = p->next; } while (p->next!=NULL); cout << p->data << endl;}int main(){ Link head = createlist(); delrep(head); printlist(head); cout << system("pause"); return 0;}

转载于:https://my.oschina.net/target/blog/208487

你可能感兴趣的文章
零售ERP系统方案选型―IT只是一个工具
查看>>
GraphQL提供数据接口新思路之数据聚合解决方案
查看>>
CentOS7 安装Firefly及测试
查看>>
术有专攻 | 如何在公私混用的设备上保障企业信息安全
查看>>
安全自动化在于信任,而非技术
查看>>
揭密巴西Banrisul银行网站遭遇5小时劫持的原因
查看>>
安装Linux流量监控工具 - iftop
查看>>
如何令移动下载飞起来 结合LTE与Wi-Fi
查看>>
亚信安全与成都市政府达成战略合作
查看>>
如果你喜欢上了一个程序员小伙
查看>>
大数据时代:统计学是数据分析的灵魂
查看>>
为什么我的物联网创业失败?看看这五个原因
查看>>
Intel融合Altera做大做强FPGA
查看>>
英特尔X520万兆网卡构建高效云中心
查看>>
AI“入侵”数据中心
查看>>
家用医疗仪器市场规模首破千亿大关 行业并购势头强劲
查看>>
SSH::Batch,在公有云中使用 ssh 工具箱
查看>>
震惊:遭劫8000万美元的孟加拉银行竟然没装防火墙
查看>>
“外围”消亡 企业安全防护需要新形态
查看>>
关于Java应用相关不同产品的架构
查看>>