线性表
来源:互联网 发布:js float 保留两位小数 编辑:程序博客网 时间:2024/06/14 21:45
一、线性表可分为顺序表和链表
顺序表:逻辑相邻,物理也相邻
链表:逻辑相邻但物理可能不相邻
在seqlist.h这个头文件里:
#pragma once //防止头文件被重复引用
//固定长度的顺序表
#define SIZE 10 //顺序表的长度
typedef struct SeqList
{
int elem[SIZE];//存放数据的数组
int length;//有效数据的个数
}SeqList,*PSeqList;
接着,在seqlist.cpp这个文件里:
#include <assert.h>
#include "seqlist.h" //一定要记得引用外来的这个
{
assert(ps != NULL);
if(ps == NULL)
{
return ;
}
}
{
return ps->length == SIZE;
}
{
if(pos<0 || pos>ps->length || IsFull(ps))//判断不合法的pos值
{
return false;
}
{
ps->elem[i+1] = ps->elem[i];
}
ps->elem[pos] = val; //将新的值插入
return true;
}
{
for(int i=0;i<ps->length;i++)
{
if(ps->elem[i] == key)
{
return i;
}
}
return -1;
}
{
int index = Search(ps,key);
if(index < 0)
{
return false;
}
/*for(int i=index;i<ps->length-1;i++)
{
ps->elem[i] = ps->elem[i+1];
}
return DeletePos(ps,index);
}
{
if(pos<0 || pos>=ps->length)//
{
return false;
}
{
ps->elem[i] = ps->elem[i+1];
}
}
{
return ps->length;
}
{
return ps->length == 0;
}
{
ps->length = 0; //(*ps).length = 0;,[],->
}
{
Clear(ps);
}
{
for(int i=0;i<ps->length;i++)
{
printf("%d ",ps->elem[i]);
}
printf("\n");
}
bool GetElem(PSeqList ps,int pos,int *rtval)
{
if(pos<0 || pos>=ps->length)
{
return false;
}
*rtval = ps->elem[pos];
}
测试用例:
int main()
{
Node head;//
InitList(&head);
for(int i=0;i<16;i++)
{
//Insert_head(&head,i);
Insert_tail(&head,i);//0 1 2 3 4 5
}
Insert_head(&head,1);
Insert_head(&head,2);
Insert_head(&head,3);
Insert_head(&head,4);
Show(&head);
//Reverse(&head);
//Show(&head);
Unique(&head);
Show(&head);
/*int val;
GetElem(&head,3,&val);
printf("%d\n",val);*/
Destroy(&head);
Destroy(&head);
return 0;
}
/*
#include <string.h>
int main()
{
printf("");
strlen("abc");
return 0;
}
*/
/*
struct Node
{
int data;
int *next;
};
int main()
{
Node na = {1,NULL};//
Node nb = {2,NULL};
Node nc = {3};
na.next = &nb.data;//int *,nb
na.next
return 0;
}
*/
#include "dseqlist.h"
int main()
{
DSeqList ds;//12
InitSeqList(&ds);
for(int i=0;i<15;i++)
{
Insert(&ds,i,i);
}
Show(&ds);
return 0;
}
/*
#include "seqlist.h"
int main()
{
SeqList s;//44
InitSeqList(&s);
for(int i=0;i<15;i++)
{
Insert(&s,i,i);
}
Show(&s);
return 0;
}
*/
- 线性表--线性存储
- 线性表 线性结构
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 线性表
- 模板方法模式
- Java 中的 static 使用之静态方法
- lua脚本学习8
- Asp.net Core 2.0+EntityFrameWorkCore 2.0添加数据迁移
- Codeforces Round #439 (Div.2)
- 线性表
- MOOS例程HelloWorld-详细注释
- 2017 10 08 NOIP2017模拟赛
- 数据结构顺序表实现
- 10个重要的Linux ps命令实战
- lua脚本学习9
- 内部类反序列化问题(fastjson exception: create instance error)
- 使用VS Code编译运行C/C++程序
- 在CentOS7上安装MongoDB