当前位置: 首页> 娱乐> 明星 > 数据结构-线性表

数据结构-线性表

时间:2025/7/26 9:04:25来源:https://blog.csdn.net/m0_73777202/article/details/140353315 浏览次数:1次

一 线性表

线性表

组成

顺序存储结构:数组

链式存储结构:链表
单链表
静态链表
循环链表
双向链表

操作

1 初始化
2 增
3 删
4 改
5 遍历
6 取出

顺序存储结构-数组:
#include <iostream>using namespace std;typedef struct {int *p;int data;
}L;
void  Make(L &l,int *a,int n)//创建数组 
{l.data = n;l.p = new int [20];for(int i = 0;i < n;i++)l.p[i] = a[i];
}void Read(L &l)//遍历  
{for(int i = 0;i < l.data;i++)cout << l.p[i] << " ";cout<<endl;
}
bool Add(L &l,int loc,int num)//增加数据 
{if(loc < 0 || loc > l.data)//特殊条件 {cout << "out of boundary" << endl;return false;}for(int j = l.data - 1;j >= loc - 1;j --)//移动 {l.p[j + 1] = l.p[j];}l.p[loc - 1] = num;l.data++;//数组数据个数增加 return true; 
} bool Delete(L &l,int loc)//删除数据
{if(loc < 0 || loc > l.data)//特殊条件 {cout << "out of boundary" << endl;return false;}for(int i = loc;i < l.data ;i++)//移动位置 {l.p[i - 1] = l.p[i];}l.data--;//数组数据个数减少 return true;
} void Change(L &l,int loc,int num)
{if(loc < 0 || loc > l.data)//特殊条件 {cout << "out of boundary" << endl;return false;}l.p[loc - 1] = num;return true;
} void Destroy(L &l)//销毁数组 
{delete [] l.p;
} 
int main()
{int n;cin >> n;int i,a[n];for(i = 0;i < n;i++)cin >> a[i];L l;Make(l,a,n);//初始化 Read(l);int m,p;cout<<"add location and data:"<<endl;cin >> m >> p; Add(l,m,p);//增加 Read(l);int loc;cout<<"What do you want to delete? Please input it's location:'"<<endl;cin>>loc;Delete(l,loc);//删除 Read(l);int loca,num;cout<<"What do you want to change?input the location and data:"<<endl;cin>>loca>>num;Change(l,loca,num);//改变 Read(l);Destroy(l);//销毁 return 0;
} 
关键字:数据结构-线性表

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: