数据结构基础-线性表

线性表

  1. 定义
  2. 基本操作
  3. 顺序表示
  4. 链式表示

类型

//静态存储
#include<iostream>
#include<stdio.h> 
using namespace std;
#define maxsize 5
typedef int elementtype;
//the define of the static seqList
typedef struct{
	elementtype data[maxsize];
	int length;
}List;
void initList(List &l){
	l.length = 0;
}
void ListInsert(List &l,int i,int e){
	l.length = i+1;
	l.data[i] = e;
}
void print(List &l){
	int i = 0;
	for(i=0;i<l.length;i++){
		cout<<l.data[i]<<" ";
	}
	cout<<"\n---------------\n";
}

int main(){
	List l,ll,lll;
	initList(l);
	initList(ll);
	initList(lll);
	//case 1
	cout<<"case 1:\n";
	ListInsert(l,2,2);
	print(l);
	ListInsert(l,2,4);
	print(l);
	//case 2
	cout<<"case 2:\n";
	ListInsert(ll,1,2);
	print(ll);
	ListInsert(ll,2,4);
	print(ll);
	//case 3
	cout<<"case 3:\n";
	ListInsert(lll,0,2);
	print(lll);
	ListInsert(lll,1,4);
	print(lll);
} 
//动态存储
#include<iostream>
#include<stdio.h>
#include<stdlib.h> 
using namespace std;
#define initsize 5
typedef int elementtype;
//the define of the dynamic seqList
typedef struct{
	elementtype *data;
	int maxsize,length;
}List;
//init 
void initList(List &l){
	l.length = 0;
	l.data=(elementtype *)malloc(initsize * sizeof(elementtype));
	l.maxsize = initsize;
	/*tip:
	malloc函数在stdlib.h头文件中
	*/
}
void ListInsert(List &l,int i,int e){
	l.length = i+1;
	l.data[i] = e;
}
void print(List &l){
	int i = 0;
	for(i=0;i<l.length;i++){
		cout<<l.data[i]<<" ";
	}
	cout<<"\n---------------\n";
}

int main(){
	List l,ll,lll;
	initList(l);
	initList(ll);
	initList(lll);
	//case 1
	cout<<"case 1:\n";
	ListInsert(l,2,2);
	print(l);
	ListInsert(l,2,4);
	print(l);
	//case 2
	cout<<"case 2:\n";
	ListInsert(ll,1,2);
	print(ll);
	ListInsert(ll,2,4);
	print(ll);
	//case 3
	cout<<"case 3:\n";
	ListInsert(lll,0,2);
	print(lll);
	ListInsert(lll,1,4);
	print(lll);
} 

存储结构

顺序表
//顺序表存储
#include<iostream>
#include<stdio.h> 
using namespace std;
#define maxsize 5
typedef int elementtype;
//the define of the static seqList
typedef struct{
	elementtype data[maxsize];
	int length;
}List;
//1.初始化表 
void initList(List &l){
	l.length = 0;
}
//2.求表长 O(1)
int  Length(List &l){
	return l.length;
} 
//3.按值查找 O(n)
int LocateElem(List &l,int e){
	int i = 0;
	for(i = 0; i < l.length; i++){
		if(l.data[i] == e){
			return i+1;
		}
	}
	return 0;
} 
//4.按位查找 O(1)
int GetElem(List &l,int i){
	if(i>=1 && i<=l.length){
		return l.data[i-1];
	}
	return 0;
} 
//5.插入操作 O(n)
/*
	i:		1-5
	length:	0-4
*/
bool ListInsert(List &l,int i,int e){
	if(i<1 || i>l.length+1)
		return false;
	if(l.length >= maxsize)
		return false;
	for(int j = l.length;j>=i;j--)
		l.data[j]=l.data[j-1];
	l.data[i-1] = e;
	l.length++;
	return true;
}
//6.删除操作 O(n)
bool Delete(List &l,int i,elementtype &e){
	if(i<1 || i>l.length)
		return false;
	e = l.data[i-1];
	for(int j =i;j<l.length;j++)
		l.data[j-1] = l.data[j];
	l.length--;
	return true;
}
//7.输出操作 O(n)
void print(List &l){
	int i = 0;
	for(i=0;i<l.length;i++){
		cout<<l.data[i]<<" ";
	}
	cout<<"\n";
}
//8.判断空 O(1)
bool isempty(List &l){
	if(l.length){
		return true;
	}
	return false;
}
//9.销毁操作 
void destroy(List &l){
	
} 

