码迷,mamicode.com
首页 > 其他好文 > 详细

类的继承——Node类链表

时间:2016-04-21 21:52:11      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

Node.h

 1 #ifndef NODE_H_
 2 #define NODE_H_
 3 class Node
 4 {
 5 private:
 6     int num;
 7     char* name;
 8 public:
 9     Node *next;
10     Node();
11     Node(const int, const char*);
12     int getNum();
13     char* getName();
14 };
15 
16 class List : public Node
17 {
18 private:
19     Node* head;
20     Node* temp1;
21     Node* temp2;
22     Node* tail;
23 public:
24     List();
25     ~List();
26     void add(Node &);
27     void remove(Node &);
28     Node* find(Node &);
29     void output();
30 };
31 
32 #endif

Node.cpp

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<iostream>
  3 #include"Node.h"
  4 using namespace std;
  5 Node::Node()
  6 {
  7     num = 0;
  8     name = new char[10];
  9     if (!name)
 10     {
 11         cout << "创建失败!内存不足!" << endl;
 12         exit(1);
 13     }
 14     strcpy(name, "No name");
 15     next = NULL;
 16 }
 17 Node::Node(const int a ,const char* b)
 18 {
 19     num = a;
 20     name = new char[strlen(b) + 1];
 21     if (!name)
 22     {
 23         cout << "创建失败!内存不足!" << endl;
 24         exit(1);
 25     }
 26     strcpy(name,b);
 27     next = NULL;
 28 }
 29 int Node::getNum()
 30 {
 31     return num;
 32 }
 33 char* Node::getName()
 34 {
 35     return name;
 36 }
 37 
 38 
 39 List::List()
 40 {
 41     head=NULL;
 42     temp1 = NULL;
 43     temp2 = NULL;
 44     tail = NULL;
 45 }
 46 
 47 List::~List()
 48 {
 49 }
 50 
 51 void List::add(Node & t)
 52 {
 53     if (head == NULL)
 54     {
 55         head = new Node();
 56         if (!head)
 57         {
 58             cout << "创建失败!内存不足!" << endl;
 59             return;
 60         }
 61         *head = t;
 62         temp1 = head;
 63         tail = head;
 64     }
 65     else {
 66         temp2 = new Node();
 67         if (!temp2)
 68         {
 69             cout << "创建失败!内存不足!" << endl;
 70             return;
 71         }
 72         *temp2 = t;
 73         temp1->next = temp2;
 74         tail = temp2;
 75         temp1 = temp2;
 76     }
 77 }
 78 
 79 void List::remove(Node &t)
 80 {
 81     temp1 = head;
 82     for (; temp1->getNum() != t.getNum();)
 83     {
 84         temp2 = temp1;
 85         temp1 = temp1->next;
 86         if (temp1->next == NULL)
 87         {
 88             cout << "删除失败!未找到节点!" << endl;
 89             return;
 90         }
 91     }
 92     if (strcmp(temp1->getName(), t.getName()) == 0)
 93     {
 94         temp2->next = temp1->next;
 95         delete temp1;
 96     }
 97     else
 98         cout << "删除失败!学号和姓名不匹配!" << endl;
 99 }
100 
101 Node* List::find(Node &t)
102 {
103     temp1 = head;
104     for (; temp1->getNum() != t.getNum();)
105     {
106         temp1 = temp1->next;
107         if (temp1->next == NULL)
108         {
109             cout << "查找失败!未找到节点!" << endl;
110             return NULL;
111         }
112     }
113     if (strcmp(temp1->getName(), t.getName()) == 0)
114     {
115         return temp1;
116     }
117     else
118         cout << "查找失败!学号和姓名不匹配!" << endl;
119 }
120 
121 
122 void List::output()
123 {
124     temp1 = head;
125     cout << "Num:" << temp1->getNum() << "  Name:" << temp1->getName() << endl;
126     while (1)
127     {
128         temp1 = temp1->next;
129         cout << "Num:" << temp1->getNum() << "  Name:" << temp1->getName() << endl;
130         if (temp1->next == NULL)
131             break;
132     }
133 }

mian.cpp

/*
exit(1)表示异常退出.这个1是返回给操作系统的。
exit(x)(x不为0)都表示异常退出;
exit(0)表示正常退出。
*/
#include<iostream>
#include"Node.h"
using namespace std;
int main()
{
    List a;
    Node stu[5] = { Node(101, "Merry") ,Node (102, "Jack") ,Node(103,"Tom"),Node(104,"John"),Node(105,"Mike")};
    for (int i = 0; i < 5;++i)
        a.add(stu[i]);
    a.output();

    cout << "删除节点" << endl;
    a.remove(Node(102, "Jack"));
    a.output();

    system("pause");
    return 0;
}

 

类的继承——Node类链表

标签:

原文地址:http://www.cnblogs.com/-yixi/p/5418763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!