c语言实现顺序表?

Python016

c语言实现顺序表?,第1张

--顺序表.h

#include<stdio.h>

#include<stdlib.h>

#include<malloc.h>

#define LIST_INIT_SIZE 100

#define LISTINCREMENT 10

#define TRUE 1

#define FLASE 0

typedef int Elemtype

typedef int Status

/*接口定义

Status InitList_Sq(SqList &L,int size,int inc)

void CreateList_Sq(SqList &L)

void print_Sq(SqList &L)

int Search_Sq(SqList L, Elemtype e)

Status DestroyList_Sq(SqList &L)

Status ClearList_Sq(SqList &L)

Status ListEmpty_Sq(SqList L)

int ListLength_Sq(SqList L)

Status GetElem_Sq(SqList L, int i, Elemtype &e)

Status PutElem_Sq(SqList &L, int i, Elemtype e)

Status Append_Sq(SqList &L, Elemtype e)

Status DeleteLast_Sq(SqList &L, Elemtype &e)

*/

--------------------

#include "顺序表.h"

//定义顺序表类型

typedef struct {

Elemtype *elem

int length

int listsize

int increment

}SqList

//初始化顺序表

Status InitList_Sq(SqList &L,int size,int inc) {

L.elem = (Elemtype *)malloc(size * sizeof(Elemtype))

L.length = 0

L.listsize = size

L.increment = inc

return TRUE

}

//创建顺序表

Status CreateList_Sq(SqList &L) {

int i

printf("请输入你要创建的顺序表元素个数:\n")

scanf_s("%d", &L.length)

if (L.length >= L.listsize) {

L.elem = (Elemtype *)realloc(L.elem, (L.listsize + L.increment) * sizeof(Elemtype))

}

if (!L.elem) {

return FLASE

}

printf("请输入你要创建的顺序表:\n")

for (i = 0i<L.lengthi++) {

scanf_s("%d", &L.elem[i])

}

}

//遍历顺序表

void print_Sq(SqList &L) {

int i

for (i = 0i<L.lengthi++) {

printf("%4d", L.elem[i])

}

}

//查找元素的位置

int Search_Sq(SqList L, Elemtype e) {

int i = 0

while (L.elem[i] != e&&i<L.length) {

i++

}

if (i>L.length)

return -1

else

return i + 1//因为C语言是从下标为0开始的,当i=0时表示第一个元素

}

//销毁顺序表

Status DestroyList_Sq(SqList &L) {

if (L.elem == NULL)

return -1

else

free(L.elem)

printf("\n销毁成功\n")

return TRUE

}

//清空顺序表

Status ClearList_Sq(SqList &L) {

if (L.elem == NULL)

exit(0)

int i

Elemtype *p_elem = L.elem

for (i = 0i<L.lengthi++) {

*L.elem = NULL

L.elem++

}

L.elem = p_elem

}

//判断顺序表是否为空

Status ListEmpty_Sq(SqList L) {

int i

Elemtype* p_elem = L.elem

for (i = 0i<L.lengthi++) {

if (*L.elem != 0) {

L.elem = p_elem

return FLASE

}

L.elem++

}

return TRUE

}

//求顺序表的长度

int ListLength_Sq(SqList L) {

return L.length

}

//用e返回顺序表L中第i个元素的值

Status GetElem_Sq(SqList L, int i, Elemtype &e) {

int j

Elemtype* p_elem = L.elem

if (i<1 || i>L.length)

return FLASE

for (j = 1j <= ij++)

L.elem++

e = *L.elem

L.elem = p_elem

return TRUE

}

//将顺序表L中第i个元素赋值为e

Status PutElem_Sq(SqList &L, int i, Elemtype e) {

L.elem[i - 1] = e

return TRUE

}

//在顺序表L表尾添加元素e

Status Append_Sq(SqList &L, Elemtype e) {

L.elem[L.length] = e

L.length++

L.listsize += L.increment

return TRUE

}

//删除顺序表L表尾元素

Status DeleteLast_Sq(SqList &L, Elemtype &e) {

e = L.elem[L.length - 1]

L.length--

return TRUE

}

********************************************主函数.c*************************************************

#include <stdio.h>

#include <stdlib.h>

#include "顺序表.h"

#include "源代码.h"

//--------------------主函数入口--------------------