int main(){
	List l;
	initList(l);
	ListInsert(l,1,2);
	print(l);
	ListInsert(l,2,4);
	ListInsert(l,3,5);
	print(l);
	cout<<"find element 4 :"<<LocateElem(l,4)<<endl;
	
	elementtype e;
	Delete(l,2,e);
	cout<<"delete element: "<<e<<endl;
	print(l);
	cout<<"find element 4 :"<<LocateElem(l,4)<<endl;
	
} 
单链表
//单链表存储
#include<iostream>
#include<stdlib.h>
//此头文件含malloc函数 
using namespace std;
#define ElementType int
typedef struct LNode{
	ElementType data;
	struct LNode *next;
}LNode,*LinkList;
//头插法建立链表-有头结点 
LinkList headInsert(LinkList &L){
	LNode *s;
	int x;
	L = (LinkList)malloc(sizeof(LNode));
	L->next = NULL;
	//空表 
	scanf("%d",&x);
	while(x!=0){
		s=(LNode*)malloc(sizeof(LNode));
		s->data=x;
		//结点s 
		s->next=L->next;
		L->next=s;
		scanf("%d",&x);
	}
	return L;
} 
//头插法建立链表-无头结点 
LinkList headInsert_(){
	LNode *s;
	LinkList L; 
	int x;
	L = (LinkList)malloc(sizeof(LNode));
	L = NULL;
	//头指针而不是头结点(空表) 
	scanf("%d",&x);
	while(x!=0){
		s=(LNode*)malloc(sizeof(LNode));
		s->data=x;
		//结点s 
		s->next=L;
		L=s;
		scanf("%d",&x);
	}
	return L;
} 
//尾插法建立链表
LinkList tailInsert(LinkList &L){
	int x;
	L = (LinkList)malloc(sizeof(LNode));
	LNode *s,*tail =L;
	scanf("%d",&x);
	while(x!=0){
		s = (LNode*)malloc(sizeof(LNode));
		s->data = x;
		//结点s
		tail->next = s;
		tail = s;
		scanf("%d",&x);
	} 
	tail->next=NULL;
	return L;
	
} 
//插入构造法:插入时间O(1) 表长n 总时间O(n) 

