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

📄 list.cpp

📁 这是一个linux下的Shell.有命令历史和命令提示
💻 CPP
字号:
#include "List.h"#include <unistd.h>#include <fcntl.h>//List.cpp //the class List is a template class.but I only use it in the shell.//so I consider T as char in the inset and get functions.template <class T>List<T>::List(void){	head = 0;	cur = 0;	num = 0;}template <class T>List<T>::~List(void){}//The List reset from a file.//I consider T as chartemplate <class T>void List<T>::overffile(char* filepath){	if(!filepath)		return;	int fid = open(filepath,O_RDONLY);	overffile(fid);}template <class T>void List<T>::overffile(int fid){		if(fid >= 0)	{		clear();		head = 0;		cur = 0;				struct stat fileinfo;		fstat(fid,&fileinfo);				int SIZE = fileinfo.st_size;		char a;		int start = 0;		if(SIZE > 0)		{			do			{				read(fid,&a,1);				start++;			}while(a!='\n');			for(int i = start;i < SIZE; i++)			{				read(fid,&a,1);				if(a == '\n')				{					lseek(fid,start,SEEK_SET);					char* work = new char[i - start + 1];					read(fid,work,i - start + 1);					work[i - start] = 0;						inset(work, -1);					delete work;					do					{						read(fid,&a,1);						i++;					}while(a == '\n' && i < SIZE);					if(a == 0)						break;					else						start = i;				}				}		}		close(fid);	}}//inset data as new headtemplate <class T>void List<T>::inset(T* data){	T* o = new T[strlen(data)+1];	strcpy(o,data);	node<T>* temp = new node<T>(o,head);	head = temp;	num++;}//inset follow the heartemplate <class T>void List<T>::inset(List<T>* p){	if(!p)		return;	if(!(p->head))		return;	p->reset();	T* temp;		while((temp = p->getCur()) != 0)	{		inset(temp,-1);	}	}template <class T>void List<T>::inset(T* data, int lev){        if(lev == 0)	{                inset(data);		return;	}        if(lev > 0)        {                		cur = head;                if(cur != 0)		{			for(int i = 1; i < lev ; i++)			{                		if(cur->next != 0)					cur = cur->next;				else					break;			}			T* o = new T[strlen(data)+1];			strcpy(o,data);			                	node<T>* temp = new node<T>(o,cur->next);                	cur->next = temp;			num++;		}        }        else        {                cur = head;               	if(cur != 0)		{        	        while(cur->next != 0)                	{                	        cur = cur->next;        	        }			T* o = new T[strlen(data)+1];			strcpy(o,data);	                			cur->next = new node<T>(o,0);       			num++;		 }		 else			inset(data);	}}//get the current point (just copy the data)//and inc the current point.template <class T>inline T* List<T>::getCur(void){	if(cur)	{		T* temp = new T[strlen(cur->ptr)+1];		strcpy(temp,cur->ptr);		cur = cur->next;		return temp;	}	return  (T*)0; }//get the point point by the index in the list. ( just copy the data)template <class T>inline T* List<T>::operator[](int index){	if(!head || index < 0 || index >= num)		return (T*)0;	cur = head;	for(int i = 0; i < index; i++)		cur = cur->next;		T* temp = new T[strlen(cur->ptr)+1];	strcpy(temp,cur->ptr);	cur = cur->next;	return temp;}template <class T>int List<T>::getNum(void){	return  num;}//use the head point reset the current point //only cur = head//normal use to scan the listtemplate <class T>void List<T>::reset(void){	cur = head;}//delete cur point//but not inc the current point//because I provide the clear function to clear the listtemplate <class T>bool  List<T>::del(int lev){	if(head)	{        	cur = head;       	 	if(lev  <= 0)        	{			head = head->next;			delete cur->ptr;                       	delete cur;			num--;			return true;        	}                                                                                                                                                       	for(int i = 1; i < lev ; i++)		{			if(cur->next->next != 0)        			cur = cur->next;                	else				break;		}        	node<T>* temp = cur->next;        	cur->next = temp->next;        	if(temp)		{			delete temp->ptr;			delete temp;		}		num--;		return true;	}	return false;} //delete a term//just compare the data of two point //this->cur and data//not the value of two point template <class T>bool  List<T>::del(T* data){	if(head)	{        	cur = head;		node<T>* father = 0;		while(cur != 0)		{			if(cur->ptr)			{				if(strcmp(cur->ptr,data) == 0)				{					if(father == 0)					{										head = head->next;												delete cur->ptr;						delete cur;						num--;						return true;					}					else					{						father->next = cur->next;						delete cur->ptr;						delete cur;						num--;						return true;					}					}			}			father = cur;			cur = cur->next;		}	}	return false;} template <class T>T** List<T>::getbuf(void){	T** o;	o = new T* [num + 1];	o[num] = 0;	cur = head;	for(int i = 0; i < num; i++)	{		o[i] = new T[strlen(cur->ptr)+1];		strcpy(o[i],cur->ptr);		cur = cur->next;	}	return o;}template <class T>T** List<T>::getbuf_subdir(void){	T** o;	o = new T* [num + 1];	o[num] = 0;	cur = head;	for(int i = 0; i < num; i++)	{		char* work = new T[strlen(cur->ptr)+1];		strcpy(work,cur->ptr);		if(strcmp(work, "/") == 0)		{			o[i] = new char[2];			o[i][0] = '/';			o[i][1] = 0;		}		else		{			int len = strlen(work);			if(work[len - 1] == '/')			{				work[len - 1] = 0;				len--;			}			int j;			for( j = len; j >= 0 &&  work[j] != '/'; j--);			o[i] = new char[len - j + 1];			strcpy(o[i], &work[j + 1]);		}		cur = cur->next;	}	return o;}template <class T>void List<T>::show(int fid){	if(head != 0)	{		cur = head;		while( cur != 0)		{			if(cur->ptr != 0)			{				write(fid,cur->ptr,strlen(cur->ptr));				write(fid,"\n",1);				}			if(fid != 1)				write(fid,0,1);				cur = cur->next;		}	}}template <class T>void List<T>::clear(void){	if(head)	{		cur = head;		node<T>* temp;		do		{			temp = cur;			cur  = cur->next;			delete temp->ptr;			delete temp;		}while(cur != 0);	}	head = 0;	num=0;}template <class T>bool List<T>::isempty(void){	return head ? false : true;}template <class T>void List<T>::inset(T** p){	if(p)	{		while(*p)		{			inset(*p);			p++;		}	}}template <class T>void List<T>::change(int index,T* data){	if(!head || index < 0 || index >= num)		return;	cur = head;	for(int i = 0;i < index; i++)		cur = cur->next;	delete cur->ptr;	if(data)	{		cur->ptr = new T[strlen(data)+1];		strcpy(cur->ptr, data);	}	else		cur->ptr = 0;}template <class T>T* List<T>::getEqu(void){	if(!head)		return 0;	T temp;	temp = head->ptr[0];	int num = 0;	while(temp)	{		cur = head;		while(cur!=0)		{			if(cur->ptr[num] == temp)				cur = cur->next;			else				break;		}		if(!cur)		{			num++;			temp = head->ptr[num];		}		else			temp = 0;	}	if(num == 0)		return 0;	else	{		T* o = new T[num + 1];		o[num] = 0;		strncpy(o, head->ptr, num * sizeof(T));		return o;	}	}

⌨️ 快捷键说明

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