您的位置:首页 > 其它

线性表的链式表示与实现

2012-05-19 10:30 190 查看
#include<iostream>
#include<malloc.h>
using namespace std;

typedef int ElemType;
typedef int Status;
#define OK 1
#define ERROR 0

typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;

/*逆位序输入n个元素的值,建立带表头结点的单链线性表L*/
void CreateList_L(LinkList &L,int n)
{
L=(LinkList)malloc(sizeof(LNode));
L->next=NULL;
for(int i=0;i!=n;i++)
{
LinkList p=(LinkList)malloc(sizeof(LNode));
cin>>p->data;
p->next=L->next;
L->next=p;
}
}

/*当第i个元素存在时,其值赋给e并返回ok,否则返回error*/
Status GetElem_L(LinkList &L,int i,ElemType &e)
{
LinkList p=L->next;
int n=1;
while(n!=i&& p!=NULL)
{
p=p->next;
n++;
}
if(!p) return ERROR;
e=p->data;
return OK;
}

/*在带头结点的单链表中,删除第i个元素,并由e返回其值*/
Status ListDelete_L(LinkList &L,int i,ElemType &e)
{
LinkList p=L->next;
LinkList q=L;
int k=1;
while(p!=NULL && k!=i)
{
k++;
q=p;
p=p->next;

}
if(p==NULL) return ERROR;
e=p->data;
q->next=p->next;
free(p);
return OK;
}

/*将两个有序链表合并为一个有序链表*/
/*La和Lb的元素按值非递减排列*/
/*在归并两个链表为一个链表时,不需要另建新表的结点空间,而只需要
将原来两个链表中的结点之间的关系解除,重新按元素值非递减的关系将所有结点连接成一个链表即可*/
void MergeList_L(LinkList &La,LinkList &Lb,LinkList &Lc)
{
LinkList pa=La->next;
LinkList pb=Lb->next;
LinkList pc=La;
Lc=pc;
while(pa && pb)
{
if(pa->data<=pb->data)
{
pc->next=pa;
pc=pa;
pa=pa->next;
}
else
{
pc->next=pb;
pc=pb;
pb=pb->next;
}
}
if(pa) pc->next=pa;
if(pb) pc->next=pb;
}

void print(LinkList &L)
{
LinkList p=L->next;
cout<<"头指针L->头结点->";
while(p!=NULL)
{
cout<<p->data<<"->";
p=p->next;
}
cout<<"NULL"<<endl;
}

int main(){
/*cout<<"逆序创建一个链表,input n:";
int n;
cin>>n;
LinkList L;
CreateList_L(L,n);
print(L);
ElemType e;
if(GetElem_L(L,3,e))
cout<<e<<endl;
else
cout<<"doesn't exist!"<<endl;

cout<<"删除链表L的第3个位置的元素:";
if(ListDelete_L(L,3,e))
{

cout<<e<<endl;
cout<<"删除之后为:";
print(L);
}
else
cout<<"doesn't exist!"<<endl;*/
int n;
cout<<"逆序创建一个链表(非递减),input n:";
cin>>n;
LinkList L1;
CreateList_L(L1,n);
print(L1);
cout<<"逆序创建一个链表(非递减),input n:";
cin>>n;
LinkList L2;
CreateList_L(L2,n);
print(L2);
cout<<"合并前面两个链表(非递减),input :";
LinkList L3;
MergeList_L(L1,L2,L3);
print(L3);

return 0;
}


#include<iostream>
#include<malloc.h>
using namespace std;

typedef int ElemType;
typedef int Status;
#define OK 1
#define ERROR 0
#define MAX_SIZE 100

/*-------线性表的静态单链表存储结构----------*/
typedef struct{
ElemType data;
int cur;
}component,SLinkList[MAX_SIZE];

/*为了辨明数组中哪些分量未被使用,解决的办法是将所有未被使用过以及被删除的分量
用游标链成一个备用的链表,每当进行插入时,便可从备用链表上取得第一个结点作为待插入的新结点
反之,在删除时将从链表中删除下来的结点链接到备用链表上*/

/*将整个数组空间初始化成一个链表*/
void InitSpace_SL(SLinkList &space)
{
/*将一维数组space中各分量链成一个备用链表,space[0].cur为头指针,0表示空指针*/
for(int i=0;i!=MAX_SIZE-1;i++)
space[i].cur=i+1;
space[MAX_SIZE-1].cur=0;
}

int Malloc_SL(SLinkList &space)
{
/*若备用空间链表非空,则返回分配的结点下标,否则返回0*/
int i=space[0].cur;
if(space[0].cur)
space[0].cur=space[i].cur;/*记录备用空链表的第一个结点*/
return i;
}

void Free_SL(SLinkList &space,int k)
{
/*将下标为k的空闲结点回收到备用链表*/
space[k].cur=space[0].cur;
space[0].cur=k;
}

