|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
struct test
{
char book[128];
char title[128];
struct test *next;
};
void initialize(struct test *node);
struct test *list_node(struct test *head);
void addbook(struct test **head);
void release(struct test **head);
void printdata(struct test *head);
void getInput(struct test *node);
void node_delete(struct test **node);
void insert(struct test **head, struct test *node);
int main()
{
struct test *head = NULL;
struct test *pnode = NULL;
head = (struct test *)malloc(sizeof(struct test));
head->next = NULL;
pnode = head;
char ch;
do
{
addbook(&pnode);
getchar();
printf("是否继续:");
ch=getchar();
getchar();
}while (ch == 'Y' || ch == 'y');
printdata(head);
printf("=-----------------------------\n");
node_delete(&pnode);//pnode指向尾节点,删除尾结点出错//
printdata(head);
printf("=-----------------------------\n");
release(&head);
printdata(head);
printf("=-----------------------------\n");
}
void getInput(struct test *node)
{
if (node == NULL)
{
printf("NULL");
exit(-1);
}
else
{
printf("请输入书名:");
scanf("%s", node->book);
printf("请输入作者:");
scanf("%s", node->title);
}
}
void printdata(struct test *head)
{
struct test *node = NULL;
if (head == NULL)
{
printf("链表为空");
exit(-1);
}
else
{
node = head->next;
while (node != NULL)
{
printf("书名:");
printf("%s\n", node->book);
printf("作者:");
printf("%s\n", node->title);
node = node->next;
}
}
}
void release(struct test **head)
{
struct test *temp = NULL;
while (*head!=NULL)
{
temp = *head;
*head = temp->next;
free(temp);
temp = NULL;
}
}
void node_delete(struct test **node)
{
struct test *temp = NULL;
if (&node == NULL)
{
printf("结点为空");
exit(-1);
}
else
{
temp = *node;
*node = temp->next;
free(temp);
temp = NULL;
}
}
void addbook(struct test **pnode)
{
struct test *node;
node = (struct test *)malloc(sizeof(struct test));
if (node == NULL)
{
printf("failed");
exit(-1);
}
else
{
initialize(node);
getInput(node);
insert(pnode, node);
}
}
void insert(struct test **pnode,struct test *node)
{
struct test *temp = NULL;
if (*pnode != NULL)
{
temp = *pnode;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
node->next = NULL;
*pnode = node;
}
else
{
*pnode= node;
node->next = NULL;
}
}
void initialize(struct test *node)
{
int i = 0;
for (i; i < 128; i++)
{
node->book[i] = NULL;
node->title[i] = NULL;
}
}
struct test *list_node(struct test *head)
{
struct test *temp = NULL;
temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
return temp;
} |
|