int main(){

SqList L

int size, inc

int e

int a

int length

int i

int temp

int j=10

int ee

printf("\n--------------------顺序表初始化------------------\n")

printf("请输入顺序表的长度size以及扩容量:\n")

scanf_s("%d %d", &size, &inc)

InitList_Sq(L, size, inc)

CreateList_Sq(L)

printf("\n--------------------判断是否为空------------------\n")

if(ListEmpty_Sq(L)){

printf("该顺序表为空\n")

}

else

printf("该顺序表不为空\n")

printf("\n--------------------遍历顺序表--------------------\n")

printf("此时顺序表为:\n")

print_Sq(L)

printf("\n--------------------查找元素----------------------\n")

printf("\n请输入要查找的元素:\n")

scanf_s("%d",&e)

a = Search_Sq(L, e)

printf("%d为第%d位:\n",e,a)

printf("\n--------------------输出长度----------------------\n")

length = ListLength_Sq(L)

printf("顺序表的长度为%d\n",length)

printf("\n----------将顺序表L中第i个元素赋值为temp----------\n")

printf("请输入第i个元素的i值和temp值:\n")

scanf_s("%d %d",&i,&temp)

PutElem_Sq(L, i, temp)

printf("\n此时顺序表为:\n")

print_Sq(L)

printf("\n---------------在顺序表表尾添加元素---------------\n")

Append_Sq(L, j)

printf("\n此时顺序表为:\n")

print_Sq(L)

printf("\n---------------在顺序表表尾删除元素---------------\n")

DeleteLast_Sq(L, ee)

printf("\n被删除的元素为%d\n",ee)

printf("此时顺序表为:\n")

print_Sq(L)

printf("\n-------------------清空顺序表---------------------\n")

ClearList_Sq(L)

if(ListEmpty_Sq(L)){

printf("\n清空成功\n")

}

printf("\n------------------销毁顺序表----------------------\n")

DestroyList_Sq(L)

getchar()

getchar()

return 0

}

#include <stdio.h>

#include <malloc.h>

#define MaxSize 50

typedef char ElemType

typedef struct

{

ElemType elem[MaxSize]

int length

}SqList

void InitList(SqList *L) /* 初始化顺序表 */

{

L=(SqList *)malloc(sizeof(SqList))

L->length=0

}

void CreateListR(SqList *L,ElemType a[],int n) /*创建顺序表 */

{

int i

for(i=0i<ni++)

L=(SqList *)malloc(sizeof(SqList))

L->elem[i]=a[i]

L->length++

}

void DispList(SqList *L) /* 输出顺序表 */

{

int i

if(ListEmpty(L)) return

for(i=0i<L->lengthi++)

printf("%c",L->elem[i])

printf("\n")

}

int ListLength(SqList *L) /* 求顺序表的长度 */

{

return(L->length)

}

int ListEmpty(SqList *L) /* 求顺序表是否为空 */

{

return(L->length==0)

}

int GetElem(SqList *L,int i,ElemType e) /*求顺序表中某个数据的元素值 */

{

if(i<1||i>L->length)

return 0

else

e=L->elem[i-1]

return 1

}

int LocateElem(SqList *L,ElemType e)/*按值查找元素*/

{

int i=0

while(i<L->length&&L->elem[i]!=e) i++

if(i>=L->length)

return 0

else

return i+1

}

int ListInsert(SqList *L,int i,ElemType e) /*按i位置插入元素*/

{

int j

if(i<1||i>L->length+1)

return 0

i--

for (j=L->lengthj>ij--)

L->elem[j]=L->elem[j-1]

L->elem[i]=e

L->length++

return 1

}

int ListDelete(SqList *L,int i,ElemType e) /*删除某个位置的元素*/

{

int j

if (i<1||i>L->length)

return 0

i--

e=L->elem[i] /*e中的放elem[i]有何用..以后没用到*/

for(j=ij<L->length-1j++)

L->elem[j]=L->elem[j+1]

L->length--

return 1

}

void DestroyList(SqList *L) /*销毁链表*/

{

free(L)

}

void main()

