【数据结构——线性表】单链表的基本运算(头歌实践教学平台习题)【合集】

目录😋

任务描述

相关知识

测试说明

我的通关代码:

测试结果:


任务描述

本关任务:编写一个程序实现单链表的基本运算。

相关知识

为了完成本关任务,你需要掌握:初始化线性表、销毁线性表、判定是否为空表、求线性表的长度、输出线性表、求线性表中某个数据元素值、按元素值查找、插入数据元素、删除数据元素等。

测试说明

平台会对你编写的代码进行测试:
测试输入:

3

4
预期输出:

(1)初始化单链表h

(2)依次采用尾插法插入a,b,c,d,e元素

(3)输出单链表h:a b c d e

(4)单链表h长度:5

(5)单链表h为非空

(6)单链表h的第3个元素:c

(7)元素a的位置:1

(8)在第4个元素位置上插入f元素

(9)输出单链表h:a b c f d e

(10)删除h的第3个元素

(11)输出单链表h:a b f d e

(12)释放单链表h

开始你的任务吧,祝你成功!


我的通关代码:

cpp 复制代码
#include <iostream>#include <string>
using namespace std;
#define MAX_SIZE 100

typedef char ElemType;

typedef struct {
  ElemType data[MAX_SIZE];
  int length;
} SeqList;

void InitList(SeqList &L) { L.length = 0; }

void PrintList(SeqList L) {
  for (int i = 0; i < L.length; i++) {
    cout << L.data[i] << " ";
  }
  cout << endl;
}
int InsertList(SeqList *L, int i, ElemType e) {
  if (i < 1 || i > L->length + 1 || L->length >= MAX_SIZE)
    return 0;
  for (int j = L->length; j >= i; j--) {
    L->data[j] = L->data[j - 1];
  }
  L->data[i - 1] = e;
  L->length++;
  return 1;
}

bool GetElem(SeqList L, int i, ElemType &e) {
  if (i < 1 || i > L.length)
    return false;
  e = L.data[i - 1];
  return true;
}

int LocateElem(SeqList L, ElemType e) {
  for (int i = 0; i < L.length; i++) {
    if (L.data[i] == e)
      return i + 1;
  }
  return 0;
}

bool ListInsert(SeqList &L, int i, ElemType e) {
  if (i < 1 || i > L.length + 1)
    return false;
  for (int j = L.length; j >= i; j--) {
    L.data[j] = L.data[j - 1];
  }
  L.data[i - 1] = e;
  L.length++;
  return true;
}

bool ListDelete(SeqList &L, int i, ElemType &e) {
  if (i < 1 || i > L.length)
    return false;
  e = L.data[i - 1];
  for (int j = i; j < L.length; j++) {
    L.data[j - 1] = L.data[j];
  }
  L.length--;
  return true;
}

int main() {
  SeqList L;
  InitList(L);
  int pos1, pos2;
  cin >> pos1 >> pos2;
  cout << "(1)初始化单链表h" << endl;

  char elements[] = {'a', 'b', 'c', 'd', 'e'};
  for (int i = 0; i < 5; i++) {
    InsertList(&L, i + 1, elements[i]);
  }
  cout << "(2)依次采用尾插法插入a,b,c,d,e元素" << endl;
  cout << "(3)输出单链表h:";
  PrintList(L);

  cout << "(4)单链表h长度:" << L.length << endl;
  cout << "(5)单链表h为非空" << endl;

  ElemType e;
  if (GetElem(L, pos1, e)) {
    cout << "(6)单链表h的第" << pos1 << "个元素:" << e << endl;
  }
  int pos = LocateElem(L, 'a');
  cout << "(7)元素a的位置:" << pos << endl;

  ListInsert(L, pos2, 'f');
  cout << "(8)在第" << pos2 << "个元素位置上插入f元素" << endl;
  cout << "(9)输出单链表h:";
  PrintList(L);

  ListDelete(L, pos1, e);
  cout << "(10)删除h的第" << pos1 << "个元素" << endl;
  cout << "(11)输出单链表h:";
  PrintList(L);

  cout << "(12)释放单链表h";

  return 0;
}

测试结果:

相关推荐
sulikey10 小时前
深入讲解:什么是 RAII(资源获取即初始化)——原理、实现、面试常考点与实战示例
c++·面试·智能指针·raii·shared_ptr·auto_ptr·资源获取即初始化
艾莉丝努力练剑10 小时前
【Git:多人协作】Git多人协作实战:从同分支到多分支工作流
服务器·c++·人工智能·git·gitee·centos·项目管理
前端炒粉12 小时前
35.LRU 缓存
开发语言·javascript·数据结构·算法·缓存·js
断剑zou天涯14 小时前
【算法笔记】窗口内最大值或最小值的更新结构
java·笔记·算法
smj2302_7968265214 小时前
解决leetcode第3753题范围内总波动值II
python·算法·leetcode
骑着猪去兜风.16 小时前
线段树(二)
数据结构·算法
fengfuyao98518 小时前
竞争性自适应重加权算法(CARS)的MATLAB实现
算法
散峰而望18 小时前
C++数组(二)(算法竞赛)
开发语言·c++·算法·github
leoufung18 小时前
LeetCode 92 反转链表 II 全流程详解
算法·leetcode·链表
靠沿18 小时前
Java数据结构初阶——Collection、List的介绍与ArrayList
java·数据结构·list