简单的链表代码

一个师弟的简单的链表代码(经过修改)。嗯,应该是学习链表的实例代码吧。

  1. // testlist.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <malloc.h>
  7. using namespace std;
  8. /*一、    插入链表。一个链表A={a1,a2,..,an}结构如下:
  9. 节点定义为:
  10. struct node{
  11. int num;        //关键字
  12. struct node *next;
  13. };
  14. 编程。要求程序实现如下功能:
  15. (1)    插入链表。主程序输入关键码值,调用插入函数insert(),将节点插入到A中;
  16. (2)    遍历节点计数。调用节点计数函数counter(),遍历求A中的节点个数。调用counter()函数之后,其返回值(整数)输出到屏幕;
  17. (3)    递归节点计数。调用递归函数Rcounter(),递归求A中的节点个数,调用Rcounter()函数之后,其返回值(整数)输出到屏幕;
  18. 我的case 3 为什么总输出0啊???
  19. 其他都能运行,就递归(Rcounter)不行?为什么
  20. */
  21. struct node{
  22.     int num;
  23.     struct node *next;
  24. };
  25. int counter(struct node *);
  26. int Rcounter(struct node *,int*);
  27. struct node *insert(struct node *,struct node *);
  28. int main()
  29. {
  30.     struct node *head=NULL,*node1;
  31.     int k;
  32.     cout<<"输入要执行的功能:1 插入节点 2 遍历计数 3 递归计数 4 退出程序"<<endl;
  33.     cin>>k;
  34.     node1=(struct node *)malloc(sizeof(struct node));
  35.     while(k!=4)
  36.     {
  37.         switch(k)
  38.         {
  39.         case 1:
  40.             cout<<"输入关键码值:"<<endl;
  41.             cin>>node1->num;
  42.             head=insert(head,node1);
  43.             break;
  44.         case 2:
  45.             int i;
  46.             i=counter(head);
  47.             cout<<"共有"<<i<<"个节点"<<endl;
  48.             break;
  49.         case 3:
  50.             struct node *s;
  51.             node* pNode = NULL;
  52.             int t=0;
  53.             int k=0;
  54.             //s=head; 由于下面是动态内存分配的所以你这里赋值是没有什么意义的           
  55.             head = (struct node *)malloc(sizeof(struct node));
  56.             //判断是否动态内存分配成功
  57.             if (NULL == head)
  58.             {
  59.                 cout << "get memory failed!";
  60.                 break;
  61.             }
  62.             //初始化
  63.             head->next = NULL;
  64.             //对于链表的头需要单独的处理
  65.             cout<<"是否输入关键字:1是 0否"<<endl;
  66.             cin>>k;
  67.             if (0 == k)
  68.             {
  69.                 cout << "递归求得的节点数是 0" << endl;
  70.                 break;
  71.             }
  72.             else
  73.             {
  74.                 head->num = k;
  75.             }
  76.                 
  77.             //动态内存分配了之后再赋值
  78.             s = head; //这样 head 里面保存了头了
  79.             
  80.             //开始建立链表
  81.             while(k)
  82.             {    
  83.                 pNode = (struct node *)malloc(sizeof(struct node));
  84.                 //判断是否动态内存分配成功
  85.                 if (NULL == pNode)
  86.                 {
  87.                     cout << "get memory failed!";
  88.                     break;
  89.                 }
  90.                 //初始化
  91.                 pNode->next = NULL;
  92.                 
  93.                 cout<<"输入num:"<<endl;
  94.                 cin>> pNode->num;
  95.                 cout<<"是否继续输入:1是 0否"<<endl;
  96.                 cin>>k;
  97.                 if(k)
  98.                 {    
  99.                     //下面的语句是建立链表的关键代码
  100.                     //s=s->next;
  101.                     s->next = pNode;
  102.                     s = pNode;
  103.                 }
  104.             }
  105.             //建立链表结束
  106.             s=head;
  107.             t = 0;
  108.             //Rcounter 函数是需要改变t的,所以参数应该传递指针,当然你的做法也可以,但是需要修改一个很微小的地方
  109.             cout<<"递归求得的节点数是:"<< Rcounter(s, &t) << endl;
  110.         }
  111.         cout<<"输入要执行的功能:1 插入节点 2 遍历计数 3 递归计数 4 退出程序"<<endl;
  112.         cin>>k;
  113.     }
  114.     if(k==4)
  115.     {
  116.         cout<<"谢谢!"<<endl;
  117.     }
  118.     return 0;
  119. }
  120. struct node *insert(struct node *head,struct node *node1)
  121. {
  122.     struct node *p0,*p1,*p2;
  123.     p1=head;
  124.     p0=node1;
  125.     if(head==NULL)
  126.     {
  127.         head=p0;
  128.         p0->next=NULL;
  129.     }
  130.     else
  131.     {
  132.         while(p0->num > p1->num)
  133.         {
  134.             p2=p1;
  135.             p1=p1->next;
  136.         }
  137.         if(p0->num<=p1->num)
  138.         {
  139.             if(head==p1)head=p0;
  140.             else p2->next=p0;
  141.             p0->next=p1;
  142.         }
  143.         else
  144.         {
  145.             p1->next=p0;
  146.             p0->next=NULL;
  147.         }
  148.     }
  149.     return (head);
  150. }
  151. int counter(struct node *head)
  152. {
  153.     int k=0,i=0;
  154.     cout<<"是否输入节点关键字:1是 0否"<<endl;
  155.     cin>>k;
  156.     while(k)
  157.     {
  158.         head=(struct node *)malloc(sizeof(struct node));
  159.         cout<<"输入num:"<<endl;
  160.         cin>>head->num;
  161.         cout<<"是否继续输入节点关键字:1是 0否"<<endl;
  162.         cin>>k;
  163.         if(k)
  164.         {    
  165.             head=head->next;
  166.             i++;
  167.         }
  168.     }
  169.     if(head==NULL)
  170.     {
  171.         cout<<"链表为空"<<endl;
  172.         return 0;
  173.     }
  174.     return i+1;
  175. }
  176. int Rcounter(struct node *s,int* pt)
  177. {
  178.     if (NULL == s)
  179.     {
  180.         return *pt;
  181.     }
  182.     else
  183.     {
  184.         (*pt)++;
  185.         return Rcounter(s->next, pt);
  186.     }
  187. }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表是一种常见的数据结构,它由节点组成,每个节点包含了一个数据元素和一个指向下一个节点的指针。链表可以用于实现队列、栈等数据结构。 以下是一个简单链表的定义和基本操作的示例代码。 ```c++ // 链表节点的定义 struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; // 在链表末尾添加一个节点 void appendListNode(ListNode*& head, int val) { ListNode* newNode = new ListNode(val); if (head == nullptr) { head = newNode; } else { ListNode* cur = head; while (cur->next != nullptr) { cur = cur->next; } cur->next = newNode; } } // 删除链表中的一个节点 void deleteListNode(ListNode*& head, int val) { if (head == nullptr) { return; } if (head->val == val) { ListNode* toDelete = head; head = head->next; delete toDelete; } else { ListNode* cur = head; while (cur->next != nullptr && cur->next->val != val) { cur = cur->next; } if (cur->next != nullptr) { ListNode* toDelete = cur->next; cur->next = cur->next->next; delete toDelete; } } } // 打印链表的值 void printList(ListNode* head) { ListNode* cur = head; while (cur != nullptr) { std::cout << cur->val << " "; cur = cur->next; } std::cout << std::endl; } // 清空链表 void clearList(ListNode*& head) { while (head != nullptr) { ListNode* toDelete = head; head = head->next; delete toDelete; } } int main() { ListNode* head = nullptr; appendListNode(head, 1); appendListNode(head, 2); appendListNode(head, 3); printList(head); // 输出 1 2 3 deleteListNode(head, 2); printList(head); // 输出 1 3 clearList(head); return 0; } ``` 这段代码定义了一个链表节点结构体 `ListNode`,包含一个整型数据成员 `val` 和一个指向下一个节点的指针成员 `next`。然后定义了一些基本操作函数,包括在链表末尾添加一个节点、删除一个节点、打印链表的值和清空链表。最后在 `main` 函数中演示了这些操作的使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值