{

SqList L

ElemType a[]={'a','b','c','d'}

int c

int e

while(1)

{

printf("Please Choose the step you want ?\n\n")

scanf("%d",&c)

if(c==0) break

switch(c)

{

case 1: InitList(&L)break

case 2: CreateListR(&L,a,4)break

case 3: DispList(&L)break

case 4: printf("long %d", ListLength(&L))break

case 5: printf("%d",ListEmpty(&L))break

case 6: GetElem(&L,3,e)break

case 7: LocateElem(&L,'a')break

case 8: ListInsert(&L,4,'f')break

case 9: DispList(&L)break

case 10: ListDelete(&L,3,e)break

case 11: DispList(&L)break

case 12: DestroyList(&L)break

default: printf("error data")break

}

}

}

#include <stdio.h>

#include <stdlib.h>

typedef int DataType // 定义数据数据类型

typedef struct {

DataType *data   // data指向数据区的首个数据

int length       // 数据长度

}SqList

void Sort(SqList *L) {

int i,j,k

DataType tmp

for(i = 0 i < L->length - 1 ++i) {

k = i

for(j = i + 1 j < L->length ++j)

if(L->data[k] > L->data[j])

k = j

if(k != i) {

tmp = L->data[k]

L->data[k] = L->data[i]

L->data[i] = tmp

}

}

}

SqList *CreateList(DataType a[],int n) {

int i

SqList *L

L = (SqList *)malloc(sizeof(SqList))

L->data = (DataType *)malloc(n * sizeof(DataType))

L->length = n

for(i = 0 i < n ++i) L->data[i] = a[i]

Sort(L)

return L

}

int InsertList(SqList *L,DataType x) {

int i,j

for (i = 0i < L->lengthi++) {

if(x <= L->data[i]) {

for(j = L->lengthj >= ij--)

L->data[j + 1] = L->data[j] // 结点后移

L->data[i] = x

L->length++

return 1

}

}

L->data[L->length++] = x

return 1

}

int RemoveListElem(SqList *L,DataType d) {

int i,j

for(i = 0 i < L->length ++i) {

if(L->data[i] == d) {

for(j = i j < L->length - 1 ++j)

L->data[j] = L->data[j + 1]

L->length--

return 1

}

}

return 0

}

SqList *AndList(SqList *A, SqList *B) {   /* A∩B */

int i,j,k = 0

int len = (A->length > B->length) ? B->length : A->length

SqList *C = (SqList *)malloc(sizeof(SqList))

C->length = len

C->data = (DataType *)malloc(len * sizeof(DataType))

for(i = 0 i < A->length ++i) {

for(j = 0 j < B->length ++j) {

if(A->data[i] == B->data[j]) {

C->data[k++] = A->data[i]

break

}

}

}

C->length = k

return C

}

SqList *OrList(SqList *A, SqList *B) {   /* A∪B */

int i,j,flag

DataType e

SqList *C = (SqList *)malloc(sizeof(SqList))

C->length = A->length + B->length

C->data = (DataType *)malloc(C->length * sizeof(DataType))

for(i = 0 i < A->length ++i) C->data[i] = A->data[i]

for(i = 0 i < B->length ++i) {

e = B->data[i]

flag = 1

for(j = 0 j < C->length ++j) {

if(e == C->data[j]) {

flag = 0

break

}

}

if(flag) InsertList(C,e)

}

return C

}

void PrintList(SqList *L) {

int i

for(i = 0 i < L->length ++i)

printf("%d ",L->data[i])

printf("\n")

}

void FreeList(SqList *L) {

free(L->data)

free(L)

}

void main() {

DataType x

DataType arra[] = {36,24,31,5,90,65,70,39,37}

DataType arrb[] = {9,8,43,51,37,89,33,46,29,80,56}

int alen = sizeof(arra)/sizeof(arra[0])

int blen = sizeof(arrb)/sizeof(arrb[0])

SqList *A = CreateList(arra,alen)

printf("A线性表为: ")

PrintList(A)

SqList *B = CreateList(arrb,blen)

printf("B线性表为: ")

PrintList(B)

SqList *C = AndList(A,B)

SqList *D = OrList(A,B)

printf(" C = A∩B: ")

PrintList(C)

printf(" D = A∪B: ")

PrintList(D)

printf("在D表中插入数据 : ")

scanf("%d",&x)

InsertList(D,x)

printf("D表插入x后 :")

PrintList(D)

printf("删除D表中数据 : ")

scanf("%d",&x)

RemoveListElem(D,x)

printf("删除x后的D表为 :")

PrintList(D)

FreeList(A)

FreeList(B)

FreeList(C)

FreeList(D)

}