Chinaunix

标题: 链表 [打印本页]

作者: framily    时间: 2015-07-17 22:44
标题: 链表
  1. /*
  2. 编写一个链表类:
  3. a)支持构造函数和析构函数,析构时将该链表内的所有节点删除;
  4. b)编写一些成员函数支持:插入数据、打印数据和求节点数量;
  5. c)在main函数中定义多个链表对象,循环选择不同的链表对象插入或打印;
  6. d)在链表类中编写一个简单的菜单,来测试以上函数:
  7. 1、插入数据
  8. 2、打印数据
  9. 3、打印节点数量
  10. */

  11. #define _CRT_SECURE_NO_WARNINGS

  12. #include <iostream>
  13. #include <string>

  14. using namespace std;

  15. typedef int Data;

  16. struct Node
  17. {
  18.         Data data;
  19.         Node *next;
  20. };

  21. class List
  22. {
  23.         Node *head_;
  24.         char name_[20];

  25.         void SetName(const char *p)
  26.         {
  27.                 strcpy(name_, p);
  28.         }

  29.         void Print()
  30.         {
  31.                 system("cls");
  32.                 cout << name_ << endl;

  33.                 Node *p = head_;

  34.                 while (p)
  35.                 {
  36.                         cout << p->data << endl;
  37.                         p = p->next;
  38.                 }

  39.                 system("pause");
  40.         }

  41.         int GetCount()
  42.         {
  43.                 Node *p = head_;
  44.                 int i = 0;

  45.                 while (p)
  46.                 {
  47.                         p = p->next;
  48.                         i++;
  49.                 }

  50.                 return i;
  51.         }

  52.         void AddHead(Data data)
  53.         {
  54.                 Node *p = new Node;

  55.                 p->data = data;
  56.                 p->next = head_;
  57.                 head_ = p;
  58.         }

  59.         void AddTail(Data data)
  60.         {
  61.                 Node *p = new Node;

  62.                 p->data = data;
  63.                 p->next = NULL;

  64.                 if (!head_)
  65.                 {
  66.                         head_ = p;
  67.                         return;
  68.                 }

  69.                 Node *q = head_;

  70.                 while (q->next)
  71.                 {
  72.                         q = q->next;
  73.                 }

  74.                 q->next = p;
  75.         }

  76.         void RemoveAll()
  77.         {
  78.                 Node *p = head_, *q;

  79.                 while (p)
  80.                 {
  81.                         q = p;
  82.                         p = p->next;
  83.                         delete q;
  84.                 }

  85.                 head_ = NULL;
  86.         }

  87.         void AddData()
  88.         {
  89.                 Data data;

  90.                 system("cls");
  91.                 cout << "请输入一个整数:";
  92.                 cin >> data;
  93.                 AddTail(data);
  94.         }

  95. public:

  96.         int Menu()
  97.         {
  98.                 system("cls");
  99.                 cout << "1.插入数据" << endl;
  100.                 cout << "2.打印数据" << endl;
  101.                 cout << "3.打印节点数量" << endl;

  102.                 int i;

  103.                 cin >> i;

  104.                 switch (i)
  105.                 {
  106.                 case 1:
  107.                         AddData();
  108.                         break;
  109.                 case 2:
  110.                         Print();
  111.                         break;
  112.                 case 3:
  113.                         cout << "当前节点数量:" << GetCount() << endl;
  114.                         system("pause");
  115.                         break;
  116.                 default:
  117.                         break;
  118.                 }

  119.                 return i;
  120.         }       

  121.         List()
  122.         {
  123.                 head_ = NULL;
  124.         }

  125.         ~List()
  126.         {
  127.                 RemoveAll();
  128.         }
  129. };

  130. int main()
  131. {
  132.         List list_1, list_2;
  133.         int i = 0;

  134.         do {       
  135.                 system("cls");
  136.                 cout << "1.链表1" << endl;
  137.                 cout << "2.链表2" << endl;
  138.                 cout << "请选择:";
  139.                 cin >> i;

  140.                 switch (i)
  141.                 {
  142.                 case 1:
  143.                         list_1.Menu();
  144.                         break;
  145.                 case 2:
  146.                         list_2.Menu();
  147.                         break;
  148.                 default:
  149.                         break;
  150.                 }
  151.         } while (i);

  152.         return 0;
  153. }
复制代码

作者: framily    时间: 2015-07-18 17:32
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef int Data;

struct Node
{
        Data data;
        struct Node *next;
};

struct List
{
        struct Node *head;
        char name[20];       
};

void SetName(struct List *const this,const char *p)
{
        strcpy(this->name, p);
}

void Print(struct List *const this)
{
        system("cls");
        puts(this->name);

        struct Node *p = this->head;

        while (p)
        {
                printf("%d\n", p->data);
                p = p->next;
        }

        system("pause");
}

int GetCount(struct List *const this)
{
        struct Node *p = this->head;
        int i = 0;

        while (p)
        {
                p = p->next;
                i++;
        }

        return i;
}

void AddHead(struct List *const this, Data data)
{
        struct Node *p = (struct Node*)malloc(sizeof(struct Node));

        p->data = data;
        p->next = this->head;
        this->head = p;
}

void AddTail(struct List *const this, Data data)
{
        struct Node *p = (struct Node*)malloc(sizeof(struct Node));

        p->data = data;
        p->next = NULL;

        if (!this->head)
        {
                this->head = p;
                return;
        }

        struct Node *q = this->head;

        while (q->next)
        {
                q = q->next;
        }

        q->next = p;
}

void RemoveAll(struct List *const this)
{
        struct Node *p = this->head, *q;

        while (p)
        {
                q = p;
                p = p->next;
                free(q);
        }

        this->head = NULL;
}

void AddData(struct List *const this)
{
        Data data;

        system("cls");
        printf("请输入一个整数:");
        scanf("%d", &data);
        AddTail(this,data);
}

void Menu(struct List *const this)
{
        system("cls");
        printf("1.插入数据\n");
        printf("2.打印数据\n");
        printf("3.打印节点数量\n");

        int i;

        scanf("%d", &i);

        switch (i)
        {
        case 1:
                AddData(this);
                break;
        case 2:
                Print(this);
                break;
        case 3:
                system("cls");
                printf("当前结点数量:%d\n", GetCount(this));
                system("pause");
                break;
        default:
                break;
        }
}

List(struct List *const this)
{
        this->head = NULL;
}

DeList(struct List *const this)
{
        RemoveAll(this);
}

int main(int argc,char *argv[])
{
        struct List list_1, list_2;

        List(&list_1);
        SetName(&list_1, "list_1");
        List(&list_2);
        SetName(&list_2, "list_2");

        int i = 0;

        do {
                system("cls");
                printf("1.->链表一2.->链表二\n");
                printf("请选择:");
                fflush(stdin);
                scanf("%d",&i);

                switch (i)
                {
                case 1:
                        Menu(&list_1);
                        break;
                case 2:
                        Menu(&list_2);
                        break;
                default:
                        break;
                }
        } while (i);

        DeList(&list_1);
        DeList(&list_2);

        return 0;
}




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2