C語言結構與串列程式求解~?

2017-06-05 10:02 pm
1.寫出一個程式,可以儲存的結構,並可以手動輸入或放入陣列讀取。
(1)英文姓名,至多10個單字
(2)年齡,至多100歲
(3)血型,預定設計A,B,O,AB

2.承第一題,利用第一題結構,建立一個具備以下功能的鍊結串列程式。
(1)新增,新增後列印串列
(2)刪除,刪除後列印串列
(3)搜尋,搜尋後列印串列

回答 (2)

2017-06-26 3:45 pm
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct data
{
char Name[11];
int Age;
char Blood[2];
//---initial
data(char *name, int age, char *blood)
{
Age = age;
strcpy(Name,name);
strcpy(Blood,blood);
}
void show()
{;
if(!strcmp(Name,"姓名")) { printf("%10s\t年齡\t血型\n","姓名"); printf("%6s----\t----\t----\n","");}
else printf("%10s\t%2d歲\t%2s型\n",Name,Age,Blood);
}
bool operator == (data n)
{ return( !strcmp(Name,n.Name) && !(Age - n.Age) && !strcmp(Blood,n.Blood) ) ; }
};
struct node
{
//---link bound
node *Head = NULL;
node *Tail = NULL;
//---link move
node *Next = NULL;
//---data
data *D;
node(data *d)
{
D = d;
Head = Tail = Next = NULL;
}
void init_point(node* head) { Head = Tail = head; }
//--- new
void add(data *d)
{
node* temp = new node(d);
Tail->Next = temp;
Tail = temp;
printf("已新增:"); temp->D->show();
}
//-- remove
void remove(data *d)
{
bool del_flag = true ;
node* search = Head ;
while(search->Next != NULL)
{
if( *(search->Next->D) == *d )
{
node* temp = search->Next;
search->Next = temp->Next;
printf("已刪除:");
temp->D->show();
delete temp;
del_flag = false;
break;
}
search = search->Next;
}
if(del_flag) { printf("找不到:"); d->show(); }
}
//---find
void find(char* name)
{
bool del_flag = true ;
node* search = Head;
while(search->Next != NULL)
{
if( !strcmp( search->Next->D->Name ,name) )
{
search->Next->D->show();
del_flag = false;
}
search = search->Next;
}
if(del_flag) { printf("找不到: %s",name); }
}
//---list
void show()
{
int count = -1;
node* search = Head ;
while(search != NULL)
{
search->D->show();
search = search->Next;
count++;
}
printf("%30s共%d人\n","",count);
}
//---
~node() { delete D; }
};
int main(void)
{
data *label = new data((char*)"姓名",0,(char*)"0");
node *list = new node(label);
list->init_point(list);
// 陣列
char Name[][11] = { "jack", "cyndi", "wynde", "WoW", "Q_Q"};
int Age[] = { 3 , 17, 12, 61, 27};
char Blood[][3]= { "O", "A", "A", "AB", "B"};
// 陣列輸入
for(int i=0;i<sizeof(Age)/sizeof(int);i++)
{
data *new_one = new data(Name[i],Age[i],Blood[i]);
list->add(new_one);
}
// 操作
int type = 0;
char name[11]; int age; char blood[3];
data *new_one;
while(1)
{
printf("\n=========================================\n");
printf("(1)新增 (2)刪除 (3)搜尋 (4)顯示清單: ");
scanf(" %d",&type);
switch(type)
{
case 1:
printf("選擇新增資料輸入\n");fflush(stdin);
printf("名稱:");gets(name);
printf("年齡:");scanf(" %d",&age);
printf("血型:");scanf(" %s",&blood);
new_one = new data(name,age,blood);
list->add(new_one);
break;
case 2:
printf("選擇刪除資料輸入\n");fflush(stdin);
printf("名稱:");gets(name);
printf("年齡:");scanf(" %d",&age);
printf("血型:");scanf(" %s",&blood);
new_one = new data(name,age,blood);
list->remove(new_one);
break;
case 3:
printf("選擇搜尋資料輸入\n");fflush(stdin);
printf("名稱:");scanf(" %s",&name);
list->find(name);
break;
case 4: list->show(); break;
}
}
list->show();
system("pause");
return 0;
}
2017-06-06 10:46 am
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct data
{
char Name[11];
int Age;
char Blood[2];
//---initial
data(char *name, int age, char *blood)
{
Age = age;
strcpy(Name,name);
strcpy(Blood,blood);
}
void show()
{;
if(!strcmp(Name,"姓名")) { printf("%10s\t年齡\t血型\n","姓名"); printf("%6s----\t----\t----\n","");}
else printf("%10s\t%2d歲\t%2s型\n",Name,Age,Blood);
}
bool operator == (data n)
{ return( !strcmp(Name,n.Name) && !(Age - n.Age) && !strcmp(Blood,n.Blood) ) ; }
};
struct node
{
//---link bound
node *Head = NULL;
node *Tail = NULL;
//---link move
node *Next = NULL;
//---data
data *D;
node(data *d)
{
D = d;
Head = Tail = Next = NULL;
}
void init_point(node* head) { Head = Tail = head; }
//--- new
void add(data *d)
{
node* temp = new node(d);
Tail->Next = temp;
Tail = temp;
printf("已新增:"); temp->D->show();
}
//-- remove
void remove(data *d)
{
bool del_flag = true ;
node* search = Head ;
while(search->Next != NULL)
{
if( *(search->Next->D) == *d )
{
node* temp = search->Next;
search->Next = temp->Next;
printf("已刪除:");
temp->D->show();
delete temp;
del_flag = false;
break;
}
search = search->Next;
}
if(del_flag) { printf("找不到:"); d->show(); }
}
//---find
void find(char* name)
{
bool del_flag = true ;
node* search = Head;
while(search->Next != NULL)
{
if( !strcmp( search->Next->D->Name ,name) )
{
search->Next->D->show();
del_flag = false;
}
search = search->Next;
}
if(del_flag) { printf("找不到: %s",name); }
}
//---list
void show()
{
int count = -1;
node* search = Head ;
while(search != NULL)
{
search->D->show();
search = search->Next;
count++;
}
printf("%30s共%d人\n","",count);
}
//---
~node() { delete D; }
};
int main(void)
{
data *label = new data((char*)"姓名",0,(char*)"0");
node *list = new node(label);
list->init_point(list);
// 陣列
char Name[][11] = { "jack", "cyndi", "wynde", "WoW", "Q_Q"};
int Age[] = { 3 , 17, 12, 61, 27};
char Blood[][3]= { "O", "A", "A", "AB", "B"};
// 陣列輸入
for(int i=0;i<sizeof(Age)/sizeof(int);i++)
{
data *new_one = new data(Name[i],Age[i],Blood[i]);
list->add(new_one);
}
// 操作
int type = 0;
char name[11]; int age; char blood[3];
data *new_one;
while(1)
{
printf("\n=========================================\n");
printf("(1)新增 (2)刪除 (3)搜尋 (4)顯示清單: ");
scanf(" %d",&type);
switch(type)
{
case 1:
printf("選擇新增資料輸入\n");fflush(stdin);
printf("名稱:");gets(name);
printf("年齡:");scanf(" %d",&age);
printf("血型:");scanf(" %s",&blood);
new_one = new data(name,age,blood);
list->add(new_one);
break;
case 2:
printf("選擇刪除資料輸入\n");fflush(stdin);
printf("名稱:");gets(name);
printf("年齡:");scanf(" %d",&age);
printf("血型:");scanf(" %s",&blood);
new_one = new data(name,age,blood);
list->remove(new_one);
break;
case 3:
printf("選擇搜尋資料輸入\n");fflush(stdin);
printf("名稱:");scanf(" %s",&name);
list->find(name);
break;
case 4: list->show(); break;
}
}
list->show();
system("pause");
return 0;
}


收錄日期: 2021-05-01 19:50:16
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20170605140219AA8wgB0

檢視 Wayback Machine 備份