📄 5040309649_01.cpp
字号:
/* F0403023 5040309649 Cieven.F.(方习文)
Programm created to indicate the use of Queue and Node.
Node is used in this programm,creating space for each one.
Dynamic allocating memory for each cell,avoiding waste of memory.
Thingking in C++,Assignment 1.
The programm icon is created by Cieven.F.
*/
#include <iostream.h>
#include <malloc.h>
struct number
{
int data;
struct number *next;
};
/* Struct forms a typical type of Node.
data is used to record the integer itself.
*next is a pointer-to-number,which is use to
save the next one's address.
*/
/* class :IntQueue.
class created to organize every variaties and functions
that are associated with the Queue.
*/
class IntQueue
{
private:
struct number *head,*present;
int size;
//private part of the class.
//*head:the address of the very first one.
//*present:keep track of the present onr.
//*the use of *present may not be apparent in this programm.
//but in my opinion it's a good way to keep track
//in case there's necessarity to expand the programm.
//size: indicate the current size of the queue.
public:
IntQueue()
{
present=new number;
head=new number;
head->next=present;
present->next=NULL;
size=0;
}
//Initial the class.
//Executing while the programm is run.
bool EnQueue (int *num)
{
struct number *p;
p=new number;
present->next=p;
p->next=NULL;
p->data=*num;
if(size==0) head->next=p;
present=p;
cout<<"This is EnQueue operation:"<<*num<<endl;
size++;
if(p!=NULL) return 1;
else return 0;
}
//Put an integer into the queue.
//Dynamic allocating the memory.
void Display () const
{
number *f;
f=head->next;
while(f!=NULL)
{
cout<<f->data<<endl;
f=f->next;
}
}
//Display all the member of the queue.
int *DeQueue (void)
{
number *p;
p=new number;
p=head;
head=head->next;
delete p;
size--;
return(&(head->data));
}
//return the address of the first member.
//Free the address of the first member.
int *Peek(void)
{
number *p;
p=head->next;
return(&(p->data));
}
//return the address of the first member.
//To be distinguished from the former one
//this function dose not free the address.
//Only return is done.
int GetSize (void) const
{
return(size);
}
//Get the current size of the queue.
//return the number of the size.
};
//The end of the class.
//Main programm.
int main(int argc, char* argv[])
{
int i = 1, j = 2, k = 3, m = 4;
IntQueue que;
que.EnQueue(&i);
que.EnQueue(&j);
que.EnQueue(&k);
que.Display();
cout << "This is Dequeue operation: " << *que.DeQueue() << endl;
cout << "This is Peek operation: " << *que.Peek() << endl;
cout << "The size of queue is: " << que.GetSize() << endl;
que.EnQueue(&m);
que.Display();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -