アルゴリズムとデータ構造カリキュラムのc++実装のシーケンステーブルとチェーンテーブル

13109 ワード

ほほほ、先生の出した宿題、自分で書いた奥!
順序表:
#include <iostream>
#include <string>
using namespace std;
struct Data
{	
	int stationNum;
    string name;
	string time;
	int personUpNum;
	int personDownNum;
	Data * next;
};

Data* LinkedlistInit()                 //       
{
    Data * L = new Data;
    if (L == NULL)
    {
		cout <<"         !"<<endl;
    }
	L->next = NULL;
	return L;
}

int LinkedListLength(Data * L)        //    
{
	Data * p = new Data;
	p = L->next;
	int j = 0;
	while(p)
	{
       j++;
	   p = p->next;
	}
    return j;
}

int LinkedListupPerson(Data * L)        //      
{
	Data * p = new Data;
	p = L->next;
	int j = 0;
	while(p)
	{
		j += p->personUpNum ;
		p = p->next;
	}
    return j;
}

Data * LinkedListInsert(Data * L,Data * p,Data e)   //      
{
    Data * pre = new Data;
	pre = L;
	while (pre && pre->next!=p)
	{
		pre = pre->next;
	}
	if (!pre)
	{
		cout <<"   *p  !"<<endl;
		exit(0);
	}
	Data * s= new Data;
	s->name = e.name;
	s->time = e.time;
	s->stationNum = e.stationNum;
	s->personUpNum = e.personUpNum;
	s->personDownNum = e.personDownNum;
	s->next = pre->next;
	pre->next = s;
	return L;
}

Data * LinkedListDel(Data * L,string name)     //      
{
	if (L->next == NULL)
	{
		cout <<"  !"<<endl;
	}
    Data * pre = new Data;
	pre = L;
	while (pre->next && pre->next->name != name)
	{
		pre = pre->next;
	}
	if (pre->next==NULL)
	{
		cout <<"      !"<<endl;
		exit(0);
	}
	Data * s= new Data;
    s = pre->next;
	pre->next = s->next;
	delete(s);
	return L;
}

void Listtraversal(Data * L)     //  List
{
   	Data * p = new Data;
	p = L;
	while(p != NULL)
	{
		cout <<"   :"<<p->stationNum<<endl;
		cout <<"   :"<<p->name<<endl;
		cout <<"     :"<<p->time<<endl;
		cout <<"     :"<<p->personUpNum<<endl;
	    cout <<"     :"<<p->personDownNum<<endl;
		p = p->next;
	}
}

string ListgetTime(Data * L , string name) //        
{
	Data * p = new Data;
	p = L->next;
	while(p->name != name)
	{
		p = p->next;
	}
	if (p != NULL)
	{
        return p->time;
	}
	else
	{
		cout <<"    !"<<endl;
		exit(0);
	}
}

void ListgetprebyName(Data * L , string name)   //          
{
	Data * p = new Data;
	Data * q = new Data;
	q = L;
	p = L->next;
	while(p->name != name)
	{
		p = p->next;
		q = q->next;
	}
	if (p != NULL && q != L)
	{
        cout <<"     :"<<q->name<<endl;
	}
	else
	{
		cout <<"    !"<<endl;
	}

}

void ListgetafterbyName(Data * L , string name)     //          
{
	Data * p = new Data;
	p = L->next;
	while(p->name != name)
	{
		p = p->next;
	}
	if (p != NULL && p->next != NULL)
	{
        cout <<"     :"<<p->next->name<<endl;
	}
	else
	{
		cout <<"    !"<<endl;
	}
	
}

int main()
{
	Data  a;
	a.name = "    ";
	a.time = "7";
	a.personUpNum = 32;
	a.personDownNum = 0;
	a.stationNum = 1;
	Data  b;
	b.name = "    ";
	b.time = "8";
	b.personUpNum = 12;
	b.personDownNum = 22;
	b.stationNum = 2;
	Data  c;
	c.name = "   ";
	c.time = "9";
	c.personUpNum = 2;
	c.personDownNum = 4;
    c.stationNum = 3;
	Data * header= new Data;
	header = LinkedlistInit();
	LinkedListInsert(header,header->next,a);
	LinkedListInsert(header,header->next,b);
	LinkedListInsert(header,header->next,c);
	cout <<LinkedListLength(header)<<endl;
	Listtraversal(header->next);
	cout <<"       :"<<LinkedListupPerson(header)<<endl;
	cout <<"  name  time :"<<ListgetTime(header,"    ")<<endl;
	ListgetprebyName(header,"    ");
	ListgetprebyName(header,"   ");
	ListgetprebyName(header,"    ");
	ListgetafterbyName(header,"    ");
	ListgetafterbyName(header,"   ");
	ListgetafterbyName(header,"    ");
	LinkedListDel(header,"    ");
    Listtraversal(header->next);
}

チェーンテーブル:
#include <iostream>
#include <string>
using namespace std;
struct Data
{	
	int stationNum;
    string name;
	string time;
	int personUpNum;
	int personDownNum;
};
class list
{
public:
	int max;
    static int length;
    Data * data;
	Data * p;
	list(int m);
    void initList(int st, string n, string t, int pu, int pd);  //          
	void deleteList(string n);                                  //      
    static int getLength();                                     //    
    int getpersonall();                                         //     
	void getinformation();                                      //    (  、    、     )
    string getTimebyname(string n);                             //   i ,             
	void getprebystationName(string n);                         //    ,       
	void getafterbystationName(string n);                       //    ,       
};
int list::length = 0;

