如何用C语言或C++实现一个List类?

Python023

如何用C语言或C++实现一个List类?,第1张

如何用C语言或C++实现一个List类?

C语言没有类的概念。C++有现成的List类, #include<list>即可。

如果要自己实现可以参考C++数据结构的书籍,是最基本的练习。

这里实现一个简单的例程,请参考:

#include <iostream>#include <fstream>#include <stdlib.h>#include <string.h>using namespace std#include<stdio.h>#include<string>#include "math.h"template<class T>class List{public:List() 构造函数{pFirst = NULL }void Add(T&t) 在Link表头添加新结点{if(pFirst == NULL){pFirst = new Node *(pFirst->pT) = t }else{Node* pNewNode = new Node *(pNewNode->pT) = t pNewNode->pNext = pFirst pFirst = pNewNode }} void Remove(T&t) 在Link中删除含有特定值的元素{Node* pNode = pFirst if(*(pNode->pT) == t){pFirst = pFirst->pNext delete pNode return }while(pNode != NULL){Node* pNextNode = pNode->pNext if(pNextNode!=NULL){if(*(pNextNode->pT) == t){pNode->pNext = pNextNode->pNext delete pNextNode return }}elsereturn没有相同的pNode = pNode->pNext }} T* Find(T&t) 查找含有特定值的结点{Node* pNode = pFirst while(pNode != NULL){if(*(pNode->pT) == t){return pNode->pT }pNode = pNode->pNext }return NULL } void PrintList() 打印输出整个链表{if(pFirst == NULL){cout<<"列表为空列表!"<<endl return }Node* pNode = pFirst while(pNode != NULL){cout<<*(pNode->pT)<<endl pNode = pNode->pNext }} ~List(){Node* pNode = pFirst while(pNode != NULL){Node* pNextNode = pNode->pNext delete pNode pNode = pNextNode }}protected: struct Node{Node* pNext T* pT Node(){pNext = NULL pT = new T }~Node(){delete pT } } Node *pFirst 链首结点指针}class Student{public:char id[20] 学号char name[20] 姓名int age 年龄Student(){}~Student(){}Student(const char* pid, const char* pname, int _age){strcpy(id, pid) strcpy(name, pname) age = _age }bool operator==(const Student&stu){return strcmp(id, stu.id) == 0 &&strcmp(id, stu.id) == 0 &&age==stu.age }Student&operator=(const Student&stu){strcpy(id, stu.id) strcpy(name, stu.name) age = stu.age }friend ostream&operator<<(ostream &out,const Student&stu)}ostream &operator<<(ostream &out,const Student&stu){out<<"id:"<<stu.id<<"\tname:"<<stu.name<<"\tage:"<<stu.age<<endl}int main(){List<Student>stuList cout<<"添加学生前:"<<endl stuList.PrintList() Student stu1("1", "张三", 18) Student stu2("2", "李四", 18) Student stu3("3", "王五", 18) Student stu4("4", "至尊宝", 18) Student stu5("5", "猪八戒", 18) Student stu6("6", "唐僧", 18) Student stu7("7", "沙和尚", 18) Student stu8("8", "观音", 18) stuList.Add(stu1) stuList.Add(stu2) stuList.Add(stu3) stuList.Add(stu4) stuList.Add(stu5) stuList.Add(stu6) stuList.Add(stu7) stuList.Add(stu8) cout<<"添加学生后:"<<endl stuList.PrintList() Student stu11("1", "张三", 18) Student* pStu = stuList.Find(stu11) cout<<"查找到的同学是:"<<*pStu stuList.Remove(stu11) cout<<"\n\n删除第一个后:"<<endl stuList.PrintList() return 0}

如何用C语言实现一个类似C++中vector的功能

你先学习template,看看Vector是怎么造的,懂得了原理,vector简单的,说穿了就是数组,数组是有大小的,当你要扩大到一定的时候,就会重新分配一个较大的数组,把先前的复制到新的数组中,这个是vector的致命缺陷~当要插入的量大的时候,但优点是访问速度快。

个人博客::blog.csdn./what951006?viewmode=list

C语言如何用if实现一个循环

label if(conditiong)

{

expresstion

goto label

}

如何用C语言实现一个类似C++中vector<string>的功能

对于不可以operator overload的c语言来说,string就算实现也是半吊子工程。至于vector,c没有template,所以也不可以。

请教各位大侠如何用C语言或者是C++实现一个对话框

自己实现是不太现实的

需要你自己来处理图形界面 工作量太大。

最简单的 就是用MFC 里面有现成的对话框函数 直接调用就好

也可以用minigui 或者QT一类的图形插件。

