您的位置:首页 > 理论基础 > 数据结构算法

复习 [数据结构] ---- 线性表

2008-09-19 10:11 246 查看
/*-------------------------------------

顺序表插入删除

-------------------------------------*/

#include <stdio.h>

#define MaxLen 50

typedef char ElemType;

typedef struct

{

ElemType data[MaxLen];

int Len;

}Sqlist;

void setnull(Sqlist &L) //置空表

{ L.Len=0; }

int length(Sqlist L) //求长度

{ return L.Len; }

ElemType get(Sqlist L,int i) //取表中第i个元素

{

if( i < 0 || i > L.Len-1 )

printf("位置参数不正确/n");

else

return (L.data[i]);

}

int Locate(Sqlist L,ElemType x) //查找表中x元素

{

int i=0;

while( i < L.Len && L.data[i] != x)

i++;

if( i == L.Len)

return -1;

else

return i;

}

int insnode(Sqlist &L,ElemType x,int i) //插入元素

{

int j;

if( i<0 || i>L.Len)

return 0;

else

{

L.Len++;

for( j=L.Len-1 ; j>i ; j--)

L.data[j]=L.data[j-1];

L.data[j]=x;

return 1;

}

}

int delnode(Sqlist &L,int i) //删除元素

{

int j;

if( i<0 || i>L.Len-1)

return 0;

else

{

for( j=i ; j<L.Len-1 ; j++)

L.data[j]=L.data[j+1];

L.Len--;

return 1;

}

}

void display(Sqlist L)

{

int j;

printf("顺序表:");

if(L.Len==0)

printf("空表/n");

else

{

if(L.Len==1)

printf("%c",L.data[0]);

else

{

for(j=0;j<L.Len-1;j++)

printf("%c-> ",L.data[j]);

printf("%c",L.data[j]);

}

printf("/n");

}

}

void main()

{

Sqlist S;

setnull(S); //初始化存入 ABCD

insnode(S,'A',0);

insnode(S,'B',1);

insnode(S,'C',2);

insnode(S,'D',3);

printf("查找元素位置:%d/n",Locate(S,'D'));

printf("取表中地3个元素:%c/n",get(S,3));

display(S);

insnode(S,'E',0); //插入元素

display(S);

delnode(S,0); //删除元素

display(S);

}

/*-------------------------------------

链式表插入删除

-------------------------------------*/

#include <stdio.h>

#include <stdlib.h>

#define ElemType char

typedef struct node //定义单链表节点类型

{

ElemType data; //数据域

struct node *next; //指针域

}SNode;

void CreateListF(SNode *&L,ElemType a[],int n) //头插法建表

{

SNode *s;int i;

L=(SNode *)malloc(sizeof(SNode)); //创建头节点

L->next=NULL;

for( i=0 ; i<n ; i++)

{

s=(SNode *)malloc(sizeof(SNode)); //创建新节点

s->data=a[i];

s->next=L->next; //接链

L->next=s;

}

}

void CreateListR(SNode *&L,ElemType a[],int n) //尾插法建表

{

SNode *s,*r;int i;

L=(SNode *)malloc(sizeof(SNode)); //创建头节点

L->next=NULL;

r=L; //r L同时指向头节点

for( i=0 ; i<n ; i++)

{

s=(SNode *)malloc(sizeof(SNode)); //创建新节点

s->data=a[i];

r->next=s;

r=s;

}

r->next=NULL;

}

void setnull(SNode *&p) //置空表

{

p=(SNode *)malloc(sizeof(SNode)); //创建节点

p->next=NULL;

}

int length(SNode *p) //求链表长度

{

int n=0;

SNode *q=p->next;

while(q!=NULL)

{

n++;

q=q->next;

}

return n;

}

SNode *get(SNode *p,int i)

{

int j=0;

SNode *q=p->next;

while( j<i && q!=NULL)

{

q=q->next;

j++;

}

if(q!=NULL)

return q;

else

{

printf("位置参数i不正确/n");

return NULL;

}

}

int locate(SNode *p,ElemType x) //查找数据域为X的节点的位置

{

int i=0;

SNode *q=p->next;

while(q!=NULL && q->data!=x)

{

q=q->next;

i++;

}

if(q==NULL)

return -1;

else

return i;

}

int insnode(SNode *p,ElemType x,int i)

{

SNode *s,*q;

s=(SNode *)malloc(sizeof(SNode)); //建立要插入的节点S

s->data=x;

if(i==0) //*s作为首元素的节点

{

s->next=p->next; //将*s插入到*p之后

p->next=s;

}

else

{

q=get(p,i-1); //查找第i-1个节点*q

if(q==NULL) //位置参数不正确

return 0; //返回出错信息

else

{

s->next=q->next; //将*s插入到*q之后

q->next=s; //断链

}

}

return 1; //返回成功信息

}

