#define MaxCol 10
//單鏈表中數據節點類型
typedef struct Node1{
ElemType data[MaxCol];
struct Node1 *next;
} DList;
//頭結點類型
typedef struct Node2{
int Row,Col;
DList *next;
} HList;
//交互式創建單鏈表算法
void CreateTable(HList *&h){
int i,j;
DList *r,*s;
h=(HList *)malloc(sizeof(HLIst)); //創建頭結點
h->next=NULL; //頭結點後置位爲空
printf("表的行數和列數:");
scanf("%d%d",&h->Row,&h->Col); //輸入表的行數和列數
for(int i=0;i<h->Row;i++){ //輸入表的所有數據
printf("第%d行:",i+1);
s=(DList *)malloc(sizeof(DList));//創建數據節點
for(j=0;j<h->Col;j++) //輸入一行的數據
scanf("%d",&s->data[j]);
if(h->next==NULL)
h->next=s; //插入第一個數據節點
else
r->next=s; //插入其他數據節點,將*s指向*r節點之後
r=s; //r始終指向尾節點
}
r->next=NULL; //尾節點next域
}
void DestroyTable(HList *&h){
DList *pre=h->next;
DList *p=pre->next;
while(p!=NULL){
free(pre);
pre=p;
p=p->next;
}
free(pre);
free(h);
}
void DisplayTable(HList *h){
int j;
DList *p=h->next;
while(p!=NULL){
for(int j=0;j<h->Col;j++){
printf("%d",p->data[j]);
}
printf("\n");
p=p->next;
}
}