//序号查找 O(n)
LNode *getElem(LinkList &L,int i){
	LNode *first = L->next;
	if(i == 0) return L;
	if(i < 1) return NULL;
	int j = 1;
	//first为空,或者查找到第i个元素 
	while(first && j < i){
		first = first->next;
		j++;
	}
	return first;
}
//按值查找 O(n)
LNode *locateElem(LinkList &L,ElementType e){
	LNode *p = L->next;
	//为空 或者 找到元素e 
	while(p!=NULL && p->data!=e){
		p=p->next;
	}
	return p;
}
//插入结点 查找结点O(n) 插入O(1) 
bool insertNode(LinkList &L,int i,ElementType e){
	if(i<=0) return false;//错误范围 
	LNode *p = L;
	LNode *s;
	int j = 1;
	while(p!=NULL && j<i){
		p = p->next;
		j++;
	}
	//查找到i-1个元素,同getElem(L,i-1) 
	if(p!=NULL){
		s=(LNode *)malloc(sizeof(LNode));
		s->data = e;
		s->next = p->next;
		p->next = s; 
		return true;
	}
	//没有查找到第i-1个
	return false;
} 
//删除结点 查找O(n) 删除O(1)
bool deleteNode(LinkList &L,int i){
	if(i<=0) return false;
	LNode *p = L;
	int j = 1;
	while(p!=NULL && j<i){
		p = p->next;
		j++;
	}
	//查找到i-1个元素,同getElem(L,i-1) 
	if(p!=NULL){
		p->next = p->next->next; 
		return true;
	}
	//没有查找到第i-1个
	return false;
} 
//求表长 O(N)
int listLength(LinkList &L){
	LNode *p =L->next;
	int j = 0;
	while(p!=NULL){
		p=p->next;
		j++;
	}
	return j;
} 
//print 
void print(LinkList &L){
	LNode *tail;
	tail = L->next;
	while(tail!=NULL){
		cout<<tail->data<<" ";
		tail = tail->next;
	}
}
int main(){
	LinkList L;
	/*
	case 1:
	input: 	1 2 3 4 5
	output: 5 4 3 2 1
	*/
	cout<<"头插法构造:\n";
	headInsert(L);
	//L=headInsert_();
	print(L);
	/*
	case 2:
	input: 	1 2 3 4 5
	output: 1 2 3 4 5
	*/
//	LinkList L1;
//	tailInsert(L1);
//	print(L1);
	cout<<"获取第i个元素e:\n";
	cout<<getElem(L,0)->data<<" "; 
	cout<<getElem(L,1)->data<<" ";
	cout<<"获取元素值e:\n";
	cout<<locateElem(L,3)->data<<" ";
	//cout<<locateElem(L,6)->data<<" ";
	// 5 4 3 2 1中无法查找到6,超出范围,程序运行错误 
	cout<<"插入结点:\n";
	cout<<insertNode(L,6,6)<<" ";
	print(L);
	cout<<"删除结点:\n";
	cout<<deleteNode(L,6)<<" ";
	print(L);
}
双链表
//双链表存储
#include<iostream>
#include<stdlib.h>
#define ElementType int
using namespace std;
typedef struct DNode{
	ElementType data;
	struct DNode *prior,*next;
}DNode,*DlinkList;
//create
DlinkList headInsert(DlinkList &L){
	DNode *s;
	int x;
	L=(DlinkList)malloc(sizeof(DNode));
	L->next = NULL;
	scanf("%d",&x);
	while(x!=0){
		s = (DNode*)malloc(sizeof(DNode));
		s->data = x;
		s->next = L->next;
		L->next->prior = s;
		s->prior = L;
		L->next = s;
		scanf("%d",&x);
	}
	return L;
} 
//print
void print(DlinkList &L){
	DNode *p = L->next;
	while(p!=NULL){
		cout<<p->data<<" ";
		p = p->next;
	}
}
//insert
bool insertNode(DlinkList &L,int i,ElementType e){
	if(i<=0) return false;
	DNode *p = L;
	DNode *s;
	int j = 1;
	while(p && j<i){
		p = p->next;
		j++;
	}
	if(p!=NULL){
		s = (DNode*)malloc(sizeof(DNode));
		s->data = e;
		s->next = p->next;
		p->next->prior = s;
		s->prior = p;
		p->next = s;
	}
	return false;
} 
//delete
bool deleteode(DlinkList &L,int i){
	if(i<=0) return false;
	DNode *p = L;
	int j = 1;
	while(p && j<i){
		p = p->next;
		j++;
	}
	if(p!=NULL){
		p->next->next->prior = p;
		p->next = p->next->next;
	}
	return false;
} 
int main(){
	DlinkList L;
	headInsert(L);
	print(L);
	//不详细设计用例测试了 
}

其他

内容链接
绪论上一篇博客
线性表正在翻阅此文章
栈和队列下一篇博客:栈和队列
树和二叉树下一篇博客:树和二叉树
下一篇博客:图
查找下一篇博客:查找
排序下一篇博客:排序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值