地址:http://www.cppblog.com/0and1/ | E-Mail:firenight@163.com | QQ:79688942 |
作业描述:
第一次作业(仅一题)


一、作业要求:
1、你必须写两个文件,文件名命名统一如下:

(1)list.h //头文件。
(2)main.c //主函数。
2、头文件中除了所有函数声明,还包含以下内容:
typedef struct student
{
char first_name[10];
char last_name[10];
char student_id[12]; //学生学号
char gender; //m or f
int age;
} Data;


typedef struct list_node {
Data d;
struct list_node *next;
} Node;

3、除了main函数以外,main.c文件中还要求包含以下五个函数:
Node *create_list(Data *array,int size) //用动态的方法建立五个节点,该函数返回链表的头指针。
void print_list(Node *head) //打印,打印出所有节点的函数,要求有适当的换行。
int count_list(Node *head) //统计节点数。
void insert_node(Node *p1,Node *p1,Node *q) //插入一个节点。
void delete_node(Node *head,Node *p) //删除一个节点。
void clear_list(Node *head); //删除所有的节点,并且用free释放空间。

4、主函数的要求:
(1)用静态的方法建立三个节点,节点数据如下:
first_name last_name student_id gender age
-------------------------------------------------------
Jacky Chen N3060101101 m 19
Jacky Zhang N3060101102 f 18
Jay zhou N3060101103 m 20
然后调用print_list和count_list函数。
(2)用create_list建立一个linked list(链表),节点数据从文件data.txt中读取,然后调用print_list和count_list函数。


(3)用insert_node函数在名字为lixiang zhang和shuang huang的节点之间插入一个节点,名字分别是Andy Liu,然后调用 print_list和count_list函数验证是否已经插入节点。

(4)用delete_node函数删除名字为dongmei wang的节点,同样调用print_list和count_list函数验证是否已经删除该节点。

提示:1.从data.txt中读数据到节点中用fscanf函数。
2.对有能力的同学不要用fsanf函数,写一个函数,每次从文本文件中读一行到buffer中,然后再从buffer里面把每
个字提取出来,功能相当于与fscanf。
3.在调用create_list之前先建立一个数组,将所有值向节点的指针放到buffer数组中,然后再从buffer中读取。
注意:任何时候调用print_list之前,之后,必须调用printf("\n")换行!

DATA.TXT
xiang chen N3060110201 m 20
lixiang zhang N3060110202 m 19
shuang huang N3060110203 f 19
xiaoming zhao N3060110204 m 18
dongmei wang N3060110205 f 21
我的想法:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
//#include <list.h>

typedef struct student
{
char first_name[10];
char last_name[10];
char student_id[12]; //学生学号
char gender; //m or f
int age;
} Data;


typedef struct list_node {
Data d;
struct list_node *next;
} Node;

Node *create_list(Data arr[],int size); //用动态的方法建立五个节点,该函数返回链表的头指针。
void print_list(Node *head); //打印,打印出所有节点的函数,要求有适当的换行。
int count_list(Node *head); //统计节点数。
void insert_node(Node *p1,Node *p2,Node *q); //插入一个节点。
void delete_node(Node *head,Node *p); //删除一个节点。
void clear_list(Node *head); //删除所有的节点,并且用free释放空间。


Data arr_static[3] = {{"Jacky","Chen","N3060101101",'m',19},
{"Jacky","Zhang","N3060101102",'f',18},
{"Jay","zhou","N3060101103",'m',20}};