如何用C++实现堆排序?不要C语言!

:baike.baidu./view/157305.htm这个网站里面有 你可以看一下

如何用c语言自己实现一个互斥体或临界区

互斥体实现了“互相排斥”(mutual exclusion)同步的简单形式(所以名为互斥体(mutex))。互斥体禁止多个线程同时进入受保护的代码“临界区”(critical section)。 每个进程中访问临界资源的那段代码称为临界区(Critical Section)

如何用c++实现一个lru cache

其实关键不在于用C++还是什么,关于在于算法

一般可以使用一个map和一个双向链表来实现lru

C语言如何用vb界面实现

C做界面用VC++

matlab中imagesc如何用C语言去实现

1.准备好C语言程序,清楚C语言的入口函数

2.编写mexfunction函数。mexfunction函数为C语言与MATLAB语言的接口函数。调用实例在mylinedetect.c文件中.在MATLAB中调用mex指令编译相关文件,将C语言编译为MEX文件。

3.编译完成后,生成mylinedetect.mexw32或mylinedetect.mexw64文件,此文件即mex文件,用于MATLAB与C语言接口函数.

4.编译完成之后,编写MATLAB函数,调用MEX文件。以MEX文件的形式调用编译完成的C语言函数[o1,o2]=mylinedetect(double(X).')......

5.输出结果,上述linedetect函数完成图像中直线检测功能,带入MATLAB中调用后形成结果。

一共有两种方法,第一种是将C++中的面向对象特征去掉,先全部理解源代码的逻辑,然后改写;第二种是在C中保留面向对象的部分特征,用结构体实现类的功能。

不过在这里第一种方法只适合于数目比较少的时候,如果数目多的话会容易写错,需要重新写,会很麻烦。所以如果数目多的情况下我们可以使用第二种方法。

由于C++解释器比C语言解释器占用的存储空间要大500k左右。为了节省有限的存储空间,降低成本,同时也为了提高效率,将用C++语言写的源程序用C语言改写是很有必要的。C++与C区别最大的就是C++中的类的概念和特性,将C++改为C的问题,就转换成如何将类化去的问题。

#include <iostream>

#include <assert.h>

using namespace std

struct LinkNode{

char data

struct LinkNode *link

LinkNode(){int data=0LinkNode *top=NULL}

}

class LinkedStack{

private:

LinkNode *top

int size

public:

LinkedStack(){top = NULL }

~LinkedStack(){makeEmpty()}

void push(char x)

char pop()

bool makeEmpty()

int getSize()

bool getTop()

bool isEmpty()

}

void LinkedStack::push(char x){

LinkNode *newNode=new LinkNode

newNode->data=x

newNode->link=top

top=newNode

size++

assert(top!=NULL) }

bool isEmpty(){

if(top == NULL)return true //这里老是提示top为“未定义标识符”怎么办?

return false

//return (top==NULL)?true:false这种写法也提示出错

}

char LinkedStack::pop() {

char x

if(isEmpty()==true){

cout<<"空链表,没有出栈的元素了哦,傻瓜!"<<endl

return false}

LinkNode *p=top

top= top->link

x=p->data

delete p

return x

}

bool LinkedStack::makeEmpty(){ //使链表全部清空

LinkNode *p

cout<<"进行元素清空处理"<<endl

while(top!= NULL){p=toptop=top->linkdelete p}

cout<<"小鬼,链式栈的元素已经清空完毕 !"<<endl

return true

}

int LinkedStack::getSize(){ //获得表的长度

LinkNode *p=top

int k =0

while(top!=NULL){

top=top->linkk++

}

cout<<"现在链表的长度为 :"<<k<<endl

return k

}

void main (){

LinkedStack lstack

int n

cout<<"请输入你要输入的字符总长度(小于100) :"<<endl

cin>>n

//char *p = new expression[100]

char expression[100]

cout<<"请输入你要测试的符号内容 :"<<endl

for(int i=0i<ni++){

cin>>expression[i]

}

for(int j=0j<n j++){

if(expression[j] ==( '('||'{'||'[')){

lstack.push(expression[j])

}

if(expression[j] == (')'||'}'||']')){

if( expression[j] == lstack.pop()){

cout<<"第"<<j+1<<"处的字符有匹配的括号"<<endl

}else{

cout<<"第"<<j+1<<"处的字符没有匹配的括号"<<endl

}

}

}

while(lstack.isEmpty() == false){

int d = lstack.getSize()

cout<<"不能匹配的括号个数为 :"<<d<<endl

cout<<"它们分别为: "<<endl

for(int k=0k<=dk++){

lstack.pop()

}

}

lstack.makeEmpty()

system("Pause")

}