C语言如何通过类实现线性表,能否详细讲解一下?

2026-04-18 23:532阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计939个文字,预计阅读时间需要4分钟。

C语言如何通过类实现线性表,能否详细讲解一下?

本文以家庭分享的C++实现线性表的具体代码为例,供大家参考。以下内容包含标准C语言函数定义,以及C++中如何使用这些函数。

标准C语言函数定义:

c// list.htypedef struct Node { int data; struct Node* next;} Node;

Node* createList(); // 创建链表void insertNode(Node* head, int data); // 插入节点void deleteNode(Node* head, int data); // 删除节点void printList(Node* head); // 打印链表void freeList(Node* head); // 释放链表内存

C++中使用这些函数:

cpp#include #include list.h

using namespace std;

int main() { Node* head=createList(); // 创建链表

insertNode(head, 10); // 插入节点 insertNode(head, 20); insertNode(head, 30);

printList(head); // 打印链表

deleteNode(head, 20); // 删除节点

printList(head); // 打印链表

freeList(head); // 释放链表内存

return 0;}

注意:

1.第一个参数是`this`的指针,表示当前对象。

2.`list.h`中的函数可以直接在C++中使用,无需修改。

本文实例为大家分享了C++类实现线性表的具体代码,供大家参考,具体内容如下

下图是标准C语言实现的函数定义

下面可以用C++实现,第一个参数就是this的指针

list.h函数

#pragma once typedef int Elem; class List { public: List(int size); ~List(); void ClearList(); // 将数组长度设为0 bool ListEmpty(); // 判断数组是否为空 int ListLength(); // 获取数组长度 bool GetElem(int i, Elem *e); // 查找指定下标元素 int LocateElem(Elem *e); // 查找指定元素 bool PriorElem(Elem *currentElem, Elem *preElem); // 查找元素的前驱元素 bool NextElem(Elem *currentElem, Elem *nextElem); // 查找元素的后继元素 void ListTraverse(); // 遍历线性表,输出元素 bool ListInsert(int i, Elem *e); // 在指定位置插入一个元素 bool ListDelete(int i, Elem *e); // 删除指定位置元素 private: int *m_pList; // 指向一块内存 int m_iSize; // 内存的大小 int m_iLength; // 数组的长度 };

类的实现,list.cpp

#include<iostream> #include "List.h" using namespace std; List::List(int size) { m_iSize = size; m_pList = new Elem[m_iSize]; m_iLength = 0; } List::~List() { delete[] m_pList; // 释放数组内存 m_pList = NULL; } void List::ClearList() { m_iLength = 0; } bool List::ListEmpty() { return m_iLength == 0 ? true : false; } int List::ListLength() { return m_iLength; } bool List::GetElem(int i, Elem *e) { if (i < 0 || i >= m_iSize) { return false; } *e = m_pList[i]; return true; } int List::LocateElem(Elem *e) { for (int i = 0; i < m_iLength; i++) { if (m_pList[i] == *e) { return i; } } return -1; } bool List::PriorElem(Elem *currentElem, Elem *preElem) { int temp = LocateElem(currentElem); // 查找元素的序号 if (temp == -1) return false; else if (temp == 0) return false; else { *preElem = m_pList[temp - 1]; return true; } } bool List::NextElem(Elem *currentElem, Elem *nextElem) { int temp = LocateElem(currentElem); // 查找元素的序号 if (temp == -1) return false; else if (temp == m_iLength - 1) return false; else { *nextElem = m_pList[temp + 1]; return true; } } void List::ListTraverse() { for (int i = 0; i < m_iLength; i++) { cout << m_pList[i] << endl; } } bool List::ListInsert(int i, Elem *e) { if (i<0 || i>m_iLength) return false; for (int k=m_iLength-1;k>=i;k--) { m_pList[k + 1] = m_pList[k]; } m_pList[i] = *e; m_iLength++; return true; } bool List::ListDelete(int i, Elem *e) { if (i<0 || i>m_iLength) return false; *e = m_pList[i]; for (int k = i + 1; k < m_iLength; k++) { m_pList[k - 1] = m_pList[k]; } m_iLength--; return true; }

测试主程序

#include<iostream> #include "List.h" using namespace std; int main() { Elem temp; Elem arry[11] = { 3,5,7,2,9,1,8 }; List *list1 = new List(10); cout << "length:" << list1->ListLength() << endl; for (int i = 0; i < 7; i++) { list1->ListInsert(i, &arry[i]); } cout << "length:" << list1->ListLength() << endl; // 删除第一个元素 list1->ListDelete(0, &temp); cout << temp << endl; // 搜索前驱元素 list1->PriorElem(&arry[4], &temp); cout << temp << endl; list1->NextElem(&arry[4], &temp); cout << temp << endl; list1->ListTraverse(); delete list1; return 0; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

