线性表
定义:线性表是具有相同数据类型的n个数据元素的有限序列。其中n为表长,当n=0时,线性表为空表。
特点:
1.表中的元素个数有限。
2.表中的元素具有逻辑上的顺序性,表示元素的先后顺序。
3.除了第一个元素外,每个元素有且仅有一个直接前驱;除了最后一个元素外,每个元素有且仅有一个直接后驱。
4.每个元素的数据类型相同。
5.仅讨论元素间的逻辑关系,不考虑元素是什么具体内容。
线性表基本操作
- InitList(&L) 初始化表
- Length(L) 求表长
- LocateElem(L,e) 按值查找
- GetElem(L,i) 按位找值
- ListInsert(&L,i,e) 插入操作
- ListDelete(&L,i,&e) 删除操作
- PrintList(L) 输出操作
- Empty(L) 判空操作
- DestoryList(&L) 销毁操作
线性表的顺序表示——顺序表
特点:1.顺序表中元素的逻辑顺序和物理顺序相同。2.表中任一元素都可以随机存储。
实现:
#include<stdio.h>
#include<stdlib.h>
#define Size 10
typedef struct{
int *head;
int MaxSize;
int length;
}seqList;
void initList(seqList &L){
L.head=(int*)malloc(Size*sizeof(int));
if(!L.head){
printf("动态分配内存失败");
exit(0);
}
L.MaxSize=Size;
L.length=0;
}
void input(seqList &L){
for(int i=0;i<L.MaxSize;i++){
L.head[i]=rand()%100;
L.length++;
}
}
void display(seqList L){
for(int i=0;i<L.length;i++){
printf("%d\n",L.head[i]);
}
}
int Length(seqList L){
return L.length;
}
void ListInsert(seqList &L,int pos,int e){
if(pos<1||pos>L.length+1){
printf("插入位置不合理,插入失败");
}
if(L.length>=L.MaxSize){
L.head=(int*)realloc(L.head,(L.MaxSize+1)*sizeof(int));
}
for(int i=L.length;i>=pos;i--){
L.head[i]=L.head[i-1];
}
L.head[pos-1]=e;
L.length++;
}
void ListDelete(seqList &L,int pos,int &e){
if(pos<1||pos>L.length+1){
printf("删除位置不合理,删除失败");
}
e=L.head[pos-1];
for(int i=pos;i<L.length;i++){
L.head[i-1]=L.head[i];
}
L.length--;
}
int LocateElem(seqList L,int e){
for(int i=0;i<L.length;i++){
if(L.head[i]==e){
return i+1;
}
}
}
bool Empty(seqList L){
if(L.length==0){
return true;
}
return false;
}
void DestoryList(seqList &L){
if(L.head){
free(L.head);
}
}
int GetElem(seqList L,int pos){
return L.head[pos-1];
}
线性表的链式表示——链表
单链表
实现:
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
LinkList HeadInsertLinkList(LinkList &L){
L=(LinkList)malloc(sizeof(LNode));
int x;
LNode *s;
scanf("%d",&x);
L->next=NULL;
while(x!=9999){
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
s->next=L->next;
L->next=s;
scanf("%d",&x);
}
return L;
}
LinkList ToilInsertLinkList(LinkList &L){
L=(LinkList)malloc(sizeof(LNode));
int x;
LNode *s,*temp=L;
scanf("%d",&x);
L->next=NULL;
while(x!=9999){
s=(LNode*)malloc(sizeof(LNode));
s->data=x;
temp->next=s;
temp=s;
scanf("%d",&x);
}
s->next=NULL;
return L;
}
LNode *getElem(LinkList L,int pos){
LNode *p=L;
int j=0;
if(pos<1){
return NULL;
}
while(p!=NULL&&j<pos){
p=p->next;
j++;
}
return p;
}
LNode *getElem2(LinkList L,int pos){
LNode *p=L->next;
int j=1;
if(pos<1){
return NULL;
}
if(pos==0){
return L;
}
while(p!=NULL&&j<pos){
p=p->next;
j++;
}
return p;
}
bool ListInsert(LinkList &L,int pos,int e){
if(i<1){
return false;
}
LNode *p=getElem(L,i-1);
if(p==NULL){
return false;
}
LNode *s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
LNode *LocateElem(LinkList L,int e){
LNode *p=L->next;
while(p!=NULL&&p->data!=e){
p=p->next;
}
return p;
}
int Length(LinkList L){
int len=0;
LNode *p=L;
while(p->next!=NULL){
p=p->next;
len++;
}
return len;
}
bool InsertLinkListWithHead(LinkList &L,int pos,int e){
if(pos<1){
return false;
}
LNode *p=L;
int j=0;
while(p!=NULL&&j<pos-1){
p=p->next;
j++;
}
if(p==NULL){
return false;
}
LNode *s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return s;
}
bool ListInsertWithoutHead(LinkList &L,int pos,int e){
if(pos<1){
return false;
}
if(pos==1){
LNode *s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=L;
L=s;
return true;
}
LNode *p=L;
int j=1;
while(p!=NULL&&j<pos-1){
p=p->next;
j++;
}
if(p==NULL){
return false;
}
LNode *s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
bool InsertNextNode(LNode *p,int e){
if(p==NULL){
return false;
}
LNode *s=(LNode*)malloc(sizeof(LNode));
if(s==NULL){
return false;
}
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
bool InsertPriorNode(LNode *p,int e){
if(p==NULL){
return false;
}
LNode *s=(LNode*)malloc(sizeof(LNode));
if(s==NULL){
return false;
}
s->next=p->next;
p->next=s;
s->data=p->data;
p->data=e;
return true;
}
bool ListDelete(LinkList &L,int pos,int &e){
if(i<1){
return false;
}
LNode *p=L;
int j=0;
while(p!=NULL&j<pos-1){
p=p->next;
j++;
}
if(p==NULL)
return false;
if(p->next=NULL)
return false;
LNode *q=p->next;
e=q->data;
p->next=q->next;
free(q);
return true;
}
bool DeleteNode(LNode *p){ //不考虑删除的结点是最后一个结点的情况
if(p==NULL){
return false;
}
LNode *q=p->next;
p->data=q->data;
p->next=q->next;
free(q);
return true;
}
双链表
实现:
#include<stdio.h>
#include<stdlib.h>
typedef struct DNode{
int data;
struct DNode *prior,*next;
}DNode,*DLinkList;
//初始化双链表:
bool InitDLinkList(DLinkList &L){
L=(DNode*)malloc(sizeof(DNode));
if(L==NULL){
return false
}
L->prior=NULL;
L->next=NULL;
return true;
}
//判断双链表是否为空
bool Empty(DLinkList L){
if(L->next=NULL){
return true;
}
else
return false;
}
//双链表的插入:P结点之后插入S结点
bool InsertNextDNode(DNode *p,DNode *s){
if(p==NULL||s==NULL){
return false;
}
s->next=p->next;
if(p->next!NULL)
p->next->prior=s;
s->prior=p;
p->next=s;
}
//删除P结点的后继结点
bool DeleteNextDNode(DNode *p){
if(p==NULL)
return false;
DNode *q=p->next;
if(q==NULL)
return false;
p->next=q->next;
if(q->next!=NULL)
q->next->prior=p;
free(q);
return true;
}
void DestoryList(DLinkList &L){
while(L->next!=NULL){
DeleteNextDNode(L);
}
free(L);
L=NULL;
}
//往后遍历
void traversal1(DNode *p){
while(p!=NULL){
p=p->next
}
}
//往前遍历
void traversal2(DNode *p){
while(p!=NULL){
p=p->prior;
}
}
//往前遍历,跳过头结点
void traversal3(DNode *p){
while(p->prior!=NULL){
p=p->prior;
}
}
循环链表
循环单链表
实现:
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
bool InitList(LinkList &L){
L=(LNode*)malloc(sizeof(LNode));
if(L==NULL){
return false;
}
L->next=L;
return true;
}
bool Empty(LinkList L){
if(L->next==L){
return true;
}
else
return false;
}
bool isTail(LinkList L,LNode *p){
if(p->next==L){
return true;
}
else
return false;
}
循环双链表
实现:
#include<stdio.h>
#include<stdlib.h>
typedef struct DNode{
int data;
struct DNode *prior,*next;
}DNode,*DLinkList;
bool InitList(DLinkList &L){
L=(LNode*)malloc(sizeof(LNode));
if(L==NULL){
return false;
}
L->prior=L;
L-next=L;
}
bool Empty(DLinkList L){
if(L->next==L){
return true;
}
else
return false;
}
bool isTail(DLinkList L,DNode *p){
if(p->next==L){
return true;
}
else
return false;
}
bool InsertList(DNode *p,DNode *s){
s->next=p->next;
p->next->prior=s;
s->prior=p;
p->next=s;
return true;
}
bool DeleteList(DNode *p){
DNode *q=p->next;
p->next=q->next;
p->next->prior=p;
free(q);
return true;
}
静态链表
略