void main()
{
//静态建立并输出
Node *head,*tail,*temp;
head=NULL;
tail=NULL;
for (int i=0;i<3;i++)
{
temp = (Node *)malloc(sizeof(Node));//建立新节点
//向新节点中添入内容
strcpy(temp->d.first_name,arr_static[i].first_name);
strcpy(temp->d.last_name,arr_static[i].last_name);
strcpy(temp->d.student_id,arr_static[i].student_id);
temp->d.gender = arr_static[i].gender;
temp->d.age = arr_static[i].age;
if(head==NULL)
head = temp;//如果链表不存在 新节点便是头节点
else tail->next = temp;//将新节点链入链尾
tail = temp;//改变尾指针
}
if(tail!=NULL)
tail->next = NULL;
printf("静态建立并输出,按回车继续 \n");
getchar();
print_list(head);
printf("\n总共有 %d 个记录\n",count_list(head));
clear_list(head);

//动态建立并输出
FILE *fp;
Data arr[5];
Node *records;
fp = fopen("c:\data.txt","r");
for (i=0;i<5;i++)
{
fscanf(fp,"%s%s%s%s%d",&arr[i].first_name,&arr[i].last_name,&arr[i].student_id,&arr[i].gender,&arr[i].age);
}
fclose(fp);
records = create_list(arr,5);
printf("\n动态建立并输出,按回车继续 \n");
getchar();
printf("\n");
print_list(records);
printf("\n总共有 %d 个记录\n",count_list(records));

//插入节点并验证
Node *p1,*p2,*q;
q = (Node *)malloc(sizeof(Node));
strcpy(q->d.first_name,"Andy"); //对插入节点赋值
strcpy(q->d.last_name,"Liu");
strcpy(q->d.student_id,"N3060101999");
q->d.gender = 'f';
q->d.age = 55;
q->next = NULL;
temp = records;
while (temp!=NULL) //查找
{
if (!strcmp(temp->d.first_name,"lixiang") && !strcmp(temp->d.last_name,"zhang") || !strcmp(temp->d.first_name,"shuang") && !strcmp(temp->d.last_name,"huang"))
{
p1 = temp;
p2 = temp->next;
break;
}
else
temp = temp->next;
}
insert_node(p1,p2,q);
printf("\n插入节点并验证,按回车继续 \n");
getchar();
printf("\n");
print_list(records);
printf("\n总共有 %d 个记录\n",count_list(records));

//删除节点
temp = records;
while (temp!=NULL) //查找
{
if (!strcmp(temp->d.first_name,"dongmei") && !strcmp(temp->d.last_name,"wang"))
break;
else
temp = temp->next;
}
delete_node(records,temp);
printf("\n删除节点并验证,按回车继续 \n");
getchar();
printf("\n");
print_list(records);
printf("\n总共有 %d 个记录\n",count_list(records));

//释放指针
clear_list(records);
printf("\n\n程序结束,所有指针已释放 \n");
getchar();

}

Node *create_list(Data arr[],int size) //用动态的方法建立五个节点,该函数返回链表的头指针。
{
Node *head,*tail,*temp;
head=NULL;
tail=NULL;
for (int i=0;i<size;i++)
{
temp = (Node *)malloc(sizeof(Node));
strcpy(temp->d.first_name,arr[i].first_name);
strcpy(temp->d.last_name,arr[i].last_name);
strcpy(temp->d.student_id,arr[i].student_id);
temp->d.gender = arr[i].gender;
temp->d.age = arr[i].age;
if(head==NULL)
head = temp;//如果链表不存在 新节点便是头节点
else tail->next = temp;//将新节点链入链尾
tail = temp;//改变尾指针
}
if(tail!=NULL)
tail->next = NULL;
return head;
}

void print_list(Node *head) //打印,打印出所有节点的函数,要求有适当的换行
{
Node *p;
p = head;
printf("姓\t名\t 学号\t\t 性别\t年龄\n");
for (int i=0;i<50;i++)
printf("-");
printf("\n");
while (p!=NULL)
{
printf("%s\t%-11s%s\t %c\t %d\n",p->d.last_name,p->d.first_name,p->d.student_id,p->d.gender,p->d.age);
p = p->next;
}
for (i=0;i<50;i++)
printf("-");
}

int count_list(Node *head) //统计节点数
{
Node *p;
int i=0;
p = head;
while (p!=NULL)
{
i++;
p = p->next;
}
return i;
}
void insert_node(Node *p1,Node *p2,Node *q) //插入一个节点
{
p1->next = q;
q->next = p2;
}

void delete_node(Node *head,Node *p) //删除一个节点
{
Node *temp;
temp = head;
if (p==head)
head = head->next;
else
{
while (temp->next!=p)
temp = temp->next;
if (p->next==NULL)
temp->next = NULL;
else
temp->next = p->next;
}
}

void clear_list(Node *head)
{
Node *p;
while (head!=NULL)
{
p = head;
head = head->next;
free(p);
}
}
|