C语言如何通过类实现线性表,能否详细讲解一下?

本文共计939个文字,预计阅读时间需要4分钟。

C语言如何通过类实现线性表,能否详细讲解一下?

本文以家庭分享的C++实现线性表的具体代码为例,供大家参考。以下内容包含标准C语言函数定义,以及C++中如何使用这些函数。

标准C语言函数定义:

c// list.htypedef struct Node { int data; struct Node* next;} Node;

Node* createList(); // 创建链表void insertNode(Node* head, int data); // 插入节点void deleteNode(Node* head, int data); // 删除节点void printList(Node* head); // 打印链表void freeList(Node* head); // 释放链表内存

C++中使用这些函数:

cpp#include #include list.h

using namespace std;

int main() { Node* head=createList(); // 创建链表

insertNode(head, 10); // 插入节点 insertNode(head, 20); insertNode(head, 30);

printList(head); // 打印链表

deleteNode(head, 20); // 删除节点

printList(head); // 打印链表

freeList(head); // 释放链表内存

return 0;}

注意:

1.第一个参数是`this`的指针,表示当前对象。

2.`list.h`中的函数可以直接在C++中使用,无需修改。

本文实例为大家分享了C++类实现线性表的具体代码,供大家参考,具体内容如下

下图是标准C语言实现的函数定义

下面可以用C++实现,第一个参数就是this的指针

list.h函数

#pragma once typedef int Elem; class List { public: List(int size); ~List(); void ClearList(); // 将数组长度设为0 bool ListEmpty(); // 判断数组是否为空 int ListLength(); // 获取数组长度 bool GetElem(int i, Elem *e); // 查找指定下标元素 int LocateElem(Elem *e); // 查找指定元素 bool PriorElem(Elem *currentElem, Elem *preElem); // 查找元素的前驱元素 bool NextElem(Elem *currentElem, Elem *nextElem); // 查找元素的后继元素 void ListTraverse(); // 遍历线性表,输出元素 bool ListInsert(int i, Elem *e); // 在指定位置插入一个元素 bool ListDelete(int i, Elem *e); // 删除指定位置元素 private: int *m_pList; // 指向一块内存 int m_iSize; // 内存的大小 int m_iLength; // 数组的长度 };

类的实现,list.cpp

#include<iostream> #include "List.h" using namespace std; List::List(int size) { m_iSize = size; m_pList = new Elem[m_iSize]; m_iLength = 0; } List::~List() { delete[] m_pList; // 释放数组内存 m_pList = NULL; } void List::ClearList() { m_iLength = 0; } bool List::ListEmpty() { return m_iLength == 0 ? true : false; } int List::ListLength() { return m_iLength; } bool List::GetElem(int i, Elem *e) { if (i < 0 || i >= m_iSize) { return false; } *e = m_pList[i]; return true; } int List::LocateElem(Elem *e) { for (int i = 0; i < m_iLength; i++) { if (m_pList[i] == *e) { return i; } } return -1; } bool List::PriorElem(Elem *currentElem, Elem *preElem) { int temp = LocateElem(currentElem); // 查找元素的序号 if (temp == -1) return false; else if (temp == 0) return false; else { *preElem = m_pList[temp - 1]; return true; } } bool List::NextElem(Elem *currentElem, Elem *nextElem) { int temp = LocateElem(currentElem); // 查找元素的序号 if (temp == -1) return false; else if (temp == m_iLength - 1) return false; else { *nextElem = m_pList[temp + 1]; return true; } } void List::ListTraverse() { for (int i = 0; i < m_iLength; i++) { cout << m_pList[i] << endl; } } bool List::ListInsert(int i, Elem *e) { if (i<0 || i>m_iLength) return false; for (int k=m_iLength-1;k>=i;k--) { m_pList[k + 1] = m_pList[k]; } m_pList[i] = *e; m_iLength++; return true; } bool List::ListDelete(int i, Elem *e) { if (i<0 || i>m_iLength) return false; *e = m_pList[i]; for (int k = i + 1; k < m_iLength; k++) { m_pList[k - 1] = m_pList[k]; } m_iLength--; return true; }

测试主程序

#include<iostream> #include "List.h" using namespace std; int main() { Elem temp; Elem arry[11] = { 3,5,7,2,9,1,8 }; List *list1 = new List(10); cout << "length:" << list1->ListLength() << endl; for (int i = 0; i < 7; i++) { list1->ListInsert(i, &arry[i]); } cout << "length:" << list1->ListLength() << endl; // 删除第一个元素 list1->ListDelete(0, &temp); cout << temp << endl; // 搜索前驱元素 list1->PriorElem(&arry[4], &temp); cout << temp << endl; list1->NextElem(&arry[4], &temp); cout << temp << endl; list1->ListTraverse(); delete list1; return 0; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

C语言如何通过类实现线性表,能否详细讲解一下?