软件设计师教程 数据结构之线性链表的实现 (C/C++语言)
来源:互联网 发布:阿里云深圳机房 编辑:程序博客网 时间:2024/06/01 08:38
#include<stdio.h>
#include<malloc.h> //用于分配地址空间
struct LNode //定义一个结点,结点包含的内容有,结点所存储的数据,以及该节点指向的下一个结点地址
{
int data;
struct LNode *Next;
}*L;
LNode *Initial_List(int n) //初始化链表
{
LNode *head,*p;
head=L=(LNode *)malloc(sizeof (LNode)); //给头结点分配地址空间
for (int i=0;i<n;i++)
{
p=(LNode *)malloc(sizeof (LNode)); //给p分配一个结点
printf("请输入第%d个数:",i+1);
scanf("%d",&p->data);
L->Next=p; //让头结点指向p,使得p为第一个元素
L=p; //再让L(此时的L已经不作为头结点,仅仅是个指针)指向p,便于循环
}
L->Next=NULL;
return head;
}
LNode *FindIndex_List(LNode *L,int index) //查找特定索引,并返回该索引的结点
{
LNode *p;
p=L;
int i=0;
while (p->Next && i<=index ) //当p的下一个结点存在且还未找到对应索引时,循环
{
p=p->Next;
i++;
}
return p; //返回要找索引对应的结点
}
void Insert_List(LNode *L,int k,int InsertNum) //在第K个元素之前插入元素InsertNum
{
LNode *p,*q;
p=L;
q=(LNode *)malloc(sizeof (LNode));
q->data=InsertNum;
int i=1;
while(p->Next && i<k)
{
p=p->Next;
i++;
}
q->Next=p->Next;
p->Next=q;
}
int Delete_List(LNode *L,int k) //删除第k个元素
{
LNode *p,*q;
int i=1,value;
p=L;
q=(LNode *) malloc(sizeof (LNode));
while (p->Next && i<k)
{
p=p->Next;
i++;
}
q=p->Next;
p->Next=q->Next;
value=q->data;
free(q);
return value;
}
void Output_List(LNode *L) //输出列表元素
{
LNode *p;
p=L;
while (p->Next)
{
p=p->Next;
printf(" %d ",p->data);
}
printf("\n");
}
void main()
{
LNode *L=Initial_List(3); //L指向头结点
LNode *p;
printf("元素列表\n");
Output_List(L); //输出列表元素
// p=L->Next ; //p指向第一个元素
// printf("%d,%d,%d",L->Next,p->Next,p->Next->Next); //相邻元素地址相差56
p=FindIndex_List(L,1); //查找第1个索引所在的结点
printf("查找的索引为1结点的值为:%d\n",p->data); //输出查找结点的存储元素
Insert_List(L,1,89);
printf("在第1个元素之前插入元素后的列表元素\n");
Output_List(L); //输出列表元素
int deleteValue=Delete_List(L,2) ;
printf("删除的第2个元素的值为:%d\n",deleteValue);
printf("删除元素后列表元素\n");
Output_List(L);
#include<malloc.h> //用于分配地址空间
struct LNode //定义一个结点,结点包含的内容有,结点所存储的数据,以及该节点指向的下一个结点地址
{
int data;
struct LNode *Next;
}*L;
LNode *Initial_List(int n) //初始化链表
{
LNode *head,*p;
head=L=(LNode *)malloc(sizeof (LNode)); //给头结点分配地址空间
for (int i=0;i<n;i++)
{
p=(LNode *)malloc(sizeof (LNode)); //给p分配一个结点
printf("请输入第%d个数:",i+1);
scanf("%d",&p->data);
L->Next=p; //让头结点指向p,使得p为第一个元素
L=p; //再让L(此时的L已经不作为头结点,仅仅是个指针)指向p,便于循环
}
L->Next=NULL;
return head;
}
LNode *FindIndex_List(LNode *L,int index) //查找特定索引,并返回该索引的结点
{
LNode *p;
p=L;
int i=0;
while (p->Next && i<=index ) //当p的下一个结点存在且还未找到对应索引时,循环
{
p=p->Next;
i++;
}
return p; //返回要找索引对应的结点
}
void Insert_List(LNode *L,int k,int InsertNum) //在第K个元素之前插入元素InsertNum
{
LNode *p,*q;
p=L;
q=(LNode *)malloc(sizeof (LNode));
q->data=InsertNum;
int i=1;
while(p->Next && i<k)
{
p=p->Next;
i++;
}
q->Next=p->Next;
p->Next=q;
}
int Delete_List(LNode *L,int k) //删除第k个元素
{
LNode *p,*q;
int i=1,value;
p=L;
q=(LNode *) malloc(sizeof (LNode));
while (p->Next && i<k)
{
p=p->Next;
i++;
}
q=p->Next;
p->Next=q->Next;
value=q->data;
free(q);
return value;
}
void Output_List(LNode *L) //输出列表元素
{
LNode *p;
p=L;
while (p->Next)
{
p=p->Next;
printf(" %d ",p->data);
}
printf("\n");
}
void main()
{
LNode *L=Initial_List(3); //L指向头结点
LNode *p;
printf("元素列表\n");
Output_List(L); //输出列表元素
// p=L->Next ; //p指向第一个元素
// printf("%d,%d,%d",L->Next,p->Next,p->Next->Next); //相邻元素地址相差56
p=FindIndex_List(L,1); //查找第1个索引所在的结点
printf("查找的索引为1结点的值为:%d\n",p->data); //输出查找结点的存储元素
Insert_List(L,1,89);
printf("在第1个元素之前插入元素后的列表元素\n");
Output_List(L); //输出列表元素
int deleteValue=Delete_List(L,2) ;
printf("删除的第2个元素的值为:%d\n",deleteValue);
printf("删除元素后列表元素\n");
Output_List(L);
}
运行后的结果:
- 软件设计师教程 数据结构之线性链表的实现 (C/C++语言)
- 软件设计师教程 数据结构之循环队列的实现 (C/C++语言)
- 数据结构之---c语言实现线性表的顺序表
- 【数据结构】数据结构C语言的实现(线性表)
- 数据结构C语言之线性表简单实现
- 数据结构c语言实现之顺序线性表
- 数据结构 C语言实现 线性表的链式实现
- 线性表数据结构C语言实现
- 数据结构_1:线性表: C语言实现
- 【C语言 数据结构】 简单线性表实现
- C语言实现数据结构--线性表
- C语言数据结构-线性表-双链表实现
- C语言数据结构之线性表
- C语言数据结构之线性表
- C语言数据结构之线性表(续)
- 数据结构中线性表的c语言代码实现
- C语言数据结构之线性表的基本操作
- 数据结构C语言实现——线性链表
- Linux中创建静态库和动态库
- Transaction in ADO.net 2.0
- ACE第二个练习:简单的Web服务
- zoj 1050 bfs
- QPushButton的应用
- 软件设计师教程 数据结构之线性链表的实现 (C/C++语言)
- 陷入迷途,程序员
- CRC 校验
- 冷静,开发人员的必备
- 新浪微博完整的API文档
- 数据开发-经典sql语句
- [Android实例] 天天动听 悬浮歌词(迷你歌词)效果解读
- 在myeclipse中快速打开Jsp文件
- 第二周