⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node.h

📁 本系统所完成功能如下: &#61548 即时消息:用户之间发送即时消息。 &#61548 聊天纪录:聊天记录的保存及显示。 &#61548 注册:登陆界面即可进入注册界面 &#61548
💻 H
字号:
#ifndef _NODE_H_
#define _NODE_H_  

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "stdlib.h"
#include "conio.h"
#include <iostream.h>
template < class T >
class Node {
    private:
      Node < T > * next;
    public:
      T data;
      // 构造函数
      Node ( const T& item ,Node < T > * ptrnext = NULL);
      // 在当前结点之后插入指针 p 所指结点
      void InsertAfter(Node < T > *p);
      // 删除当前结点的后继结点
      Node < T > * DeleteAfter(void);
      // 返回指向当前结点的后继结点的指针
      Node < T > * NextNode(void) const;
      //创建一个data域值为 item,next 域值为  	 nextptr的结点。
      Node<T> * GetNode(const T& itme,Node<T> * nextptr = NULL);
	  //在头指针为 head 的链表中,插入一个data 域为 item 的新结点作为该链表的表头结点。
      void InsertFront (Node<T> * &head ,const T item);
	  //遍历链表
      void printList(Node<T> * head);
	  //表尾插入结点
      void InsertRear(Node<T> * & head,const T& item );
	  //将一head为头结点的单链表逆转
      void Inverse(Node<T> * & head);
	  //初始化链表
	  void InitialNode(Node<T> * head,Node <T> * nextptr);
	  //求链表长
	  float SizeNode(Node<T> * head);
	  //递增排序
      void InSort(Node<T> * head);
}; 

        
//成员处理函数
//构造函数
template < class T >
Node< T >::Node(const T& item ,Node < T > * ptrnext):
   data(item),next(ptrnext)
   { }


//返回当前结点next域值
template < class T >
Node < T > * Node < T > ::NextNode(void) const
   { 
    return next;
   }


//当前结点后插入结点p
template < class T >
void Node < T >:: InsertAfter(Node < T > *p)
{ p->next = next;
      next=p; }


//删除当前结点的后继结点指针
template < class T > 
Node < T >* Node < T > ::DeleteAfter(void)
  { if( next== NULL)
     return  NULL;
   Node < T > * tempptr = next;
   next = tempptr->next; return tempptr;
   }


//创建一个data域值为 item,next 域值为  	 nextptr的结点。
template < class T >
Node<T> * Node< T >::GetNode(const T& itme,Node<T> * nextptr )
  { Node<T> * newNode ;
    newNode = new Node<T> (itme, nextptr );
    if( newNode == NULL)
	{
       cerr<<"Memory allocation failure !"<<endl;
       exit(1); }
     return newNode; 
   }


//在头指针为 head 的链表中,插入一个data 域为 item 的新结点作为该链表的表头结点。
template < class T >
void Node< T >::InsertFront (Node<T> * &head , T item)
 { 
   head = GetNode( item,head);
  }


//遍历链表
template < class T >
void Node< T >::printList(Node<T> * head)
	 {   
	    Node<T> * currptr = head->next;
        int count = 0;
        while ( currptr != NULL )
		{ 
			cerr<<(currptr->data)<<" ";          //cerr--输出流对象关联无缓冲的标准错误输出设备  
		if( (++count ) %10==0 ) 	                   
			printf("\n");
 	      currptr = currptr->NextNode( );
		} 
}  


//表尾插入结点
template < class T >
void Node< T >::InsertRear(Node<T> * & head,const T& item )
  { 
	Node<T> * currptr = head;
    if(currptr == NULL)
      InsertFront(head,item);
    else
	{
		while ( currptr->NextNode( )!= NULL )
	    currptr = currptr->NextNode( ); 
        Node<T> * newNode;
        newNode = GetNode(item);
        currptr->InsertAfter(newNode);
	}
  }


//将一head为头结点的单链表逆转
template < class T >
void Node< T >::Inverse(Node<T> * & head)
{
	if(head==NULL) return;
	Node<T> * q=head->next,*p=head->next->next,*r=NULL;
	while(p!=NULL)
	{
		q->next=r;r=q;q=p;p=p->next;
	}
	q->next=r;head->next=q;
}


//初始化链表
template < class T >
void Node< T >::InitialNode(Node<T> * head,Node <T> *  nextptr)
{
	Node <T> *p=nextptr; float item;
	cout<<"请输入此单链表的初始化值:"<<endl;
    while(cin>>item,!cin.eof())
	{
		if(item!=0&&!cin.eof())
		{
			p=GetNode(item,NULL); 
		    nextptr->next=p;
		    nextptr=p;
		}
		else break;
	}
	cout<<"您所初始化的链表为:";	
	printList(head);
	cout<<endl;
}


//求链表长
template < class T >
float Node< T >::SizeNode(Node<T> * head)
	 {   
	    Node<T> * currptr = head->next;
        float count = 0;
        while ( currptr != NULL )
		{           
		  count++; 
 	      currptr = currptr->NextNode( );
		} 
		return count;
}  


//递增排序
template < class T >
void  Node < T >::InSort(Node<T> * head)
{
	Node<T> *p=head->next;
    while ( p != NULL )    
	{
		Node<T> * currptr=head->next;
		while ( currptr->next != NULL )
		{            
 	      if((currptr->data)>(currptr->next->data))
		  {
			 T t;
			 t=currptr->data;
			 currptr->data=currptr->next->data;
			 currptr->next->data=t;
		  }
			currptr = currptr->NextNode( );
		}
		p = p->NextNode( );
	}
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -