/*
copyright(c)2012 三江学院 电子信息工程
* 文件名称:use.c
* 文件标识:见配置管理计划书
* 摘要:创建链表,查找结点,删除结点,插入结点,输出链表
* 当前版本:1.1
* 作者:黄路
* 完成日期:2012年6月30日
*/
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
STU *head = NULL;//表头
STU *temp = NULL;//临时结点指针
int num = 0;//目的编号
STU add;//等待插入的结点
head = CreatList();
temp = head;
//输出
OutputList(temp);
//查找
printf("\n searching:>>> \n");
printf("which number ?\n");
scanf("%d", &num);
temp = SearchNode(head, num);
if (temp != NULL)
printf("%-20s #%d\n", temp->name, temp->num);
else
printf("no #%d!\n", num);
//删除
printf("\n after deleting:>>> \n");
head = DeleteNode(head, num);
OutputList(head);
//插入
printf("\n inserting:>>> \n");
printf("name,number:\n");
scanf("%s %d", add.name, &add.num);
head = InsertNode(head, add);
OutputList(head);
//free
free(head);
head = NULL;
temp = NULL;
return 0;
}
/*
copyright(c)2012 三江学院 电子信息工程
* 文件名称:list.h
* 文件标识:见配置管理计划书
* 摘要:常量定义和声明,结构体定义和声明,函数声明
* 当前版本:1.1
* 作者:黄路
* 完成日期:2012年6月30日
*/
#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
//常量的定义和声明
//编译器会对const 常量进行安全监察,而对define定义的常量只做字符替换
//可以对外公开的常量
#define M 20 //字符串的长度
//结构体的定义和声明
typedef struct stu
{
char name[M];
int num;
struct stu *next;
}STU;
//函数声明
STU *CreatList(void);
void OutputList(STU *h);
STU *SearchNode(STU *h, int num);
STU *DeleteNode(STU *h, int num);
STU *InsertNode(STU *h, STU add);
#endif // LIST_H_INCLUDED
/*
copyright(c)2012 三江学院 电子信息工程
* 文件名称:list.c
* 文件标识:见配置管理计划书
* 摘要:(简要描述本文件的内容)函数定义,包括创建链表,
查找结点,删除结点,插入结点,输出链表 。
* 当前版本:1.1
* 作者:黄路
* 完成日期:2012年6月30日
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "list.h"
//不想对外公开的:常量
#define N 5 //链表的长度
/*
************************************
函数的实现
************************************
*/
/*
创建链表
输入:void
返回:STU *h 或者NULL
设计思路:从链表尾部开始,不断在前面插入新结点
*/
STU *CreatList(void)
{
STU *h = NULL;
STU *move = NULL;
int i = 0;
for (i = 1; i < N + 1; i++)
{
move = (STU *)malloc(sizeof(STU));
//memory check
if (NULL == move)
{
printf("no memory!\n");
return NULL;
}
printf("name?\n");
scanf("%s", move->name); //获得姓名
move->num = i; //编号
//指针前移
move->next = h;
h = move;
}
return h;
}
/*
输出链表
递归法
输入:STU *h
*/
void OutputList(STU *h)
{
assert(h != NULL);
if (h->next != NULL)
{
OutputList(h->next);
}
printf("%-20s #%4d\n", h->name, h->num);
}
/*
查找
输入:STU *h, int num;
输出:STU *p
*/
STU *SearchNode(STU *h, int num)
{
/*
检查h的有效性
int assert(int expression);
若expression 为真,返回 1,程序继续执行;
若为假,返回0,程序终止。
*/
assert(h != NULL);
STU *p = h;//指针必须初始化!!!!
while (NULL != p)
{
if (p->num == num)
{
return p;//返回查找结点 对应的指针
}
p = p->next;
}
return NULL;
}
/*
删除
输入:STU *h, int num
输出:STU *p
bug:无法插入到尾部
*/
STU *DeleteNode(STU *h, int num)
{
assert(h != NULL);
STU *p = h;
STU *q = h->next;
//被删数 位于表头
if (p->num == num)
{
h = h->next;
free(p);
return h;
}
while (q != NULL)
{
if(q->num == num)
{
p->next = q->next;
free(q);
return h;
}
p = q;
q = q->next;
}
return NULL;
}
STU *InsertNode(STU *h, STU add)
{
assert(h != NULL);
STU *p;
STU *q;
STU *neu;//新结点
neu = (STU *)malloc(sizeof(STU *));
strcpy(neu->name, add.name);
neu->num = add.num;
//插在表头
if (neu->num > h->num)// 5 4 3 2 1
{
neu->next = h;
h = neu;
return h;
}
p = h;
q = p->next;
//中间和尾部
while (q != NULL)
{
if ((p->num - neu->num) == 1)
{
p->next = neu;
neu->next = q;
return h;
}
p = q;
q = q->next;
if (q == NULL)
{
p->next = neu;
neu->next = q;
return h;
}
}
return h;
}