C++ 递归实现反向打印一个链表(引入wrapper function的概念)


打开NewContactList工程文件, 相关的修改的程序文件如下(未修改的没有post 上去)。

ContactList.h文件如下:

/*
 *ContactList.h
 *
 *   created on Jul 6, 2014
 *      Author: ***
 *
 *
 */

 #ifndef CONTACT_LIST_H
 #define CONTACT_LIST_H


 #include "Contact.h" // the first thing to do is to include the node header file

 class ContactList {
     public:
        ContactList(); // prototype for constructor
        void AddToHead(const std::string&);//reference, 避免复制, 更快, const, 所以不让修改
         void printList();
        void insert(const std::string& inputName);
        void deleteNode(const std::string&);
        //void reversePrintWorker(Contact*); //introduce a new concept , , 为了便于引入这个概念, 在这里注释掉, 显示出来
        void reversePrintList();//a wrapper function, 没有吃参数, 在实现的时候调用reversePrintWorker
     private:
        void reversePrintWorker(Contact*);
        Contact* head;
        int length;
};
   #endif /*end of CONTACT_LIST_H*/

上面的程序中, 介绍了wrapper function的概念。

一个wrapper function 就是public available to the client who uses this class, while they don't have to know the head pointer。

将真正做工作的函数(reversePrintWorker)放在private section。 一个wrapper function(在这里是reversePrintList) 调用这个函数。

 

Contactlist.cpp如下:


 

// an implementation of ContactList.h

#include "ContactList.h"
using namespace std;

ContactList::ContactList():head(0), length(0) {

}

void ContactList::AddToHead(const string& name) {
   Contact* newOne = new Contact(name);
   if(head == 0) {
      head = newOne;
   }
   else {
      newOne -> next = head;
      head = newOne;
   }
   length++;
}

void ContactList::printList() {
   Contact* tp;

   tp = head;

   while(tp != NULL) {
      cout << tp -> name << endl;

      tp = tp -> next;
   }
}

void ContactList::insert(const string& inputName) {
   Contact* newNode = new Contact(inputName);//create a node in the heap
   //case1 - empty list
   if(head == 0) {
      head = newNode;
   }
   else {

   Contact* curr = head;
   Contact* trail = 0;
   //Traverse the list to find insert location
   while(curr != 0) {
      if(curr -> name >= newNode ->name) {
         break;
      }
      else {
         trail = curr;
         curr = curr -> next;
      }
   }
   //case2 - insert at head(not empty)
   if(curr == head) {
      newNode -> next = head;
      head = newNode;
   }
   else {

   //case3 - insert somewhere after the head(not empty)
      newNode -> next = curr;
      trail -> next = newNode;
   }
   }
   length++; // incrementing the size
}

void ContactList::deleteNode(const string& inputName) {
   //case1 - empty list
   if(head == 0) {
      cout << "empty list, cannot delete " << endl;
   }

   else {
      Contact* curr = head;
      Contact* trail = 0;

      // traverse list to find the node to delete
      while(curr != 0) {
         if(curr -> name == inputName) {
             break;
         }
         else {
            trail = curr;
            curr = curr -> next;
         }
      }
      //case2 -Node with name "inputName" not found in list
      if(curr == 0) {
         cout << "Contact with name: " << inputName << " not found." << endl;
      }

      else {
         //case3 - delete from the head
         if(head == curr) {
            head = head -> next;
         }
         // case 4 - delete beyond head
         else {
            trail -> next = curr ->next;
         }
         delete curr;
         length--; //别忘了
      }
   }

}

void ContactList::reversePrintList() {
   reversePrintWorker(head);
}

void ContactList::reversePrintWorker(Contact* curr) {
   if(curr != 0) {
      reversePrintWorker(curr -> next);
      cout << *curr << endl;// 由于我们已经在Contact中重载了<< operator, 所以这样写是正确的
   }
}



 

测试主程序: NewContactListApp.cpp:

#include "ContactList.h"
using namespace std;

int main() {
   ContactList* cl1 = new ContactList;
   string name;
   while(true) {
      cout << "Enter the name of the contact, or q to quit:";
      cin >> name;
      if(name == "q")
         break;
      cl1->insert(name); // so that we can have a sorted linked list
   }

   while(true) {
      cout << "Enter the name of the contact to delete, or q to quit:";
      cin >> name;
      if(name == "q")
         break;
      cl1-> deleteNode(name); // so that we can have a sorted linked list
   }

   cl1 -> reversePrintList();
   cl1 -> printList();
   return 0;
}


运行结果如下:

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值