线性表代码的实现(最详细)
Posted 垚垚是小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了线性表代码的实现(最详细)相关的知识,希望对你有一定的参考价值。
顺序表的存储结构
typedef struct List {
int *date;
int size;
int length;
}L;
构造线性表
void ListInit(L* ps) {
ps->date = (int*)malloc(sizeof(int) * 4);
if (ps == NULL) {
cout << "创建失败" << endl;
}
else {
cout << "创建成功" << endl;
ps->size = 0;
ps->length = 4;
}
}
销毁线性表
void ListDestory(L* ps) {
assert(ps);
if (ps != NULL) {
cout << "已经销毁" << endl;
free(ps->date);
ps->size = 0;
cout << "销毁成功" << endl;
}
else
cout << "不需要销毁" << endl;
}
打印线性表
void ListPrint( L* ps) {
for (int i = 0; i <ps->size; i++) {
cout << ps->date[i];
}
cout << endl;
}
返回线性表的长度
void ListLengh(L* ps) {
cout << ps->size << endl;;
}
查找某值在线性表中第一次出现的位置
void ListLocate(L* ps, int x) {
for (int i = 0; i <= ps->size; i++) {
if (x == ps->date[i]) {
cout << i+1 << endl;
}
}
}
扩容
void ListExpand(L* ps) {
if (ps->length == ps->size) {
ps->length *= 2;
ps->date = (int*)realloc(ps->date,sizeof(int) * ps->length);//长度扩大
}
if (ps->date == NULL) {
cout << "扩容失败" << endl;
}
}
尾插
void ListPushBack(L *ps,int x) {
ListExpand(ps);
ps->date[ps->size] = x;
ps->size++;
}
头插
void ListPushFront(L* ps, int x) {
ListExpand(ps);
int end = ps->size;
for (int i = 0; i < ps->size; i++) {
ps->date[end] = ps->date[end-1];
end--;
}
ps->date[0] = x;
ps->size++;
}
中间插
void ListPush(L *ps,int pos,int x) {
ListExpand(ps);
int end = ps->size;
for (int i = 0; i <= ps->size - pos+1; i++) {
ps->date[end+1] = ps->date[end];
end--;
}
ps->date[pos] = x;
ps->size++;
}
尾删
void listPopBack(L *ps) {
ps->size--;
}
头删
void ListPopFront(L *ps) {
int start =0;
for (int i = 0; i <ps->size - 1; i++) {
ps->date[start] = ps->date[start+1];
start++;
}
ps->size--;
}
中间任意位置删
void ListPop(L *ps,int pos) {
int start = pos-1;
for (int i = 0; i < ps->size; i++) {
ps->date[start] = ps->date[start+1];
start++;
}
ps->size--;
}
总代码
# define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
using namespace std;
#include "assert.h"
typedef struct List {
int *date;
int size;
int length;
}L;
//构造线性表
void ListInit(L* ps) {
// ps->date = (int*)malloc(sizeof(int*) * 4);
ps->date = (int*)malloc(sizeof(L) * 4);
if (ps == NULL) {
cout << "创建失败" << endl;
}
else {
cout << "创建成功" << endl;
ps->size = 0;
ps->length = 10;
}
}
//销毁线性表
void ListDestory(L* ps) {
assert(ps);
if (ps != NULL) {
cout << "已经销毁" << endl;
free(ps->date);
ps->size = 0;
cout << "销毁成功" << endl;
}
else
cout << "不需要销毁" << endl;
}
//打印线性表
void ListPrint( L* ps) {
for (int i = 0; i <ps->size; i++) {
cout << ps->date[i];
}
cout << endl;
}
//返回线性表的长度
void ListLengh(L* ps) {
cout << ps->size << endl;;
}
//查找线性表中的值
void ListLocate(L* ps, int x) {
for (int i = 0; i <= ps->size; i++) {
if (x == ps->date[i]) {
cout << i+1 << endl;
}
}
}
//扩容
void ListExpand(L* ps) {
if (ps->length == ps->size) {
ps->length *= 2;
ps->date = (int*)realloc(ps->date,sizeof(int) * ps->length);//长度扩大
}
if (ps->date == NULL) {
cout << "扩容失败" << endl;
}
}
//尾插
void ListPushBack(L *ps,int x) {
ListExpand(ps);
ps->date[ps->size] = x;
ps->size++;
}
//头插
void ListPushFront(L* ps, int x) {
ListExpand(ps);
int end = ps->size;
for (int i = 0; i < ps->size; i++) {
ps->date[end] = ps->date[end-1];
end--;
}
ps->date[0] = x;
ps->size++;
}
//中间插
void ListPush(L *ps,int pos,int x) {
ListExpand(ps);
int end = ps->size;
for (int i = 0; i <= ps->size - pos+1; i++) {
ps->date[end+1] = ps->date[end];
end--;
}
ps->date[pos] = x;
ps->size++;
}
//尾删
void listPopBack(L *ps) {
ps->size--;
}
//头删
void ListPopFront(L *ps) {
int start =0;
for (int i = 0; i <ps->size - 1; i++) {
ps->date[start] = ps->date[start+1];
start++;
}
ps->size--;
}
//中间删
void ListPop(L *ps,int pos) {
int start = pos-1;
for (int i = 0; i < ps->size; i++) {
ps->date[start] = ps->date[start+1];
start++;
}
ps->size--;
}
int main() {
L l;
ListInit(&l);
ListPushBack(&l, 1);
ListPushBack(&l, 2);
ListPushBack(&l, 3);
ListPushBack(&l, 4);
ListPushBack(&l, 5);
ListPushBack(&l, 6);
ListPushBack(&l, 7);
ListPrint(&l);
cout<<endl;
ListLengh(&l);
ListLocate(&l, 5);
ListPrint(&l);
ListPushFront(&l, 9);
ListPrint(&l);
ListPush( &l, 3, 3);
ListPrint(&l);
listPopBack(&l);
ListPrint(&l);
ListPopFront(&l);
ListPrint(&l);
ListPop(&l, 3);
ListPrint(&l);
}
代码实现结果
以上是关于线性表代码的实现(最详细)的主要内容,如果未能解决你的问题,请参考以下文章