资源简介
C与语言实现链表的创建、插入(头插法、尾插法)、遍历、查找、删除操作
代码片段和文件信息
#include
#include
/*定义结构体*/
typedef struct stu{
int num;
struct stu *next;
}STU*PSTU;
/*遍历链表的实现*/
void show(PSTU head)
{
if(head == NULL){
printf(“There is no data!\n“);
}
else{
/*当前结点部位空时,打印信息*/
for(; head != NULL; head = head->next){
printf(“%d -> “head->num);
}
printf(“NULL\n“);
}
}
/*创建链表函数的实现*/
PSTU Create(PSTU head)
{
int n = 0;
PSTU tail = NULL; /*定义尾指针并初始化为空*/
printf(“You should input the numbersand you can input ‘-1‘ to stop create link_table!\n“);
while(1){
PSTU new = (PSTU)malloc(sizeof(STU));
printf(“Please input the number: “);
scanf(“%d“&n);
if(n == -1){
free(new);
printf(“create over!\n“);
return head;
}
new->num = n;
new->next = NULL;
if(head == NULL){ /*当头结点为空时,当前结点设为头结点,并使tail指针指向头结点*/
head = new;
tail = new;
}
else{ /*当不是头结点时,在尾部插入数据*/
tail->next = new;
tail = new;
}
}
return head;
}
/*头插法插入数据 的实现*/
PSTU Insert_from_head(PSTU head)
{
PSTU new = (PSTU)malloc(sizeof(STU));
int n = 0;
printf(“Please input the insert number n = “);
scanf(“%d“&n);
new->num = n;
new->next = NULL;
if(head == NULL){
head = new;
}
else{
new->next = head->next;
head->next = new;
}
return head;
}
/*尾插法插入数据 的实现*/
PSTU Insert_from_tail(PSTU head)
{
PSTU tail = NULL;
PSTU new = (PSTU)malloc(sizeof(STU));
int n = 0;
printf(“Please input the insert number n = “);
scanf(“%d“&n);
new->num = n;
new->next = NULL;
if(head == NULL){
head = new;
}
else{
tail = head;
/*将尾指针指向最后一个结点*/
for(; tail->next != NULL; tail = tail->next);
tail->next = new;
}
return head;
}
/*查找函数的实现*/
void Find(PSTU head)
{
PSTU ptr = NULL;
int n = 0;
printf(“Please input the number you will find: “);
scanf(“%d“&n);
if(head == NULL){
printf(“There is no data!\n“);
}
else{
ptr = head;
/*当当前结点的数据与查找的数据不相符时移向下一个结点*/
for(; (ptr->num != n) && (ptr != NULL); ptr = ptr->next);
if(ptr == NULL){
printf(“Can‘t find !\n“);
}
else{
printf(“find!\nThe number is :%d\n“ptr->num);
}
}
}
/*删除结点函数的实现*/
PSTU Delete(PSTU head)
{
PSTU ptr = NULL;
PSTU pf = NULL;
int n = 0;
printf(“Please input the number you will delete :“);
scanf(“%d“&n);
if(head == NULL){
printf(“There is no data!\n“);
}
else{
ptr = head;
for(; (ptr->num != n) && (ptr != NULL); ptr = ptr->next){
pf = ptr;
}
if(ptr == NULL){
printf(“The number is no in the link_table !\n“);
}
else{
pf->next = ptr->next;
free(ptr);
}
}
printf(“now The link_table is :“);
show(head);
return head;
}
int main(void)
{
PSTU head = NULL;
int choose = 0;
/*简易的菜单*/
while(1){
printf(“\t\t-----------------Menu-------------------\n“);
printf(“\t\t-------------- 1. Create ------------ \n“);
printf(“\t\t-------------- 2. Insert from head -- \n“);
printf(“\t\t-------------- 3. Insert from tail -- \n“);
printf(“\t\t-------------- 4. Find -------------- \n“);
printf(“\t\t-------------- 5. Delte ------------- \n“);
printf(“ 属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2013-09-13 16:39 链表操作\
文件 4199 2013-09-13 16:38 链表操作\li
- 上一篇:VxWorks串口编程代码
- 下一篇:c++编写的简单的汇编器
相关资源
- 链表的基本操作.c
- 链表实现学生管理系统(main.c)
- c++ 单链表
- c语言课程设计-职工信息管理系统-单
- MFC实现的红绿灯程序
- 线性表用链表实现学生信息系统
- 职工信息管理系统C 链表
- 一个异质链表类的实现
- ldpc编译码c代码
- 链表栈的基本操作(C语言
- 基于循环双向链表的大整数计算器c
- 单向链表类模板全C++
- C语言实现上机账户管理系统 VS 2010-
- C语言链表应用的完整版ATM提款机
- 单链表c语言实现增删改查操作
- MFC航空客运订票系统
- 数据结构王红梅
- 一元多项式加法乘法运算C+链表
- mfc-一元稀疏多项式计算器
- c语言制作歌词解析源程序
- 学生成绩管理系统用c++ 链表 结构体
- C语言实现 学生信息管理系统 双链表
- c语言期末大作业,控制台实现图书馆
- C语言学生管理系统项目
- 双向链表的基本操作C语言
- C语言双向链表基本操作
- 用C++链表结构实现多项式的加法,乘
- 线性表的基础训练 链表
- nmea-0183协议解析C语言链表实现
- C++实现数据结构算法
川公网安备 51152502000135号
评论
共有 条评论