用c语言实现顺序表和链表,利用C++简单实现顺序表和单链表的示例代码

本文主要给大家介绍了关于C++实现顺序表和单链表的相关内容,分享出来供大家参考学习,话不多说,来一起看看详细的介绍:

一、顺序表示例代码:

#include

#include

using namespace std;

typedef int Datatype;

class SeqList

{

public:

SeqList()

:_array(NULL)

,_size(0)

,_capacity(0)

{

}

SeqList(const SeqList& s)

{

_array = (Datatype*)malloc(s._size*(sizeof(Datatype)));

memcpy(_array, s._array, s._size*(sizeof(Datatype)));

_size = _capacity = s._size;

}

SeqList& operator=(SeqList& s)

{

free(_array);

Swap(s);

return *this;

}

void Swap(SeqList& s)

{

_array = s._array;

_size = s._size;

_capacity = s._capacity;

}

~SeqList()

{

if (_array)

{

free(_array);

_array = NULL;

_size = _capacity = 0;

}

}

void Print()

{

for (size_t i = 0; i < _size; i++)

{

cout << _array[i] << " ";

}

cout << endl;

}

void CheckCapcacity()

{

if (_size == _capacity)

{

_capacity = 2 * _capacity + 3;

_array = (Datatype*)realloc(_array, _capacity*sizeof(Datatype));

assert(_array);

}

}

//后插

void PushBack(Datatype x)

{

Insert(_size, x);

}

//前插

void PushFront(Datatype x)

{

Insert(0, x);

}

//删除最后一个

void PopBack()

{

Erase(_size);

}

//删除第一个

void PopFront()

{

Erase(0);

}

//[]运算符重载

Datatype& operator[](size_t pos)

{

assert(pos < _size);

return _array[pos];

}

//pos位置前插入x

void Insert(size_t pos, Datatype x)

{

assert(pos <= _size);

CheckCapcacity();

int end = (int)_size - 1;

if (pos == 0)

{

while (end >= 0)

{

_array[end + 1] = _array[end];

end--;

}

_array[0] = x;

}

else

{

while (end >= (int)pos)

{

_array[end + 1] = _array[end];

end--;

}

_array[pos] = x;

}

_size++;

}

//删除pos位置的元素

void Erase(size_t pos)

{

assert(pos < _size);

//popfront的实现

if (_size > 0)

{

if (pos == 0)

{

int end = 0;

while (end < (int)_size - 1)

{

_array[end] = _array[end + 1];

end++;

}

_size--;

}

//popback的实现

else if (pos == _size)

{

_size--;

}

//erase

else

{

int end = pos;

while (end < (int)_size - 1)

{

_array[end] = _array[end + 1];

end++;

}

_size--;

}

}

return;

}

private:

Datatype* _array;

size_t _size;

size_t _capacity;

};

二、单链表(不含头结点)示例代码

#include

#include

using namespace std;

typedef int DataType;

struct SListNode

{

SListNode* _next;

DataType _data;

SListNode(DataType x)

:_data(x)

, _next(NULL)

{}

};

typedef SListNode Node;

class SList