void CreateList_SL(SLinkList &space,int n)
{
InitSpace_SL(space);
int S=Malloc_SL(space);//生成头结点
int r=S;//r指向S的当前最后结点
int m;
for(int i=0;i!=n;i++)
{
m=Malloc_SL(space);
cin>>space[m].data;
space[r].cur=m;
r=m;
}
space[r].cur=0;
}

/*在静态单链线性表L中,查找第1个值为e的元素
若找到,则返回它在L中的位序,否则返回0.*/
int LocateElem_SL(SLinkList space,ElemType e)
{
int i=space[1].cur;
while(i && space[i].data!=e)
i=space[i].cur;
if(i==0)
return 0;
return i-1;
}

void print_SL(SLinkList space)
{
int i=space[1].cur;
while(i)
{
cout<<space[i].data<<"->";
i=space[i].cur;
}
cout<<"NULL"<<endl;
}

/*删除一个指定位置的元素*/
Status SListDelete_SL(SLinkList &space,int i,ElemType &e)
{
int k=space[1].cur;
int r=k-1;
int count=1;
while(k && count!=i)
{
r=k;
k=space[k].cur;
count++;
}
if(k==0)
return ERROR;
e=space[k].data;
space[r].cur=space[k].cur;
Free_SL(space,k);
return OK;
}

/*在指定的位置插入元素*/
Status SListInsert_SL(SLinkList &space,int i,ElemType e)
{
int k=space[1].cur;
int count=1;
int r=k-1;
while(k && count!=i)
{
r=k;
k=space[k].cur;
count++;
}
if(k==0)
return ERROR;
int t=Malloc_SL(space);
space[t].data=e;
space[r].cur=t;
space[t].cur=k;
return OK;
}

int main(){
SLinkList space;
cout<<"请输入4个整数:";
CreateList_SL(space,4);
print_SL(space);
cout<<"在上述链表中,找到第一个等于2的数的位置:";
int t;
if(t=LocateElem_SL(space,2))
cout<<t<<endl;
else
cout<<"不存在!"<<endl;
cout<<"将上述位置的元素删除后:";
int e;
if(SListDelete_SL(space,t,e))
print_SL(space);
else
cout<<"ERROR!"<<endl;
cout<<"将该删除的元素还原后:";
if(SListInsert_SL(space,t,e))
print_SL(space);
else
cout<<"ERROR!"<<endl;
return 0;
}


#include<iostream>
#include<stdlib.h>

using namespace std;

typedef int ElemType;
typedef int Status;
#define OK 1
#define ERROR 0
#define MAX_SIZE 100

/*---------线性表的双向链表存储结构-------*/
typedef struct DuLNode{
ElemType data;
struct DuLNode *prior;
struct DuLNode *next;
}DuLNode,*DuLinkList;

/*创建双向链表*/
void CreateDuL(DuLinkList &L,int n)
{
L=(DuLinkList)malloc(sizeof(DuLinkList));
L->prior=L;
L->next=L;
DuLinkList t,p;
p=L;
for(int i=0;i!=n;i++)
{
t=(DuLinkList)malloc(sizeof(DuLinkList));
cin>>t->data;
p->next=t;
L->prior=t;
t->prior=p;
t->next=L;
p=t;
}
}

/*在带头结点的双链循环线性表L中第i个位置之前插入元素e*/
Status ListInsert_DuL(DuLinkList &L,int i,ElemType e)
{
DuLinkList p=L->next;
int count=1;
while(p!=L && count!=i)
{
p=p->next;
count++;
}
if(count==i)
{
DuLinkList t=(DuLinkList)malloc(sizeof(DuLinkList));
t->data=e;
t->next=p;
t->prior=p->prior;
p->prior->next=t;
p->prior=t;
return OK;
}
return ERROR;
}

/*删除带头结点的双链循环线性表L的第i个元素*/
Status ListDelete_DuL(DuLinkList &L,int i,ElemType &e)
{
DuLinkList p=L->next;
int count=1;
while(p!=L && count!=i)
{
count++;
p=p->next;
}
if(p==L)
return ERROR;
e=p->data;
p->prior->next=p->next;
p->next->prior=p->prior;
//free(p);/*----------free的时候出错,留待以后解决--------------*/
return OK;
}

/*输出双向链表*/
void print_DuL(DuLinkList L)
{
DuLinkList p=L->next;
cout<<"L->头结点->";
while(p!=L)
{
cout<<p->data<<"->";
p=p->next;
}
cout<<"NULL"<<endl;
}

int main(){
DuLinkList L;
cout<<"输入4个整数:";
CreateDuL(L,4);
cout<<"输出链表:";
print_DuL(L);

//	cout<<"在位置3之前,插入元素3后的链表为:";
//	ListInsert_DuL(L,3,3);
cout<<"在位置5之前,插入元素5后的链表为:";
ListInsert_DuL(L,5,5);
print_DuL(L);

cout<<"删除上述操作后:";
ElemType e;
if(ListDelete_DuL(L,5,e))
print_DuL(L);
else
cout<<"ERROR!"<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: