成语大全网 - 汉语词典 - c语言链表实现词典

c语言链表实现词典

顺序存储的线性表的算法

#include "stdio.h"

#include "stdlib.h"

#define Status int

#define OVERFLOW 0

#define TRUE 1

#define FALSE 0

#define OK 1

#define MAXSIZE 100

typedef int ElemType;

typedef struct list

{ElemType elem[MAXSIZE];

int length;

}SqList;

void InitList(SqList &L){

L.length=0;

}

/*建立顺序表*/

void CreateList(SqList &L)

{

int i;

printf("input the length");

scanf("%d",&L.length);//输入表长

for(i=1;i<=L.length;i++)

scanf("%d",&L.elem[i-1]);//输入元素

}

//顺序表的遍历

void printdata(ElemType e){

printf("%4d",e);

}

void Traverse(SqList L,void (*visit)(ElemType e))

{ int i;

printf("The elements of the lists are:\n");

for(i=1;i<=L.length;i++){

if (i%10==0)printf("\n");//每行显示10个元素

visit(L.elem[i-1]);//输出表中元素

}

printf("\n");

}

//有序顺序表L中插入元素e使序列仍有序

void Insert(SqList &L,ElemType e)

{int i,j;

if (L.length==MAXSIZE)exit(OVERFLOW);//表满,不能插入

for(i=1;i<=L.length&&L.elem[i-1]<=e;i++);//向后查找

for(j=L.length;j>=i;j--)

L.elem[j]=L.elem[j-1];//元素后移

L.elem[i-1]=e;//插入e

L.length=L.length+1;//表长加1

}

//建立递增有序的顺序表

void CreateList_Sorted(SqList &L)

{int i,num;

ElemType e;

L.length=0;

printf("Create a sorted list,Input the length of the list\n");

scanf("%d",&num);

printf("Input the data %d numbers\n",num);

for(i=1;i<=num;i++){

scanf("%d",&e);

Insert(L,e);

}

}

/*Merge two sorted lists*/

void MergeList(SqList La,SqList Lb,SqList &Lc)

{int *pa,*pb,*pc;

if (La.length+Lb.length>MAXSIZE) exit(OVERFLOW);

else

{pa=La.elem;pb=Lb.elem;pc=Lc.elem;

while (pa<La.elem+La.length&&pb<Lb.elem+Lb.length)

*pc++=(*pa<=*pb)?*pa++:*pb++;/*公***部分合并*/

while (pa<La.elem+La.length) *pc++=*pa++;

/*R1表的剩余部分放到R的后部*/

while (pb<Lb.elem+Lb.length) *pc++=*pb++;

/*R2表的剩余部分放到R的后部*/

Lc.length=La.length+Lb.length;/*R表长*/

}

}

//判断元素是否对称,对称返回TRUE 否则返回FALSE

Status Symmetric(SqList L)

{int low,high;

low=0;

high=L.length-1;

while(low<high)

if (L.elem[low]==L.elem[high]){low++;high--;}

else return(FALSE); return(TRUE); }

//顺序表的主函数部分

//#include "seqlist.h"

void main()

{SqList L1,L2,L;

int select;

ElemType e;

do{printf("\n1 insert 2 merge");

printf("\n3 symmetric 0 exit \n");

printf("Please select(0--3)\n");

scanf("%d",&select);

switch(select){

case 1:

InitList(L);

CreateList_Sorted(L);

Traverse(L,printdata);

printf("\nInput the element of inserted\n");

scanf("%d",&e);

Insert(L,e);

Traverse(L,printdata);

break;

case 2:

InitList(L1);

CreateList_Sorted(L1);

Traverse(L1,printdata);

InitList(L2);

CreateList_Sorted(L2);

Traverse(L2,printdata);

InitList(L);

MergeList(L1,L2,L);

Traverse(L,printdata);

break;

case 3:

InitList(L);

CreateList(L);

Traverse(L,printdata);

if (Symmetric(L)) printf("Yes!\n"); else printf("Not\n");

break;

case 0:break;

default:printf("Error! Try again!\n");

}

}while(select);

}

/*单向链表的有关操作示例*/

/*类型定义及头文件部分,文件名为sllink.h*/

#include <stdio.h>

#include <stdlib.h>

typedef int ElemType;//元素实际类型

typedef struct LNode{

ElemType data;

struct LNode *next;

}LNode,*LinkList; //定义结点、指针类型名

//头插法建立无序链表

void CreateList(LinkList &L){

LinkList p;

ElemType e;

L=(LinkList)malloc(sizeof(LNode));

L->next=NULL;

printf("头插法建立链表,以0结束\n");

scanf("%d",&e);

while(e){

p=(LinkList)malloc(sizeof(LNode));

p->data=e;

p->next=L->next;

L->next=p;

scanf("%d",&e);

}

}

/*非递减有序单向链表L插入元素e序列仍有序*/

void Insert_Sort(LinkList &L,ElemType e){

LinkList p,s;

s=(LinkList)malloc(sizeof(LNode));

s->data=e;

p=L;

while(p->next&&p->next->data<=e)

p=p->next;/*查找插入位置*/

s->next=p->next; /*插入语句*p结点后插入*s结点*/

p->next=s;

}

/*建立递增有序的单向链表*/

void Create_Sort(LinkList &L){

ElemType e;

L=(LinkList)malloc(sizeof(LNode));

L->next=NULL;

printf("建立有序表,输入任意个整型数据以0结束\n");

scanf("%d",&e);

while(e){

Insert_Sort(L,e);

scanf("%d",&e);

}

}

/*单向链表的遍历*/

void Traverse(LinkList L){

LinkList p;

printf("遍历链表");

for(p=L->next;p;p=p->next)

printf("%5d",p->data);

printf("\n");

}

/*在单向链表删除元素e*/

void Delete(LinkList &L,ElemType e){

LinkList p,q;

p=L;

q=L->next;

while(q&& q->data!=e){//查找元素的删除位置

p=q;

q=q->next;

}

if(!q) printf("\nnot deleted");/*未找到元素e*/

else {p->next=q->next;/*找到删除*/

free(q);}

}

/*单向链表的逆置*/

void exch(LinkList &L){

LinkList p,s;

p=L->next;

L->next=NULL;

while(p){

s=p;

p=p->next;

s->next=L->next;

L->next=s;

}

}

/*两个非递减有序单向链表合并后仍为非递减序列*/

void MergeIncrease(LinkList La,LinkList Lb,LinkList &Lc){

LinkList p,q,s,rear;

p=La->next;

q=Lb->next;

Lc=rear=La;

free(Lb);

while(p&&q){

if (p->data<q->data) {s=p;p=p->next; }

else {s=q;q=q->next; }

rear->next=s;/*较小元素插入表尾*/

rear=rear->next;

}

if (p) rear->next=p; else rear->next=q;

}

/*主函数部分,文件名为sllink.c*/

//#include "sllink.h"

void main(){

LinkList La,Lb,Lc;

ElemType e;

int select;

do{

printf(" 1 建立无序表,再删除指定元素\n");

printf(" 2 建立递增有序表,再逆置\n");

printf(" 3 建立两个递增有序表,将它们和并为一个仍递增表\n");

printf(" 0 退出,请输入选项(0-3)\n");

scanf("%d",&select);

switch(select){

case 0:

break;

case 1:

CreateList(La);

Traverse(La);

printf("输入需删除的元素\n");

scanf("%d",&e);

Delete(La,e);

Traverse(La);

break;

case 2:

Create_Sort(La);

Traverse(La);

exch(La);

printf("The list that is exchaged\n");

Traverse(La);

break;

case 3:

Create_Sort(La);Traverse(La);

Create_Sort(Lb);Traverse(Lb);

MergeIncrease(La,Lb,Lc);Traverse(Lc);

break;

default:

printf("输入选项错误,请重新输入!\n");

}

}while(select);

}

这些内容不知道能不能帮助到你。