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

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

如何用C语言或C++实现一个List类
C语言没有类的概念。C++有现成的List类, #include<list>即可。
如果要自己实现可以参考C++数据结构的书籍,是最基本的练习。

这里实现一个简单的例程,请参考:
#include <iostream>#include <fstream>#include <stdlibh>#include <stringh>using namespace std;#include<stdioh>#include<string>#include "mathh"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; } } else return;没有相同的 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, stuid) == 0 && strcmp(id, stuid) == 0 && age==stuage; } Student& operator=(const Student& stu) { strcpy(id, stuid); strcpy(name, stuname); age = stuage; } friend ostream& operator<< (ostream &out,const Student& stu);};ostream & operator<< (ostream &out,const Student& stu){ out<<"id:"<<stuid<<"\tname:"<<stuname<<"\tage:"<<stuage<<endl;}int main(){ List<Student> stuList; cout<<"添加学生前:"<<endl; stuListPrintList(); 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); stuListAdd(stu1); stuListAdd(stu2); stuListAdd(stu3); stuListAdd(stu4); stuListAdd(stu5); stuListAdd(stu6); stuListAdd(stu7); stuListAdd(stu8); cout<<"添加学生后:"<<endl; stuListPrintList(); Student stu11("1", "张三", 18); Student pStu = stuListFind(stu11); cout<<"查找到的同学是:"<<pStu; stuListRemove(stu11); cout<<"\n\n删除第一个后:"<<endl; stuListPrintList(); return 0;}
如何用C语言实现一个类似C++中vector的功能
你先学习template,看看Vector是怎么造的,懂得了原理,vector简单的,说穿了就是数组,数组是有大小的,当你要扩大到一定的时候,就会重新分配一个较大的数组,把先前的复制到新的数组中,这个是vector的致命缺陷~当要插入的量大的时候,但优点是访问速度快。
个人博客::blogcsdn/what951006viewmode=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语言!
:baikebaidu/view/157305htm这个网站里面有 你可以看一下
如何用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语言的接口函数。调用实例在mylinedetectc文件中在MATLAB中调用mex指令编译相关文件,将C语言编译为MEX文件。
3编译完成后,生成mylinedetectmexw32或mylinedetectmexw64文件,此文件即mex文件,用于MATLAB与C语言接口函数
4编译完成之后,编写MATLAB函数,调用MEX文件。以MEX文件的形式调用编译完成的C语言函数[o1,o2]=mylinedetect(double(X)');
5输出结果,上述linedetect函数完成图像中直线检测功能,带入MATLAB中调用后形成结果。


#include<stdioh>
#include<windowsh>
#include <stdioh>
#include <malloch>
#include <stdlibh>
//定义数据类型名称
typedef int DataType;
#define flag -1        //定义数据输入结束的标志数据
//单链表结点存储结构定义
typedef struct Node
{
    DataType data;
    struct Node next;
}LNode ,LinkList;
//建立单链表子函数
 LNode Create_LinkList()
{
    LNode s,head,L;int i=0,x;        //定义指向当前插入元素的指针
    while(1)
    {
        scanf("%d",&x);
        if(-1==x)
        {   return head;
            break;}
        s= (LNode )malloc(sizeof(LNode));        //为当前插入元素的指针分配地址空间
        s->data =x;
        s->next =NULL;
        i++;
        if(i==1)
            head=s;
        else
            L->next =s;
            L=s;
    }
}
//查找子函数(按序号查找)
LNode Get_LinkList(LinkList L,int i)
{
    LNode p;
    int j;        //j是计数器,用来判断当前的结点是否是第i个结点
    p=L;
    j=1;
    while(p!=NULL&&j<i)
    {
        p=p->next ;        //当前结点p不是第i个且p非空,则p移向下一个结点
        j++;
    }
    return p;
}
//插入运算子函数
void Insert_LinkList(LinkList L,int i,DataType x)        //在单链表L中第i个位置插入值为x的新结点
{
    LNode p,s;
    p =Get_LinkList(L,i);        //寻找链表的第i-1个位置结点
    if(p==NULL)
    {
        printf("插入位置不合法!");
        exit(-1);
    }
    else
    {
        s= (LinkList)malloc(sizeof(LNode));        //为当前插入元素的指针分配地址空间
        s->data =x;
        s->next =p->next ;
        p->next =s;
    }
}
//单链表的删除运算子函数
void Delete_LinkList(LinkList L,int i)        //删除单链表上的第i个结点
{
    LNode p,q;
    p=Get_LinkList(L,i-1);        //寻找链表的第i-1个位置结点
    if(p==NULL)
    {
        printf("删除的位置不合法!");        //第i个结点的前驱结点不存在,不能执行删除 *** 作
        exit(-1);
    }
    else
    {
        if(p->next ==NULL)
        {
            printf("删除的位置不合法!");        //第i个结点不存在,不能执行删除 *** 作
            exit(-1);
        }
        else
        {
            q=p->next ;
            p->next =p->next->next;
            free(q);
        }
    }
}
//求表长运算子函数
int Length_LinkList(LinkList L)
{
    int l;        //l记录L的表长
    LNode p;
    p=L;
    l=1;
    while(p->next)
    {
        p=p->next;
        l++;
    }
    return l;
}
int main ()
{
    LNode head,p;
    head=(LinkList)malloc(sizeof(LNode));
    int x,y;
    a:
    printf("menu\n");
    printf("创建1\n");
    printf("插入2\n");
    printf("删除3\n");
    printf("表长4\n");
    printf("清屏5\n");
    printf("打印6\n");
    printf("退出other\n");
    printf("\n");
    int i=1;
    while(i)
    {
        printf("请输入选项:");
        scanf("%d",&i);
        switch(i)
        {
            case 1:head=Create_LinkList(); getchar();break;
            case 2:printf("请输入位置和数据;");
            scanf("%d%d",&x,&y);
            Insert_LinkList(head,x,y);break;
            case 3:printf("请输入位置;");
                scanf("%d",&x);
                Delete_LinkList(head,x);break;
            case 4:printf("%d",Length_LinkList(head));break;
            case 5:system("cls");goto a;
            case 6:p=head;
        while(p!=NULL)
                {printf("%d\n",p->data);
                p=p->next;}
                break;
            default :i=0;
        }
    }
}

我把创建给改了一下

你这是数据结构中的队列问题,而不是栈的问题。head代表的是队列头,删除时删的是队列头元素,tail代表的是队列尾,插入时插的是队列尾元素
程序中这条语句是错误的。中括号只有一半,那一半呢? p_y=qp++head];
至于要求的主函数,如果是队列问题还要涉及的是一般队列还是循环队列,因此没法写给你。

