含有头结点的单链表,实现部分功能,希望大家能够给予指点

//LinkNode节点类
#ifndef LINKNODE_H
#define LINKNODE_H
template
    
    
     
     
struct LinkNode{
	T data;
	LinkNode
     
     
      
       *next;
	LinkNode(LinkNode
      
      
       
        *str = NULL){
		next = str;
	}
	LinkNode(T tempData, LinkNode
       
       
        
         *str = NULL){
		data = tempData;
		next = str;
	}
};
#endif
//链表类
#include
        
        
          using namespace std; //利用结构体,实现节点(存取数据和下一节点的指针) template 
         
           struct LinkNode{ T data; LinkNode 
          
            *next; LinkNode(LinkNode 
           
             *str = NULL){ next = str; } LinkNode(T tempData, LinkNode 
            
              *str = NULL){ data = tempData; next = str; } }; //该函数实现的是无头结点的单链表 template 
             
               class LinkList{ private: LinkNode 
              
                *head;//头指针 LinkNode 
               
                 *currPtr;//记录表当前位置的指针,又插入和删除操作更新 LinkNode 
                
                  * value; int position;//当前元素在表中的位置序号,由函数reset使用 public: LinkList(); LinkList(const T& tempPtr); LinkList(const LinkList 
                 
                   & tempPtr); ~LinkList(); int getSize()const;//返回链表中元素的个数 bool isEmpty()const;//判断链表是否为空 void reset(int pos = 0);//初始化指针的位置(第一位数据的位置设为0) void next(); bool endOfList()const;//判断是否到了链尾 int currentPosition(void);//返回当前指针的位置 void insertHead(const T& item);//在表头插入节点 void insertTail(const T& item);//在表尾插入节点 //insertAt()函数在单链表中无法实现 //void insertAt(const T& item);//在当前节点之前插入节点 void insertAfter(const T& item);//在当前节点之后插入节点 void deleteHead();//删除头结点 void deleteCurrent();//删除当前节点 T& data();//返回当前节点数据的引用 const T& data()const;//返回当前节点数据的常引用 void clear(); LinkNode 
                  
                    * setPos(int pos);//返回指定位置的pos的指针 bool insertPos(const int i, const T value); bool deletePos(const int i); public: int length(); void print(); }; template 
                   
                     void LinkList 
                    
                      ::print() { LinkNode 
                     
                       * tempStr = head; for (int i = 0; i < length(); i++) { cout << tempStr->data << " "; tempStr = tempStr->next; } cout << endl; } template 
                      
                        int LinkList 
                       
                         ::length() { int count = 0; LinkNode 
                        
                          * tempStr = head; while (tempStr) { count++; tempStr = tempStr->next; } return count; } //默认构造函数 template 
                         
                           LinkList 
                          
                            ::LinkList() { head = NULL; currPtr = NULL; value = NULL; } //含参数的构造函数 template 
                           
                             LinkList 
                            
                              ::LinkList(const T& tempPtr) { head = new LinkNode 
                             
                               (tempPtr, NULL); } //复置构造函数 template 
                              
                                LinkList 
                               
                                 ::LinkList(const LinkList 
                                
                                  & tempPtr) { LinkNode 
                                 
                                   * str = tempPtr.head; LinkNode 
                                  
                                    * temp = new LinkNode 
                                   
                                     (str->data, NULL); head = temp; currPtr = temp; position++; str = str->next; while (str) { temp->next = new LinkNode 
                                    
                                      (str->data, NULL); currPtr = temp; position++; temp = temp->next; str = str->next; } } template 
                                     
                                       LinkList 
                                      
                                        ::~LinkList() { clear(); } template 
                                       
                                         int LinkList 
                                        
                                          ::getSize()const { LinkNode 
                                         
                                           * tempStr = head; int count = 0; //因为该链表有头结点,所以要将第一个节点除去,就要从第二个开始计算 while (tempStr->next) { count++; tempStr = tempStr->next; } return count; } template 
                                          
                                            bool LinkList 
                                           
                                             ::isEmpty()const { if (head == NULL) return true; return false; } //初始化currPtr指针的位置(第一位数据的位置设为0) template 
                                            
                                              void LinkList 
                                             
                                               ::reset(int pos = 0) { position = pos; if (head->next == NULL) { currPtr = NULL; return; } currPtr = head->next; } //将currPtr的位置向后移一个位置 template 
                                              
                                                void LinkList 
                                               
                                                 ::next() { if (value != NULL) value = value->next; else return; } template 
                                                
                                                  bool LinkList 
                                                 
                                                   ::endOfList()const { if (currPtr->next == NULL) return true; else return false; } //返回当前的position的值 template 
                                                  
                                                    int LinkList 
                                                   
                                                     ::currentPosition(void) { LinkNode 
                                                    
                                                      * tempStr = head; while (tempStr != currPtr) { position++; tempStr = tempStr->next; } return position; } //在head后添加节点 template 
                                                     
                                                       void LinkList 
                                                      
                                                        ::insertHead(const T& item) { LinkNode 
                                                       
                                                         * p = new LinkNode 
                                                        
                                                          (item, head); head = p; position++; } //在尾部节点后添加节点 template 
                                                         
                                                           void LinkList 
                                                          
                                                            ::insertTail(const T& item) { if (head == NULL) { head = new LinkNode 
                                                           
                                                             (item, NULL); currPtr = head; value = head; position++; return; } LinkNode 
                                                            
                                                              * p = currPtr; p->next = new LinkNode 
                                                             
                                                               (item, NULL); p = p->next; currPtr = p; position++; } //在当前节点之后添加节点 template 
                                                              
                                                                void LinkList 
                                                               
                                                                 ::insertAfter(const T& item) { LinkNode 
                                                                
                                                                  * tempStr = new LinkNode 
                                                                 
                                                                   (item); tempStr->next = currPtr->next; currPtr->next = tempStr; position++; currPtr = tempStr; } //删除head节点,使链表成为无头结点的单链表 template 
                                                                  
                                                                    void LinkList 
                                                                   
                                                                     ::deleteHead() { LinkNode 
                                                                    
                                                                      * tempStr = head; delate head; head = tempStr; tempStr = NULL; return } //删除当前节点 template 
                                                                     
                                                                       void LinkList 
                                                                      
                                                                        ::deleteCurrent() { LinkNode 
                                                                       
                                                                         * tempStr = setPos(position - 1); tempStr->next = currPtr->next; delete currPtr; currPtr = tempStr; tempStr = NULL; position--; } //返回当前数据的引用 template 
                                                                        
                                                                          T& LinkList 
                                                                         
                                                                           ::data() { return value->data; } template 
                                                                          
                                                                            const T& LinkList 
                                                                           
                                                                             ::data()const { return value->data; } template 
                                                                            
                                                                              void LinkList 
                                                                             
                                                                               ::clear() { LinkNode 
                                                                              
                                                                                * tempStr; while (head->next != NULL) { tempStr = head->next; head->next = tempStr->next; delete tempStr; } position = 0; } //返回pos位置处的指针 template 
                                                                               
                                                                                 LinkNode 
                                                                                
                                                                                  * LinkList 
                                                                                 
                                                                                   ::setPos(int pos) { if (pos == -1) return head; int count = 0; LinkNode 
                                                                                  
                                                                                    * tempStr = head; while (tempStr != NULL&&count < pos) { tempStr = tempStr->next; count++; } return tempStr; } template 
                                                                                   
                                                                                     bool LinkList 
                                                                                    
                                                                                      ::insertPos(const int i, const T value) { LinkNode 
                                                                                     
                                                                                       *tempStr, *tempPtr; //(tempStr = setPos(i - 1))作用是返回要插入点的前驱指针 if ((tempStr = setPos(i - 1)) == NULL) return false; tempPtr = new LinkNode 
                                                                                      
                                                                                        (value, tempStr->next); tempStr->next = tempPtr; return true; } template 
                                                                                       
                                                                                         bool LinkList 
                                                                                        
                                                                                          ::deletePos(const int i) { LinkNode 
                                                                                         
                                                                                           *tempStr, *tempPtr; if ((tempStr = setPos(i - 1)) == NULL || tempStr->next == NULL) return false; tempPtr = tempStr->next; if (tempPtr->next == NULL) { delete tempPtr; tempStr->next = NULL; } else if (tempPtr->next != NULL) { tempStr->next = tempPtr->next; delete tempPtr; } return true; } //主函数 #include"LinkList.h" int main(int argc, char* argv[]) { LinkList 
                                                                                          
                                                                                            list(4); cout << list.isEmpty() << endl; list.insertHead(5); cout << list.length() << endl; list.print(); LinkList 
                                                                                           
                                                                                             temp(list); temp.print(); return 0; } 
                                                                                            
                                                                                           
                                                                                          
                                                                                         
                                                                                        
                                                                                       
                                                                                      
                                                                                     
                                                                                    
                                                                                   
                                                                                  
                                                                                 
                                                                                
                                                                               
                                                                              
                                                                             
                                                                            
                                                                           
                                                                          
                                                                         
                                                                        
                                                                       
                                                                      
                                                                     
                                                                    
                                                                   
                                                                  
                                                                 
                                                                
                                                               
                                                              
                                                             
                                                            
                                                           
                                                          
                                                         
                                                        
                                                       
                                                      
                                                     
                                                    
                                                   
                                                  
                                                 
                                                
                                               
                                              
                                             
                                            
                                           
                                          
                                         
                                        
                                       
                                      
                                     
                                    
                                   
                                  
                                 
                                
                               
                              
                             
                            
                           
                          
                         
                        
                       
                      
                     
                    
                   
                  
                 
                
               
              
             
            
           
          
        
       
       
      
      
     
     
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值