顺序链表
一张顺序表包括以下特征:
有一个唯一的表名来标识该顺序表;
内存单元连续存储,也就是说,一张顺序表要占据一块连续的内存空间;
数据顺序存放,元素之间有先后关系。
1)顺序表的定义:
有两种定义顺序表的方法:一是静态地定义一张顺序表;二是动态地生成一张顺序表
typedef int ElemType;
/***************************************
*The definition of sequential list
****************************************/
typedef struct Sqlist{
ElemType Elem_array[MAX_SIZE];
int length;
}Sqlist;
2)对顺序链表的操作代码如下:
/**************************************
* The basic operation of sequential list
***************************************/
//initialize the sequential list
int init_sqlist(Sqlist *L)
{
L->length = 0;
}
// Create a sequential list
void create_list(Sqlist *L)
{
int i;
printf("Please input the length of the sequential list that want to build:");
scanf("%d",&L->length);
printf("Please input %d elements to the sequential list:\n", L->length);
for(i = 0; i < L->length; i++)
{
scanf("%d", &L->Elem_array[i]);
}
printf("building the sequential list successfully.\n");
system("pause");
}
// Get the length of sequential list
int length_list(Sqlist *L)
{
return L->length;
}
// Judge a sequential list if a null or not
int empty_list(Sqlist *L)
{
if(L->length == 0)
return 1;
else
return 0;
}
//Judge a sequential list is a full.
int full_list(Sqlist *L)
{
if(L->length == MAX_SIZE)
return 1;
else
return 0;
}
//Clear the Sequential list
void clear_list(Sqlist *L)
{
L->length = 0;
}
// Get the element that in the sequential list
int get_list(Sqlist *L, int i)
{
if(empty_list(L) || i > length_list(L) || i < 1)
{
printf("The sequential list is empty or the position is wrong");
return;
}
return L->Elem_array[i - 1];
}
// Print the sequential list
void print_list(Sqlist *L)
{
int i;
if(empty_list(L) == 1)
{
printf("The sequential list is empty!");
return;
}
else
{
for(i = 0; i < L->length; i++)
{
printf("%d\t", L->Elem_array[i]);
}
}
printf("\n");
}
/***********************
* search the element
************************/
// search the element according to position
int locate_list_pos(Sqlist *L, int i)
{
if(i < 1 || i > length_list(L))
{
printf("the position is invalid!");
return 0;
}
return L->Elem_array[i - 1];
}
// search the element according to content.
int locate_list_content(Sqlist *L, ElemType x)
{
int i = 0;
while(i < L->length && L->Elem_array[i] != x)
i++;
if(i < L->length)
return (i + 1);
else
return 0;
}
/*********************************************
* The insert and delete of the sequential list
***********************************************/
// Insert a element in the i position.
void insert_list(Sqlist *L, int i, ElemType x)
{
int j;
if(L->length == MAX_SIZE)
printf("the list full, can't insert!\n");
else if(i < 1 || i > L->length + 1)
printf("The inserted position is invalid!\n");
else
{
for(j = L->length - 1; j >= i - 1; j--)
{
L->Elem_array[j + 1] = L->Elem_array[j];
}
L->Elem_array[i - 1] = x;
L->length++;
}
}
// Delete the i element in the sequential list.
void delete_list(Sqlist *L, int i)
{
int j;
ElemType x;
if(i < 1 || i > L->length)
{
printf("The deleted position is invalid!\n");
}
else
{
x = L->Elem_array[i - 1];
for(j = i; j < L->length; j++)
{
L->Elem_array[j - 1] = L->Elem_array[j];
}
L->length--;
printf("%d is deleted\n",x);
}
}
3)主函数如下:
#include <stdio.h>
#include <stdlib.h>
#include "Seqlist.h"
int main()
{
int p;
Sqlist L;
create_list(&L);
// clear_list(&L);
print_list(&L);
system("pause");
p = locate_list_content(&L, 5);
printf("The position of the element 5 is %d:\n", p);
system("pause");
insert_list(&L, 2, 2);
printf("After insert element:\n");
print_list(&L);
system("pause");
delete_list(&L, 1);
print_list(&L);
system("pause");
return 0;
}
分享到:
相关推荐
数据结构顺序链表的实现数据结构顺序链表的实现数据结构顺序链表的实现数据结构顺序链表的实现数据结构顺序链表的实现数据结构顺序链表的实现
用C++实现的顺序链表,简单易懂,适合初学者
关于顺序链表的初始化,定义指针,插入元素,删除元素和修改头结点,
关于c语言的程序与习题 有关于顺序链表 单链表 双链表 栈等程序,运用相关软件打开并运行,就可以看到效果
1.航班管理员可以——添加航班——查询航班信息——修改航班信息——查询乘客信息(管理员登陆口令:123456) 2.乘客可以——查询航班信息——订票——退票 3.航班链表为单向顺序链表,乘客链表为双向非循环顺序链表
包括链表的正序创建和逆序创建,输出,删除插入一个节点,求链表长度,合并。
销毁线性表,包括释放其占用的空间、链表长度置为0等 3. ClearList(L) 将线性表L重置为空表 4. ListEmpty(L) 判断线性表是否为空 5. ListLength(L) 求线性表的长度 6. GetElem(L,i,&e) 取线性表L中的第i个结点,...
顺序链表.txt
数据结构(顺序表 顺序链表 链队列)里面含有很详细的解释,用c++写的
C++实现顺序链表类模板及测试
用顺序链表,单链表,静态链表三种结构求解的约瑟夫josephus问题,完整的代码,可以直接编译,运行的,
如何建立链表的源代码!简单而且易懂,保证你一看就会。 来看看吧!不要后悔哟!
本题要求实现一个函数,将两个链表表示的递增整数序列合并为一个非递减的整数序列。L1和L2是给定的带头结点的单链表,其结点存储的数据是递增有序的;函数Merge要将L1和L2合并为一个非递减的整数序列。应直接使用原...
学习c++的数据结构,总是不清楚学习的链表要如何才能应用到企业开发中,这里上传了完整的代码 并做了详细的注释
自己写的 因为刚学数据结构 相互交流 本程序适合初学数据结构的同学
1、学习数据结构 用代码实现入门程序。 2、[数据结构(C语言版)].严蔚敏_吴伟民 版代码实现,可直接运行
本文实例讲述了php数据结构之顺序链表与链式线性表。分享给大家供大家参考,具体如下: 链表操作 1、 InitList(L):初始化链表 2、 DestroyList(L):删除连接 3、 ClearList(L):清空链表 4、 ListEmpty(L):...
今天小编就为大家分享一篇关于使用C++实现顺序链表,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
顺序建链表