链表类List

这个类是为了给GeneralTree提供Queue队列操作才创建的。

因为GeneralTree使用层次遍历的时候,必须要使用这样一个队列。当然如果你没有这样一个队列,但是你在每个节点那里设置了计数器,那么你还是可以完成一个层次遍历操作。

这里为了保持GeneralTreeNode的最简单性,所以没有添加计数器属性。

下面是List类的具体代码:

/* * create by chico chen * date: 2009/02/02 */ #ifndef _LIST_H_ #define _LIST_H_ template<class T> class ListNode { public: T data; ListNode<T>* next; ListNode() { next = NULL; } ListNode(T & data) { this->data = data; next = NULL; } ListNode(T & data, ListNode<T>* next) { this->data = data; this->next = next; } ~ListNode() { next = NULL; } }; template<class T> class List { private: int length; ListNode<T>* head; public: List() { head = NULL; length = 0; } List(ListNode<T>* head) { this->head = head; length = 1; } ~List() { ListNode<T>* tempHead = head; while(NULL != head) { tempHead = head->next; delete head; head = tempHead; } length = 0; } bool IsEmpty() { return 0 == length; } // the coral function to insert the node // pre : none // post: no matter what kind of situation, the function can work. void Insert(ListNode<T>* node, int index) { if(0 == index) { // insert in the front if(NULL == head) { head = node; } else { node->next = head; head = node; } } else { if(NULL == head) { head = node; } else { if(index >= length || index < 0) { index = length; } ListNode<T>* temp = head; while(NULL != temp->next && index--) { temp = temp->next; } temp->next = node; } } length ++; } ListNode<T>* GetHead() { return this->head; } // pre: list is not empty // post: return the last element. ListNode<T>* GetTail() { if(NULL == head) { return NULL; } ListNode<T>* tempHead = head; while(NULL != tempHead->next) { tempHead = tempHead->next; } return tempHead; } // pre: node is not NULL // post: return the node's next node ListNode<T>* GetNext(ListNode<T>* node) { if(NULL != node) { return node->next; } } // pre: begin at zero, and the index is less than the length of the list // post: if index is -1, then return the last // post: if index is not -1 and less than zer, or bigger than the length, throw exception. ListNode<T>* GetAt(int index) { ListNode<T>* temp = NULL; if(-1 == index) { // get the last temp = GetTail(); } else if(0 <= index && index < length) { ListNode<T>* tempHead = head; while(tempHead && index--) { tempHead = tempHead->next; } temp = tempHead; } else { throw "out of list"; } return temp; } void DeleteHead() { ListNode<T>* tempHead = head; head = head->next; delete tempHead; this->length--; } // pre: head is not NULL // post: delete the last one and length subtract 1 void DeleteTail() { if(NULL == head) return; ListNode<T>* tempTail = head; while(NULL != tempTail->next) { tempTail = tempTail->next; } delete tempTail; length--; } // pre: head may be not NULL // post: if index is equal to -1, then delete tail element // post: if index is equal to 0, then delete the head // post: if it is bigger than the length, or less than 0, throw exception // post: delete one node at index and length subtract 1 void DeleteAt(int index) { if(-1 == index) { DeleteTail(); } else if(0 == index) { DeleteHead(); } else if(0 < index && index < length) { ListNode<T>* temp = GetAt(index-1); ListNode<T>* dNode = temp->next; temp->next = dNode->next; delete dNode; length --; } else { throw "out of List"; } } }; #endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下载不后悔,带题带答案,为下一届学员造福。 1.简单档1题: 试按以下给出的排序算法为整数链表编写一个排序函数: 该算法是按表元键值的各位值进行排序。 设有一个整数链表,其中表元的键值为不超过三位数的整数,不妨设键值形式ABC。其中A表示键值的百位数,B为十位数,C为个位数。首先按键值中的个位值C对链表作分拆和链接,先把链表分拆成10个队列链表,然后以C的值从0至9的顺序把分拆后的十个队列链表重新收集成一个链表。接着依次对键值中的B和A进行同样的分拆和链接操作,则最后收集起来的链表是按键值从小到大排序链接的。如有一个链表按它们的键值其表元的链接顺序依次为: 153 678 56 288 457 653 721 876 433 254 按它们的键值的个位分拆,得到十个队列链表,列出它们的键值顺序有: 0: 空链表 1: 721 2: 空链表 3: 153 653 433 4: 254 5: 空链表 6: 56 876 7: 457 8: 678 288 9: 空链表 顺序将它们收集一起后,链表的键值顺序有: 721 153 653 433 254 56 876 457 678 288 再按它们键值的十位分拆,得到十个队列链表,列出它们的键值顺序有: 略。 顺序将它们收集在一起后,链表的键值顺序有: 721 433 153 再按它们键值的百位分拆,得到十个队列链表,列出它们的键值顺序有: 略。 顺序将它们收集一起后,链表的键值顺序有: 56 153 254 288 433 457 653 678 721 876 要求: 1、 试用C语言编程实现以上功能 2、 10个数字随机生成 3、 程序可读性好
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值