閱讀下列程序說明,將在空缺處填入正確的內(nèi)容。
【程序說明】
定義一個多邊形結構:struct polygon實現(xiàn)以下內(nèi)容: (1) 建立該結構的鏈表:create函數(shù)是創(chuàng)建鏈表,每輸入一個結點的數(shù)據(jù),就把該結點加入到鏈表當中,它返回創(chuàng)建的鏈表的頭指針。 (2) 顯示鏈表的各個結點數(shù)據(jù):結點數(shù)據(jù)包括:多邊形頂點數(shù)、各頂點的縱橫坐標、當多邊形頂點數(shù)為0時,鏈表創(chuàng)建結束。 (3) 編寫一個函數(shù)disp,刪除鏈表中的所有結點。需要注意的是:要先釋放結點數(shù)據(jù)內(nèi)存,再刪除結點,如果在釋放結點數(shù)據(jù)內(nèi)存單元之前刪除結點,則無法找到結點數(shù)據(jù)內(nèi)存單元的地址,也就無法釋放數(shù)據(jù)的內(nèi)存單元。
【程序】
#include"iostream.h"
#include"iomanip.h"
struct polygon
{
int n;
int *x;
int *y;
polygon *next;
};
void Push(polygon*& head,int n)
{
polygon*newNode=new polygon;
newNode=new polygon;
newNode->next= (1) ;
newNode->x=new int[n];newNode->y=new int[n];newNode->n= (2) ;
for(int i=0;i<= (3) ;i++){
cout<<"請輸入多邊形各頂點x、y坐標,坐標值之間用空格分隔:";
cin>>newNode->x[i]>>newNode->y[i];}
(4) =head;// 在head前不需要額外的*
head=newNode;
}
polygon *create()
{
polygon*head=NULL;
polygon*tail;
int n;
cout<<"請輸入多邊形頂點的個數(shù)(頂點個數(shù)為0時結束):";
cin>>n;
if(n==0)return (5) ;
Push(head, (6) ;
tail=head;
cout<<"請輸入多邊形頂點的個數(shù)(頂點個數(shù)為0時結束):";
cin>>n;
while(n!=0)
{
Push(tail->next, (7) ;//在tail->next增加結點
tail=tail->next;//advance tail to point to last node
cout<<"請輸入多邊形頂點的個數(shù)(頂點個數(shù)為0時結束):";
cin>>n;
}
return head;
}
void disp(polygon*head)
{
int i,No=1;
cout<
while(head!=NULL)
{
cout<<"第"<
for(i=0;i<=head->n-1;i++)
cout (8) ; head= (9) ; }//Match while statement } void del(polygon*head) { polygon*p; while(head!=NULL) { p= (10) ; head=head->next; delete p->x; delete P->y; deletep; }//Match while statement } void main() { polygon*head; head=create(); disp(head); del(head); }