c语言事业单位笔试,论述题 3 :针对以下 C 语言程序,请按要求回答问题( 18 分)已知 l...

论述题 3 :针对以下 C 语言程序,请按要求回答问题( 18 分)

已知 link.c 源程序如下:

/*link.c 程序对单向链表进行操作 , 首先建立一个单向链表 , 然后根据用户的选择可以对其进行插入节点 、

删除节点和链表反转操作 */

#include

#include

typedef struct list_node *list_pointer; // 定义链表指针

typedef struct list_node{ // 定义链表结构

int data;

list_pointer link;

}list_node;

// 用到的操作函数:

list_pointer create(); // 建立一个单向链表

void insert(list_pointer *p_ptr, list_pointer node); // 在 node 后加入一个新的节点

void delete_node(list_pointer *p_ptr, list_pointer trail, list_pointer node);

// 删除前一个节点是 trail 的当前节点 node

void print(list_pointer ptr); // 打印链表节点中的值

list_pointer invert(list_pointer lead); // 反转链表

int main()

{

list_pointer ptr=NULL;

list_pointer node, trail;

list_pointer *p = &ptr;

int choose, location, i;

printf("you should create a link first:

");

// 建立一个单向链表:

ptr=create(); /* ptr 指向链表的第一个节点 */

print(ptr);

// 根据用户的不同选择进行相应的操作:

printf("input number 0, you can quit the program

");

printf("input number 1, you can insert a new node to link

");

printf("input number 2, you can delete a node from the link

");

printf("input number 3, you can invert the link

");

printf("please input your choice

");

scanf("%d", &choose);

while(choose!=0){

switch(choose){

case 1:

printf("you will insert a node to the link

");

printf("please input the location of the node:

");

scanf("%d", &location);

node = ptr;

i = 1;

while(i

node = node->link;

i++;

}

insert(p, node); /* p 为指向 ptr 的指针 */

print(ptr);

break;

case 2:

printf("you will delete a node from the link

");

printf("please input the location of the node:

");

scanf("%d", &location);

node = ptr;

if(location ==1)

trail = NULL;

trail = ptr;

i = 1;

while(i

trail = trail->link;

i++;

}

node = trail->link;

delete_node(p, trail, node);

print(ptr);

break;

case 3:

printf("you will invert the link

");

ptr = invert(ptr);

print(ptr);

break;

default:

break;

return -1;

}

printf("please input your choice

");

scanf("%d", &choose);

}

return 0;

}

// 根据用户的输入数值建立一个新的单向链表:

list_pointer create()

{

int i, current, length;

list_pointer p1, p2, head;

printf("please input the node number of the link:

");

scanf("%d", &length);

printf("the number of the link is : %d

", length);

printf("please input the data for the link node:

");

i =0;

p1= p2= (list_pointer) malloc(sizeof(list_node));

head = p1;

for(i = 0; i

scanf("%d", ¤ t);

p1->data = current;

p2->link = p1;

p2 = p1;

p1 = (list_pointer) malloc(sizeof(list_node));

}

p2->link = NULL;

return head;

}

……

( 1 )画出主函数 main 的控制流程图。( 10 分)

( 2 ) 设计一组测试用例 , 尽量使 main 函数的语句覆盖率能达到 100% 。 如果认为该函数的语句覆盖率无法达到 100% ,需说明原因。( 8 分)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值