int list::getLength()
{
    return length;
}

void list::getprebystationName(string n)
{
	int len = 0;
	if (length == 0)
	{
		cout <<"  !"<<endl;
		return;
	}
	data = p;
	while(data->name != n)
	{
		if (len >= (length-1))
		{
			break;
		}
		data++;
		len++;
	}
	data--;
    try
	 {
	   cout <<"   :"<<data->stationNum<<endl;
	   cout <<"   :"<<data->name<<endl;
	   cout <<"     :"<<data->time<<endl;
	   cout <<"     :"<<data->personUpNum<<endl;
	   cout <<"     :"<<data->personDownNum<<endl;
	}
    catch(int)
	{cerr<<"error!";}
	catch(string)
	{cerr<<"error!";} 
	catch(Data *)
	{cerr<<"error!";}
    catch(...)
	{cerr<<"         !"<<endl;}
}

void list::getafterbystationName(string n)
{
	int len = length;
	data = p;
	while(data->name !=n)
	{
		if (len <= 0)
		{
			break;
		}
		data++;
		len--;
	}
	if (len = 0)
	{
		cout <<"          !"<<endl;
		return ;
	}
	data++;
	if (data++)
	{try{
        cout <<"   :"<<data->stationNum<<endl;
		cout <<"   :"<<data->name<<endl;
		cout <<"     :"<<data->time<<endl;
		cout <<"     :"<<data->personUpNum<<endl;
		cout <<"     :"<<data->personDownNum<<endl;
	}
    catch(...)
	{cerr<<"         !"<<endl;}
	}
}

string list::getTimebyname(string n)
{
    int len = length;
	data = p;
	while(len > 0 )
	{
		if (data->name ==n)
		{
			break;
		}
		data++;
		len--;
	}
	return data->time;
}

void list::getinformation()
{
	int len = length;
	data = p;
	while(len > 0)
	{
		cout <<"   :"<<data->stationNum<<endl;
		cout <<"   :"<<data->name<<endl;
		cout <<"     :"<<data->time<<endl;
		cout <<"     :"<<data->personUpNum<<endl;
		cout <<"     :"<<data->personDownNum<<endl;
		data++;
		len--;
	}
}

int list::getpersonall()
{
	int personNumbers = 0;
 	int len = length;
	data = p;
	while (len >= 1)
	{
		personNumbers+=data->personUpNum;
        data++;
        len--;
	}
	return personNumbers;
}

list::list (int m)
{
    this->max = m;
    data = new Data[max];
	p = data;
}

void list::initList(int st, string n, string t, int pu, int pd)
{
	if (length == max + 1)
	{
		cout <<"     !"<<endl;
		return;
	}
	if (st < 0|| st > max)
	{
		cout <<"      !"<<endl;
		return;
	}
	if (length == 0)
	{
		data = data + st;
		data->stationNum = st;
		data->name = n;
		data->time = t;
		data->personUpNum = pu;
		data->personDownNum = pd;
	    length++;
	}
	else
	{
		int j = length;
		for (; j > st;j--)
		{
			data[j].stationNum = data[j-1].stationNum;
			data[j].name = data[j-1].name;
			data[j].time = data[j-1].time;
			data[j].personUpNum = data[j-1].stationNum;
			data[j].personDownNum = data[j-1].personDownNum;
		}
		data[j].stationNum = st;
		data[j].name = n;
		data[j].time = t;
		data[j].personUpNum = pu;
	    data[j].personDownNum = pd;
	    length++;
	}
}

void list::deleteList(string n)
{
	int len = 0;
	if (length == 0)
	{
		cout <<"      !"<<endl;
		return;
	}
	data = p;
	while(data->name != n)
	{
		if (len >= (length-1))
		{
			break;
		}
		data++;
		len++;
	}
	if (len >= (length-1))
	{
		cout <<"             !"<<endl;
		return ;
	}
	else
	{
	    data = p;
		while(len <(length - 1))
		{
			data[len].stationNum = data[len+1].stationNum;
			data[len].name = data[len+1].name;
			data[len].time = data[len+1].time;
			data[len].personUpNum = data[len+1].stationNum;
			data[len].personDownNum = data[len+1].personDownNum;
			len++;
		}
        length--;
		}
}

int main()
{
    list list1(5);
    list1.initList(0,"  ","6",12,4);
    list1.initList(1,"    ","7",45,8);
	list1.initList(3,"     ","9",7,21);
	list1.initList(2,"    ","8",8,43);
	list1.initList(2,"     ","4",4,1);
    cout <<"length :"<<list1.getLength()<<endl;
	cout <<"allnum :"<<list1.getpersonall()<<endl;
    cout <<"      :"<<endl;
	list1.getinformation();
    cout <<"length :"<<list1.getLength()<<endl;
	cout <<"  name  time :"<<list1.getTimebyname("    ")<<endl;
	cout <<"    ,       "<<endl;
	list1.getprebystationName("  ");
	cout <<"    ,       "<<endl;
	list1.getprebystationName("    ");
	cout <<"    ,       "<<endl;
	list1.getafterbystationName("     ");
	cout <<"    ,       "<<endl;
	list1.getafterbystationName("     ");
    cout <<"      "<<endl;
    list1.deleteList("    ");
	cout <<"      :"<<endl;
	list1.getinformation();
	cout <<"length :"<<list1.getLength()<<endl;
    cout <<"      "<<endl;
    list1.deleteList("   ");
	return 0;
}

以上のコードは粗末で、皆さんに教えてほしいです.