int delnode(SNode *p,int i)

{

SNode *q,*t;

if(i==0) //*s作为首元素的节点

{

t=p->next;

p->next=t->next;

free(t);

}

else

{

q=get(p,i-1); //查找第i-1个节点*q

if(q==NULL) //位置参数不正确

return 0; //返回出错信息

else

{

t=q->next; //t指向*q之后的节点既被删节点

q->next=t->next; //从链表中删除*t

free(t); //释放t所指内存空间

}

}

return 1; //返回成功信息

}

void display(SNode *p) //显示链表内容

{

int n=length(p),i; //n为链表长度

SNode *q=p->next; //q指向首节点

printf("单链表显示:");

if(n==0)

printf("空表");

else if(n==1) //只有一个元素

printf("%c",q->data);

else

{

for(i=1;i<n;i++)

{

printf("%c-> ",q->data);

q=q->next;

}

printf("%c",q->data);

}

printf("/n");

}

void main()

{

char array[4]={'A','B','C','D'};

SNode *S;

CreateListF(S,array,4); //建立链表

printf("链表长度为:%d/n",length(S)); //显示链表长度

display(S);

insnode(S,'E',0); //插入操作

display(S);

delnode(S,0); //删除操作

display(S);

}

/*-------------------------------------

双链表插入删除

-------------------------------------*/

#include <stdio.h>

#include <stdlib.h>

#define ElemType char

typedef struct node

{

ElemType data; //数据域

struct node *prior; //指向前驱节点的指针

struct node *next; //指向直接后续节点的指针

}DNode;

void setnull(DNode *&p) //p为引用型指针

{

p=(DNode *)malloc(sizeof(DNode)); //创建节点

p->next=p->prior=NULL;

}

int length(DNode *p) //计算双链表长度

{

int n=0;

DNode *q=p->next;

while(q!=NULL)

{

n++;

q=q->next;

}

return n;

}

DNode *get(DNode *p,int i)//取双链表第i个节点的地址

{

int j=0;

DNode *q=p->next;

while( j<i && q!=NULL)

{

q=q->next;

j++;

}

if(q!=NULL)

return q;

else

{

printf("位置参数i不正确/n");

return NULL;

}

}

int locate (DNode *p,ElemType x)//按值查找

{

int i=0;

DNode *q=p->next;

while( q!=NULL && q->data!=x) //查找data域为x的第一个节点

{

q=q->next;

i++;

}

if(q==NULL) //未找到

return -1;

else

return i; //找到

}

int insnode(DNode *p,ElemType x,int i) //在第i个位置插入新的元素

{

DNode *s,*q;

s=(DNode *)malloc(sizeof(DNode)); //建立要插入的节点S

s->data=x;

if(i==0) //*s作为首元素的节点

{

s->next=p->next; //将*s插入到*p之后

if(p->next!=NULL)

p->next->prior=s;

s->prior=p;

p->next=s;

}

else

{

q=get(p,i-1); //查找第i-1个节点*q

if(q==NULL) //位置参数不正确

return 0; //返回出错信息

else

{

s->next=q->next; //将*s插入到*q之后

if(q->next!=NULL)

q->next->prior=s;

s->prior=q;

q->next=s; //断链

}

}

return 1; //返回成功信息

}

int delnode(DNode *p,int i) //删除节点

{

DNode *q,*s;

if(i==0) //删除首元素的节点

{

s=p->next;

p->next=s->next;

if(s->next!=NULL)

s->next->prior=p;

free(s);

}

else

{

q=get(p,i-1); //查找第i-1个节点*q

if(q==NULL) //位置参数不正确

return 0; //返回出错信息

else

{

s=q->next; //s指向*q之后的节点既被删节点

q->next=s->next; //从链表中删除*s

if(s->next!=NULL)

s->next->prior=q;

free(s); //释放s所指内存空间

}

}

return 1; //返回成功信息

}

void display(DNode *p) //显示链表内容

{

int n=length(p),i;

DNode *q=p->next;

printf("双链表显示:");

if(n==0)

printf("空表");

else if(n==1) //只有一个元素

printf("%c",q->data);

else

{

for(i=1;i<n;i++)

{

printf("%c-> ",q->data);

q=q->next;

}

printf("%c",q->data);

}

printf("/n");

}

void main()

{

DNode *L;

setnull(L);

insnode(L,'A',0);

insnode(L,'B',1);

insnode(L,'C',2);

insnode(L,'D',3);

display(L);

delnode(L,3);

display(L);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: