オブジェクト向け4回目のジョブリファレンスコード

2502 ワード

第一題:配列
#include 

const int MAXSIZE = 20;

class List{
public:
	List()
	{
		arr = new int[MAXSIZE];   //Allocation the space
		length = 0;               //the length of current array
		cout<>*(arr + i);
			length ++;
		}
	}
	~List()
	{
		delete []arr;
	}
	void Insert(int i,int x);   //Insert a data into the array
	void Delete(int i);         //Delete a data from the array
	int Find(int x);            //Search a data from the array return the position of the data
	void Show();                //Print the data to SCR
private:
	int *arr;
	int length;
};

void List::Insert(int i,int x)
{
	if(i < 1 || i > length + 1)
	{
		cout<= q; --p)
		*(p + 1) = *p;   //            
	*q = x;  //  x
	++length;  //   1
	return;
}

void List::Delete(int i)
{
	if( i < 1 || i > length)
	{
		cout<

第二題:チェーンテーブル
#include  
  
struct Arr  
{  
    int data;  //  
    struct Arr *next;  //      
}; 

class Link{
private:
	Arr *head;
public:
	Link();
	void Create();
	void Insert(int n,Arr *Insdata);
	void Delete(int m);
	void Show();
	int Find(int x);
	static int num;
};

int Link::num = 0;  //       

Link::Link()  //    
{
	head = new Arr;
	head->next = NULL;
}

void Link::Create()  //    
{
	Arr *p1,*p2;
	p1 = p2 = new Arr;
	cout<>p1->data;
	while(p1->data != 0)
	{
		if(head->next == NULL)
		{
			head->next = p1;
		}
		else
		{
			p2->next = p1;
		}
		p2 = p1;
		num++;
		p1 = new Arr;
		cin>>p1->data;
	}
	p2->next = NULL;
	return;
}

void Link::Show()  //       
{
	Arr *p;
	p = head->next;
	cout<data<next;
	}
	cout<next;
	p2 = p1->next;
	cout<>Insdata->data;
	p1->next = Insdata;
	Insdata->next = p2;
	num++;
}

void Link::Delete(int m)//   m     
{
	Arr *p1,*p2;
	p1 = head;
	for(int i = 1;i < m;i++)
		p1 = p1->next;
	p2 = p1->next;
	p1->next = p2->next;
	delete p2;
	num--;
}

int Link::Find(int x)//     x      
{
	Arr *p;
	int n = 1;
	p = head->next;
	while(p != NULL)
	{
		if(p->data == x)
			return n;
		else
			p = p->next;
		n++;
	}
	return -1;
}

void main()
{
	Link obj;
	obj.Create();
	obj.Show();
	Arr *p = new Arr;;
	int n,m;
	cout<>n;
	if(n > obj.num + 1 || n < 1)
	{
		cout<>n;
	if(n > obj.num || n < 1)
	{
		cout<>n;
	m = obj.Find(n);
	if(m == -1)
		cout<