{

public:

SList()

:_head(NULL)

, _tail(NULL)

{}

SList(const SList& s)

:_head(NULL)

,_tail(NULL)

{

Copy(s);

}

SList& operator=(const SList& s)

{

Destroy();

Copy(s);

return *this;

}

~SList()

{

Destroy();

}

void Copy(const SList& s)

{

Node* cur = s._head;

while (cur)

{

PushBack(cur->_data);

cur = cur->_next;

}

}

void Destroy()

{

Node* cur = _head;

while (_head != NULL)

{

cur = _head;

_head = cur->_next;

delete cur;

}

_head = _tail = NULL;

}

void PushBack(DataType x)

{

if ((_head == NULL)&&(_tail == NULL))

{

_head = _tail = new Node(x);

}

else

{

_tail->_next = new Node(x);

_tail = _tail->_next;

}

}

void PopBack()

{

if (_head == NULL)

{

return;

}

else if (_head ->_next == NULL)

{

delete _head;

_head = _tail = NULL;

}

else

{

Node* tmp = _head;

while (tmp->_next->_next != NULL)

{

tmp = tmp->_next;

}

_tail = tmp;

tmp->_next = NULL;

}

}

void PushFront(DataType x)

{

if ((_head == NULL) && (_tail == NULL))

{

_head = _tail = new Node(x);

}

else

{

Node* tmp = new Node(x);

tmp->_next = _head;

_head = tmp;

}

}

void PopFront()

{

if (_head == NULL)

{

return;

}

Node* cur = _head;

_head = _head->_next;

delete cur;

}

Node* Find(DataType x)

{

Node* tmp = _head;

while (tmp)

{

if (tmp->_data == x)

return tmp;

tmp = tmp->_next;

}

return NULL;

}

// 插入一个节点在pos的前面

void Insert(Node* pos, DataType x)

{

assert(pos);

if (pos == 0)

{

PushFront(x);

}

else

{

Node* cur = _head;

while (cur->_next != pos)

{

cur = cur->_next;

}

Node* tmp = new Node(x);

tmp->_next = pos;

cur->_next = tmp;

}

}

void Erase(Node* pos)

{

assert(pos);

if (pos == 0)

{

PopFront();

}

else if (pos->_next == NULL)

{

PopBack();

}

else

{

Node* cur = _head;

while (cur->_next != pos)

{

cur = cur->_next;

}

Node* tmp = cur->_next;

cur->_next = tmp->_next;

delete tmp;

}

}

void Print()

{

Node* tmp = _head;

while (tmp != NULL)

{

cout <_data << "->";

tmp= tmp->_next;

}

cout <

}

private:

Node* _head;

Node* _tail;

};

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
链表(Linked List)是一种常见的数据结构,通过节点将元素按顺序连接起来。链表的每个节点包含数据部分和指向下一个节点的指针。 在C语言中,我们可以使用结构体来定义一个链表节点,并使用指针来表示不同节点的连接关系。 首先,我们定义一个链表节点的结构体: ```c typedef struct Node { int data; // 数据部分 struct Node* next; // 指向下一个节点的指针 } Node; ``` 接着,我们可以定义一个链表的结构体,存储链表的头节点和尾节点: ```c typedef struct LinkedList { Node* head; // 链表的头节点 Node* tail; // 链表的尾节点 } LinkedList; ``` 链表实现包括一系列操作,比如插入、删除、查找等。 插入操作:在链表的指定位置插入一个新的节点: ```c void insert(LinkedList* list, int position, int data) { // 创建新的节点 Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; // 插入到链表中 if (position == 0) { // 插入到链表的头部 newNode->next = list->head; list->head = newNode; } else { // 插入到指定位置 Node* currentNode = list->head; for (int i = 0; i < position - 1 && currentNode != NULL; i++) { currentNode = currentNode->next; } if (currentNode != NULL) { newNode->next = currentNode->next; currentNode->next = newNode; } } } ``` 删除操作:删除链表中指定位置的节点: ```c void remove(LinkedList* list, int position) { // 删除链表中的节点 if (position == 0) { Node* currentNode = list->head; list->head = currentNode->next; free(currentNode); } else { Node* currentNode = list->head; Node* previousNode = NULL; for (int i = 0; i < position && currentNode != NULL; i++) { previousNode = currentNode; currentNode = currentNode->next; } if (currentNode != NULL) { previousNode->next = currentNode->next; free(currentNode); } } } ``` 查找操作:根据节点的值查找节点所在位置: ```c int search(LinkedList* list, int data) { int position = 0; Node* currentNode = list->head; while (currentNode != NULL) { if (currentNode->data == data) { return position; } position++; currentNode = currentNode->next; } return -1; // 表示未找到 } ``` 以上是链表的一些基本操作示例,通过结构体和指针,我们可以很方便地实现一个链表的类(class)来进行链表操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值