c数据结构 顺序表和链表 相关操作

时间:2023-03-08 17:12:36
c数据结构 顺序表和链表 相关操作

编译器:vs2013

内容:

c数据结构 顺序表和链表 相关操作

#include "stdafx.h"
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

#define LINK_INIT_SIZE 100
#define LISTINCREAMENT 10
#define ElemType int
#define OVERFLOW -2
#define OK 1
#define ERROR 0

typedef int status;

//定义顺序表,并定义一个新类型Sqlist
typedef struct Sqlist{
ElemType *elem;
int listsize;
int length;
}Sqlist;

//函数声明
status InitList(Sqlist &L);
status CreatList(Sqlist &L);
status PrintfList1(Sqlist L);
status PrintfList2(Sqlist L);
status DeleteList(Sqlist &L, int i);
status ListEmpty(Sqlist L);
status ListLength(Sqlist L);
status InsertList(Sqlist &L, int i, ElemType e);
status DestroyList(Sqlist &L);

int main()
{
int i,n,j ;
ElemType e;
Sqlist L1;
InitList(L1); //顺序表初始化操作

ListEmpty(L1); //顺序表判空

CreatList(L1); //顺序表赋值操作

printf("正序输出为:\n");
PrintfList1(L1); //顺序表正序输出操作
printf("\n");
printf("逆序输出为:\n");
PrintfList2(L1); //顺序表逆序输出操作
printf("\n");

ListLength(L1); //求顺序表表长

printf("请输入插入的位置i=");
scanf_s("%d", &i);
e=rand() % 100 + 1;
InsertList(L1, i, e); //顺序表插入操作
PrintfList1(L1);
printf("\n");

printf("请输入删除的位置j=");
scanf_s("%d", &j);
DeleteList(L1,j); //顺序表删除操作
PrintfList1(L1);

DestroyList(L1); //顺序表销毁操作
PrintfList1(L1);
return 0;
}

//构建空的顺序表
status InitList(Sqlist &L)
{
L.elem = (ElemType*)malloc(LINK_INIT_SIZE*sizeof(ElemType));
if (!L.elem)
exit (OVERFLOW);
L.length = 0; //将新的顺序表长度定为0
L.listsize = LINK_INIT_SIZE; //初始化顺序表大小为LINK_INIT_SIZE
return OK;
}

//顺序表插入操作
status InsertList(Sqlist &L, int i, ElemType e)
{
ElemType *newbase, *q,*p;
if (i<1 || i>L.length + 1)
{
printf("未插入成功!!!\n");
return ERROR;
}
if (L.length >= L.listsize)
{
newbase = (ElemType*)realloc(L.elem, (L.listsize + LISTINCREAMENT)*sizeof(ElemType));
if (!newbase)
exit(OVERFLOW);
L.elem = newbase;
L.listsize += LISTINCREAMENT;
} //检验线性表是否满了,满了则重新增加存储空间
q = &(L.elem[i - 1]);
for (p = &(L.elem[L.length - 1]); p >= q; p--)
*(p + 1) = *p;
*q = e;
L.length++;
return OK;
}

//顺序表输入操作
status CreatList(Sqlist &L)
{
ElemType i=0,e;
for (i = 0; i < 10; i++) //顺序表赋值操作
{
L.elem[i] = rand() % 100 + 1;
L.length++;
}
return 0;
}

//顺序表正序输出操作
status PrintfList1(Sqlist L)
{
int i;
if (L.length == 0)
{
printf("该线性表为空!\n");
return 0;
}
for (i = 0; i < L.length; i++)
printf("L.elem[%d]=%d\n", i + 1, *(L.elem + i));
}

//顺序表删除操作
status DeleteList(Sqlist &L, int i)
{
int *p, *q,e;
if (i<1 || i>L.length + 1)
{
printf("不存在此删除位置!!!\n");
return ERROR;
} //检验输入的i是否在线性表所存在的范围内
p = &(L.elem[i - 1]);
e = *p;
q = L.elem + L.length - 1;
for (p++; p <= q; ++p)
*(p - 1) = *p;
L.length--;
printf("删除的元素为%d\n", e);
return 0;
}

//顺序表逆序输出操作
status PrintfList2(Sqlist L)
{
int i;
if (L.length == 0)
{
printf("该线性表为空!\n");
return 0;
}
for (i = L.length-1; i >=0; i--)
printf("L.elem[%d]=%d\n", i + 1, *(L.elem + i));
}

//求顺序表是否为空
status ListEmpty(Sqlist L)
{
if (L.length == 0)
printf("The List is Empty!!!\n");
else
printf("The List is not Empty!!!\n");
return 0;
}

//求顺序表长度
status ListLength(Sqlist L)
{
printf("线性表的长度为%d\n", L.length);
return OK;
}

status DestroyList(Sqlist &L)
{
free(&L.elem[0]);
L.length=0;
return 0;
}

实验结果

c数据结构 顺序表和链表 相关操作

链表操作

#include<malloc.h>

#include<stdlib.h>

typedef int Elemtype;

typedef int Status;

typedef struct LNode{

Elemtype data;

struct LNode *next;

}LNode,*LinkList;

//函数声明

Status CreatList1(LinkList &L, int n);                                   //头插法构建

Status PrintfList(LinkList L);                                          //链表输出

Status CreatList2(LinkList &L, int n);                                   //尾插法构建

Status InverseList(LinkList &L);                                            //逆置链表

int main()

{

LinkList L;

int n,i,e;

printf("请输入建立的线性表结点个数n=");

scanf_s("%d", &n);

CreatList1(L, n);

PrintfList(L);                                                   //头插法建立链表

printf("请输入建立的线性表结点个数n=");

scanf_s("%d", &n);

CreatList2(L, n);

PrintfList(L);                                                   //尾插法建立链表

InverseList(L);                                                //逆置链表

PrintfList(L);

}

//头插法创建链表

Status CreatList1(LinkList &L,int n)

{

int i;

LinkList p;

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

L->next = NULL;

for (i = n; i > 0; i--)

{

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

p->data = rand() % 100 + 1;

printf("头插法的第%d个数据:%d\n", i,p->data);

p->next = L->next;

L->next = p;

}

return 0;

}

//尾插法构建线性表

Status CreatList2(LinkList &L, int n)

{

LinkList p,q;

int i;

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

L->next = NULL;

for (q=L,i = 0; i < n;i++)

{

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

p->data = rand() % 100 + 1;

printf("尾插法的第%d个数据:%d", i + 1,p->data);

q->next = p;

q = p;

printf("\n");

}

q->next = NULL;

return 0;

}

//输出链表

Status PrintfList(LinkList L)

{

LNode *p;

if (L->next == NULL)

printf("The List is Empty!!!\n");

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

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

printf("\n");

return 0;

}

//逆置链表

Status InverseList(LinkList &L)

{

LinkList p,q;

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

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

{

L->next = q->next;

q->next = p->next;

p->next = q;

}

return 0;

}

实验结果

c数据结构 顺序表和链表 相关操作

c数据结构 顺序表和链表 相关操作