如果你现在的md5js是:

exportsmd5=xxx;

的话,那么你可以把它改成:

moduleexports=xxx;

这么写的话,你在require的时候

var md5=require('/md5js');

这个md5就是md5js里面的xxx。

该函数中的dwCreateFlags、nStackSize、lpSecurityAttrs参数和API函数CreateThread中的对应参数有相同含义,该函数执行成功,返回非0值,否则返回0。
一般情况下,调用AfxBeginThread()来一次性地创建并启动一个线程,但是也可以通过两步法来创建线程:首先创建CWinThread类的一个对象,然后调用该对象的成员函数CreateThread()来启动该线程。

第一个问题:因为类型名知道声明的末尾才定义完成,所以你在中间使用Node,编译器不能像人一样,看到后面的再来找前面的定义。

第二个问题,typedef 并不是一个类型的关键字,比如int ,double,还有struct 都可以代表一个类型。typedef只是一个表示别称的关键字,比如你朋友叫王二,你可以叫他王二,别人有的人叫他小王。typedef就是取别称的关键字,如果去掉struct,系统不知道它的类型是什么了。

其实相当于

struct list {
int data;
Node next;//为什么不能用Node定义??
}

现在你想给它取个别称,不想每次就写 struct list mylist, 就把别称 typedef struct list Node; 和声明写到了一起。struct list 表示一种类型 ,对这个类型取别名叫 Node

比如,你想把int这种类型取别名,可以 typedef int aa, 那么你以后可以用aa b=55,代替int b=55;
struct list 联合起来是你定义的一种类型。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/13367515.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-31
下一篇2025-08-31

发表评论

登录后才能评论

评论列表(0条)

    保存