c 언어: 양 방향 링크 로 양 단 대기 열 을 실현 합 니 다 (대기 열 양쪽 에서 모두 입대 작업 을 할 수 있 습 니 다)

//       :      1,      2
#include<iostream>
#include<stdio.h>
#include<math.h>
#define LEN sizeof(struct DQueuelist)
using namespace std;
typedef struct DQueuelist    //       
{
	int data;                //      
	struct DQueuelist *prior; //       
	struct DQueuelist *next;  //       
}DQueuelist, *DQueueptr;
typedef struct                //      
{
	DQueueptr front;         //   ,        
	DQueueptr rear;          //   ,           
}LinkDQueue;
//      
void Error(char *s);
LinkDQueue Creat_DQueuelist();              //      
void En_DQueuelist(LinkDQueue &q, int e);    //        
void De_DQueuelist(LinkDQueue &q, int e);    //    
void Print_DQueuelist(LinkDQueue &q);        //    
int Getlength_DQueuelist(LinkDQueue &q);     //          
void Gethead_DQueuelist(LinkDQueue &q, int e); //             
void Destroy_DQueuelist(LinkDQueue &q);      //        

//      
void Error(char *s)
{
	cout << s << endl;
	exit(1);
}
LinkDQueue Creat_DQueuelist()  //      
{
	DQueuelist *pnew;      //       
	LinkDQueue head;
	head.front = head.rear = (struct DQueuelist *)malloc(LEN);  //     
	head.front->data = 0;  
	head.front->next = NULL;
	head.rear->next = NULL;
	int length = 0;
	int number = 0;
	cout << "         :";
	cin >> length;
	cout << "         :";
	for (int i = 0; i < length; i++) //  2      
	{
		pnew = (struct DQueuelist *)malloc(LEN);
		if (!pnew)
			Error("      !");
		cin >> number;
		pnew->data = number;
		pnew->next = NULL;
		head.rear->next = pnew;
		pnew->prior = head.rear;
		head.rear = pnew;
	}
	return head;
}
void En_DQueuelist(LinkDQueue &q, int e)  //    
{//1     1  ,2     2  
	int a;
	DQueuelist *ptr = NULL;
	DQueuelist *temp = NULL;
	cout << "       (     1 2):";
	cin >> a;
	switch (a)
	{
	case 1:
		ptr = (struct DQueuelist *)malloc(LEN);//  1         ,       ,
		                                      //            ,         
		                                        
		if (!ptr)
			Error("      !");
		ptr->data = e;
		temp = q.front->next;
		q.front->next = ptr;
		ptr->next = temp;
		temp->prior = ptr;
		ptr->prior = q.front;
		break;
	case 2:
		ptr = (struct DQueuelist *)malloc(LEN); //  2         ,       
		                                        //              
		if (!ptr)
			Error("      !");
		ptr->data = e;
		ptr->next = NULL;
		q.rear->next = ptr;
		ptr->prior = q.rear;
		q.rear = ptr;
		break;
	}
}
void De_DQueuelist(LinkDQueue &q, int e)  //    
{//1     1  ,2     2  
	int a;
	cout << "       (     1 2):";
	cin >> a;
	DQueuelist *ptr = NULL;
	DQueuelist *temp = NULL;
	switch (a)
	{
	case 1://  1  ,               ,     ,      
		if (q.front->next == NULL)
			Error("     !");
		ptr = q.front->next;
		e = ptr->data;
		cout << "  1      :" << e << endl;
		q.front->next = ptr->next;
		ptr->next->prior = q.front;
		if (q.rear == ptr)
			q.rear = q.front;
		delete ptr;
		break;
	case 2://  2  ,             ,     ,      
		if (q.rear == NULL)
			Error("     !");
		ptr = q.rear->prior;
		temp = q.rear;
		e = temp->data;
		cout << "  2      :" << e << endl;
		ptr->next = NULL;
		q.rear = ptr;
		if (q.front == ptr)
			q.front = q.rear;
		delete temp;
		break;
	}
}
void Print_DQueuelist(LinkDQueue &q) //    
{
	int e = 0;
	int n = 0;
	cout << "         :";
	cin >> n;
	switch (n)
	{
	case 1:  //  1    ,    
		while (q.front->next != NULL)
		{
			DQueuelist *p = NULL;
			p = q.front->next;
			e = p->data;
			cout << e << " ";
			q.front->next = p->next;
			//p->next->prior = q.rear;
			if (q.rear == p)
				q.rear = q.front;
		}
		cout << endl;
		break;
	case 2://  2    ,    
		while ((q.rear != NULL)&&(q.rear->data!=0))
		{
			DQueuelist *p = NULL;
			DQueuelist *temp = NULL;
			p = q.rear->prior;
			temp = q.rear;
			e = temp->data;
			cout << e << " ";
			p->next = NULL;
			q.rear = p;
			if (q.front == p)
				q.rear = q.front;
		}
		cout << endl;
		break;
	}
}
int Getlength_DQueuelist(LinkDQueue &q) //          
{
	DQueuelist *p = NULL;
	p = q.front;
	int length = 0;
	while (p->next != NULL)
	{
		length++;
		p = p->next;
	}
	return length;
}
void Gethead_DQueuelist(LinkDQueue &q, int e) //             ,         ,         
{//1          1   ,2          2   
	cout << "       :";
	int a = 0;
	cin >> a;
	switch (a)
	{
	case 1:
		if (q.front->next == NULL) //               
			Error("     !");
		e = q.front->next->data;
		cout << "  1       :" << e << endl;
		break;
	case 2:
		if (q.rear == NULL) //            
			Error("     !");
		e = q.rear->data;
		cout << "  2       :" << e << endl;
		break;
	}
}
void Destroy_DQueuelist(LinkDQueue &q)  //    
{
	while (q.front)
	{
		q.rear = q.front->next;
		delete q.front;
		q.front = q.rear;
	}
	cout << "        !" << endl;
}

int main()
{
	LinkDQueue Q;
	Q = Creat_DQueuelist();
	cout << "         :" << Getlength_DQueuelist(Q) << endl;
	int m = 0;
	for (int i = 0; i < 2; i++)
	{
		int n = 0;
		cout << "         :";
		cin >> n;
		En_DQueuelist(Q, n);
	}
	cout << "           :" << Getlength_DQueuelist(Q) << endl;
	for (int j = 0; j < 2; j++)
	{
		De_DQueuelist(Q, m);
	}
	cout << "           :" << Getlength_DQueuelist(Q) << endl;
	Print_DQueuelist(Q);
	Destroy_DQueuelist(Q);
	return 0;
}

좋은 웹페이지 즐겨찾기