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

📄 queue.cpp

📁 It is about link data structure of q
💻 CPP
字号:
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
/*
The following code is inserted to apply clrscr and gotoxy in visual C++
Date:	12/16/06
*/

#include < windows.h >

void clrscr(void);
void clreol(void);
void clreoscr(void);
void gotoxy(int ,int );

// --- Functions implementing

/*********************************************************************************/

void clrscr(void)  //clearscreen 
{
    CONSOLE_SCREEN_BUFFER_INFO    csbiInfo;                            //variable declaration
    HANDLE    hConsoleOut;
    COORD    Home = {0,0};
    DWORD    dummy;

    hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);

    FillConsoleOutputCharacter(hConsoleOut,' ',csbiInfo.dwSize.X * csbiInfo.dwSize.Y,Home,&dummy);  
    csbiInfo.dwCursorPosition.X = 0; 
    csbiInfo.dwCursorPosition.Y = 0; 
    SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition); 
}

/*********************************************************************************/

void clreol(void)  //clear end of line 
{
    CONSOLE_SCREEN_BUFFER_INFO        csbiInfo;                           //variable declaration
    HANDLE    hConsoleOut;
    COORD    Home,pos;
    DWORD    dummy;

    hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);

    Home = csbiInfo.dwCursorPosition;
    pos.X = 80 - csbiInfo.dwCursorPosition.X;

    FillConsoleOutputCharacter(hConsoleOut,' ',pos.X,Home,&dummy);
}

/*********************************************************************************/

void clreoscr(void)  //clear end of screen
{
    CONSOLE_SCREEN_BUFFER_INFO        csbiInfo;                          //variable declaration
    HANDLE    hConsoleOut;
    COORD    Home;
    DWORD    dummy;

    hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);

    Home=csbiInfo.dwCursorPosition;
    FillConsoleOutputCharacter(hConsoleOut,' ',csbiInfo.dwSize.X * csbiInfo.dwSize.Y,Home,&dummy);
}

/*********************************************************************************/

void gotoxy(int x,int y)  //cursor an position 
{
    CONSOLE_SCREEN_BUFFER_INFO        csbiInfo;                            //variable declaration
    HANDLE    hConsoleOut;

    hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);

    csbiInfo.dwCursorPosition.X = x;
    csbiInfo.dwCursorPosition.Y = y;
    SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition); 
}

/*********************************************************************************/
//user-defined types
typedef char string4[5];
typedef char string30[31];

typedef struct runner{
	string4 raceNumber;
	string30 name;
};

//self-referential structure
typedef struct node{
	runner entry;
	node * next;
};

typedef node * qnodeptr;//pointer to struct

typedef struct queueADT{
	qnodeptr front;
	qnodeptr rear;
};

//function prototypes
void menuControl();
void displayMenu();
void pauseScreen();
void addControl(queueADT &);
void deleteControl(queueADT &);
void displayQ(queueADT);
void traverseQ(queueADT);
void createNode(node &);
void initialiseQ(queueADT &);
int emptyQ(queueADT);
void addToQ(queueADT &, qnodeptr);
void deleteFromQ(queueADT &, string4 &);

//-------------- start of program --------------
void main()
{
	clrscr();
	menuControl();
}//end main

void menuControl()
{
	queueADT oneQueue;
	char choice;
	oneQueue.front = NULL;
    oneQueue.rear = NULL;

	do{
		clrscr();
		displayMenu();
		gotoxy(10,10);
		printf("\n\n\t\tMake your selection > ");
		choice = toupper(getch());

		switch(choice){
	    	case '1' : initialiseQ(oneQueue);
				       break;
			case '2' : addControl(oneQueue);
			           break;
	    	case '3': deleteControl(oneQueue);
			           break;
	    	case '4': displayQ(oneQueue);
			           break;
					   }//endswitch
	}while(choice != 'Q');
}

void displayMenu()
{
	printf("\n\t\tSoftware Development: Linked Data Structures.");
	printf("\n\t\t    Assessment Task 3 - RPN Calculator");
	printf("\n\n\t\t\t1. Cancel Race.");
	printf("\n\t\t\t2. Arrivals.");
	printf("\n\t\t\t3. Departures.");
	printf("\n\t\t\t4. Display runners.");
	printf("\n\t\t\tQ. QUIT");
}

void addControl(queueADT & aQueue)
{
	qnodeptr tempPtr;

	if ((tempPtr=new (node))==NULL){
		gotoxy(10,12);
		printf("Out of memory");
	}
	else {
		createNode(*tempPtr);
		if (aQueue.front==NULL){
			aQueue.front=aQueue.rear=tempPtr;
		}
		else
			//append the node on to the queue
			addToQ(aQueue, tempPtr);
		//end if
	}
}


void createNode(node & aNode)
{
	printf("\n\n\t\tEnter the race number > ");
	gets(aNode.entry.raceNumber);
	printf("\t\tEnter the runner name > ");
	gets(aNode.entry.name);
	aNode.next=NULL;
}

void deleteControl(queueADT & aQueue)
{
	string4 raceNo;

	if (emptyQ(aQueue)){
		printf("\n\n\t\t*** empty queue ***");
		pauseScreen();
	}
	else{
		deleteFromQ(aQueue, raceNo);
		printf("\n\n\t\trunner %s GO", raceNo);
		pauseScreen();
		}//end if
}

void displayQ(queueADT aQueue)
{
	if (aQueue.front==NULL)
		 printf("\n\n\t\t*** no runners in queue ***");
    else{
		 printf("\n\n\tposn\tnumber\tname\n");
		 clreol();
		 traverseQ(aQueue);
		}
    pauseScreen();
}

void traverseQ(queueADT aQueue)
{
	int pos=1;
	qnodeptr current;

    current=aQueue.front;
	while(current!=NULL){
		printf("\t%d\t%s\t%s\n",pos,current->entry.raceNumber,current->entry.name);
		current=current->next;
		pos++;
	}//endwhile
}

void pauseScreen(){
	//display message until key is pressed
	gotoxy(5,24);
	printf("Press any key to continue ...");
	getch();

	//clear message
	gotoxy(40,24);
	clreol();
}

void initialiseQ(queueADT & aQueue)
{
	aQueue.front=NULL;
	aQueue.rear=NULL;
}

int emptyQ(queueADT aQueue)
{
	if(aQueue.front==NULL&&aQueue.rear==NULL)
       return 1;
	else
	   return 0;
}

void addToQ(queueADT & aQueue, qnodeptr tempPtr)
{
    aQueue.rear->next=tempPtr;
    tempPtr->next=NULL;

    if(emptyQ(aQueue)){
        aQueue.front=tempPtr;
        aQueue.rear=tempPtr;
	}
    else{
     	aQueue.rear=tempPtr;
	}
}

void deleteFromQ(queueADT & aQueue, string4 & raceNo)
{
	node * delptr;
	delptr=aQueue.front;
	strcpy(raceNo, aQueue.front->entry.raceNumber);
	aQueue.front=aQueue.front->next;
	delete delptr;
}


⌨️ 快捷键说明

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