如何用C语言或C++实现一个List类?
你先学习template,看看Vector是怎么造的,懂得了原理,vector简单的,说穿了就是数组,数组是有大小的,当你要扩大到一定的时候,就会重新分配一个较大的数组,把先前的复制到新的数组中,这个是vector的致命缺陷~当要插入的量大的时候,但优点是访问速度快。
个人博客:http://blog.csdn.net/what951006?viewmode=list
list::iterator p; p = list_test.begin(); p++; *p += 5;
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;
}
}
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, 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<<"name:"<<stu.name<<"age:"<<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<<"
删除第一个后:"<<endl;
stuList.PrintList();
return 0;
}
C++本身就有一个List容器对象啊
c语言没有类.如果一定要用函数指针来模拟class的行为最后的代码看起来有点奇怪
c++有现成的 #include<list>即可
需要手写的话随便找一本数据结构的书 都有现成的吧
c风格的写法是定义一个node struct 然后用一堆全局函数来操控
struct node
{
int ele;
node* next;
};
insert(node* list,int x);
getatlocation(node* list,int x);
等等
c++风格的写法通常会使用template
template<typename Node> class LinkedList
{
private:
int SizeOfList; // Number of nodes in the LinkedList object
Node* Head; //
public:
LinkedList();
LinkedList(int n);
~LinkedList();
int insert(const Node& value);
}
等等..
//头文件:LinkList.h
typedef struct LNode {
int data;
struct LNode *next;
}LNode, *pLinkList;
class LinkList {
private:
pLinkList m_pList;
int m_listLength;
public:
LinkList();
~LinkList();
bool InitList ();
bool DestroyList ();
bool ClearList();
bool IsEmpty ();
int GetLength ();
bool GetNode(int position, LNode** node);
int LocateElem(int elem);
bool SetNodeData(int position, int newData);
bool GetNodeData(int position, int &data);
bool InsertNode(int beforeWhich, int data);
bool DeleteNode(int position);
};
//Cpp文件:LinkList.cpp
#include <iostream.h>
#include "LinkList.h"
LinkList::LinkList() {
m_pList = NULL;
m_listLength = 0;
InitList();
}
LinkList::~LinkList() {
if (!DestroyList()) {
DestroyList();
}
}
//初始化,分配一个头节点。
bool LinkList::InitList() {
if (!(m_pList = new LNode)) {
return false;
}
m_pList->next = NULL;
return true;
}
//销毁链表。
bool LinkList::DestroyList() {
if (!ClearList()) {
return false;
}
delete m_pList;
return true;
}
//判断链表是否为空。若为空,返回true,否则返回false。
bool LinkList::IsEmpty() {
if (m_pList->next == NULL) {
return true;
}
return false;
}
//返回链表的中当前节点数。
int LinkList::GetLength() {
return m_listLength;
}
//将链表清空,释放当前所有节点。
bool LinkList::ClearList() {
if (m_pList == NULL) {
return false;
}
LNode *pTemp = NULL;
while (m_pList->next != NULL) {
pTemp = m_pList->next;
m_pList->next = pTemp->next;
delete pTemp;
}
m_listLength = 0;
return true;
}
//将position指定的节点内的数据设置为newData。
//第一个有效节点的position为1。
bool LinkList::SetNodeData(int position, int newData) {
LNode *pTemp = NULL;
if (!(GetNode(position, &pTemp))) {
return false;
}
pTemp->data = newData;
return true;
}
//得到指定位置节点的数据。
//节点索引从1到listLength。
bool LinkList::GetNodeData(int position, int &data) {
LNode *pTemp = NULL;
if (!(GetNode(position, &pTemp))) {
return false;
}
data = pTemp->data;
return true;
}
//在链表中插入一个节点。
//插入的位置由beforeWhich指定,新节点插入在beforeWhich之前。
//beforeWhich的取值在1到ListLength+1之间。
bool LinkList::InsertNode(int beforeWhich, int data) {
LNode *pTemp = NULL;
if (beforeWhich < 1 || beforeWhich > (m_listLength + 1)) {
return false;
}
if (!(GetNode(beforeWhich - 1, &pTemp))) {
return false;
}
LNode *newNode = new LNode;
newNode->data = data;
newNode->next = pTemp->next;
pTemp->next = newNode;
m_listLength++;
return true;
}
//删除一个指定的节点。
//节点位置由position指定。
//positon的值从1到listLength。
//若链表为空或指定的节点不存在则返回false。
bool LinkList::DeleteNode(int position) {
if (position < 1 || position > m_listLength) {
return false;
}
LNode *pTemp = NULL;
if (!(GetNode(position - 1, &pTemp))) {
return false;
}
LNode *pDel = NULL;
pDel = pTemp->next;
pTemp->next = pDel->next;
delete pDel;
m_listLength--;
return true;
}
//得到指定位置节点的指针。
bool LinkList::GetNode(int position, LNode **node) {
LNode *pTemp = NULL;
int curPos = -1;
pTemp = m_pList;
while (pTemp != NULL) {
curPos++;
if (curPos == position)
break;
pTemp = pTemp->next;
}
if (curPos != position) {
return false;
}
*node = pTemp;
return true;
}
//定位与指定数据相等的数据节点。
//如果在当前链表中已经存在该数据则返回该数据节点的索引号。
//若不存在这样的节点则返回0。
//节点索引从0开始到listLength。
int LinkList::LocateElem(int elem) {
LNode *pTemp = NULL;
int curIndex = 1;
pTemp = m_pList->next;
while ((pTemp != NULL) && (pTemp->data != elem)) {
pTemp = pTemp->next;
curIndex++;
}
if (pTemp == NULL) {
return 0;
}
return curIndex;
}
/*
int main(){
LinkList l;
l.InsertNode(1, 10);
l.InsertNode(2, 20);
l.InsertNode(3, 30);
l.InsertNode(4, 40);
cout << l.GetLength() << endl;
int dataTemp = 0;
for (int i = 1; i <= l.GetLength(); i++) {
l.GetNodeData(i, dataTemp);
cout << dataTemp << endl;
}
if (l.SetNodeData(3, 50)) {
cout <<"DONE\n";
} else {
cout << "Failed\n";
}
for (i = 1; i <= l.GetLength(); i++) {
l.GetNodeData(i, dataTemp);
cout << dataTemp << endl;
}
if (l.DeleteNode(4)) {
cout <<"DONE\n";
} else {
cout << "Failed\n";
}
for (i = 1; i <= l.GetLength(); i++) {
l.GetNodeData(i, dataTemp);
cout << dataTemp << endl;
}
cout << l.LocateElem(50) << endl;
return 0;
}
如何用c语言分别输出个位十位和百位的数字?
用c语言分别输出个位十位和百位的数字,可以根据下面步骤进行,c语言作为一种程序代码,因此在输入时绝对不能弄错任何符号。include<stdio.h> intmain(){intx;scanf("%d",&x);printf("%d的百位=%d,十位=%d,个位=%d\n",x,x/100,x/10%10,x%10);return0;} ...
用C语言或C++ 实现鼠标画图,并可以定位鼠标坐标
楼主你好,我用的是c语言。c语言绘制鼠标的一般步骤是 1.寄存器中断,得到鼠标的位置 2.在改位置画上鼠标 3.后继处理 4.重复上面步骤 那么,在画鼠标的方式上,又有3种不同的方法(就我知道的)1.直接画线法(参考文献)http:\/\/hi.baidu.com\/yql1990115\/blog\/item\/3fdda4eff4000aebb2fb95ae...
或且非怎样用c语言写?
或为||,比如x<1||x>3意思就是x小于1或者大于3。且为&&,比如x<3&&x>1意思就为1<x<3。非为!,比如!0,意思就是非0。“!”(逻辑非)、“&&”(逻辑与)、“||”(逻辑或)是三种逻辑运算符。“逻辑与”相当于生活中说的“并且”,就是两个条件都同时成立的情况下“逻辑与”的运算...
C语言中的 与或非运算
1、逻辑与 在C语言中逻辑与用&&表示。举例:a&&b(其中a、b都代表一个条件)如果a和b都为真,则结果为真,如果a和b中有一个条件为假,则结果为假。2、逻辑或 在C语言中逻辑或用||表示。举例:a||b(其中a、b都代表一个条件)如果a和b有一个或以上为真,则结果为真,二者都为假时,...
C语言有什么作用,编写软件或者游戏是用C语言编写的吗?
缺点也有, C语言的缺点主要表现在数据的封装性上,这一点使得C在数据的安全性上有很大缺陷,这也是C和C++的一大区别。C语言的语法限制不太严格,对变量的类型约束不严格,影响程序的安全性,对数组下标越界不作检查等。从应用的角度,C语言比其他高级语言较难掌握。像电脑游戏或者手机游戏其他编程语言都...
c语言中逻辑或怎么用
一般的来说,这种区别对于程序的运行没有多大影响,但是当表达式作为函数的返回值时,函数就被调用了两次(以后再说明),而且如果使用普通的赋值运算符,也会加大程序的开销,使效率降低。 条件运算符 条件运算符(?:)是C语言中唯一的一个三目运算符,它是对第一个表达式作真\/假检测,然后根据结果返回两外两个表达式中...
c语言中表示或者的符号“||”在电脑上怎么打出来?
同时按住【shift】键和【\\】键,按两下就好。【\\】键在键盘右边enter键上方,如下图,上面也标有“|”符号。C语言中其名称为逻辑“或”,与其并列的逻辑运算符还有:逻辑“与”运算符(&&)、逻辑“非”运算符(!)。
c语言编译中,“或”的符号怎么用键盘输入?
“shift”和“\\”键一起按 键盘不同,位置稍有差别!
用c语言或c++做一个通讯录管理系统
int p=-2,c=n;char name[20];cout<<"请输入姓名"<<endl;cin>>name;for(int i=0;i<n;i++){ if(strcmp(book[i].gname(),name)==0){ p=i;break;} } if(p!=-2){ for(i=p;i<n;i++)book[i]=book[i+1];return --c;} else return c;} void pbook::edit(pbook *...
求用c语言或c++,编写判断五张扑克是同花顺等类型的问题。
分成两类判断:1、是不是同花 2、判断类型 判断过程:1.进行排序--->2.排序后进行相邻的两两相减--->3.在每次相减的时候对结果进行记录--->若只有一个0,则为一对;2个连续0则为三条;2个不连续的0则为三两对;3个连续的0则为四条;3个不连续的0则为三带一...
占追经舒: 读入一个字符串,然后用system函数执行.用C++写的,要C的话改改就行了;#include #include using namespace std; int main() { string cmd; while ( true ) { cin >> cmd; if ( cmd.compare( "quit" ) == 0 ) { break; } else { system( cmd.c_str() ); } } return 0; }
安岳县15839599961: 如何用C语言或C++实现一个List类? - ?
占追经舒: 1. C语言没有类的概念.C++有现成的List类, #include即可. 2. 如果要自己实现可以参考C++数据结构的书籍,是最基本的练习. 这里实现一个简单的例程,请参考: #include #include #include #include using namespace std; #include #include...
安岳县15839599961: 如何用VC++用C语言做DLL文件 - ?
占追经舒: 新建->工程->dll(不要选MFC的那个)->一个简单的dll工程 看一下人家的,然后好好理解一下 dll是什么东东,工作原理是什么,知道一些API函数就可以自己写了
安岳县15839599961: 用C++语言编写?
占追经舒: #include <iostream> using namespace std; int main(int argc, char* argv[]) { int ZhangSan; int LiSi; int WangWu; //每个变量只可能取2个值,取0表示说假话,取1表示说真话 for (ZhangSan = 0; ZhangSan <= 1; ZhangSan++) { for (LiSi = 0; LiSi <= ...
安岳县15839599961: 请教:使用c或者c++语言编写一个字典程序 - ?
占追经舒: 提供一些思路吧: 如果使用C,可以考虑用一个搜索树实现,就是一个26叉树,每个节点最后带有该单词的解释,搜索一个单词的时候从树根开始,每个字母选择一个分支,单词搜索完,停留的节点最后就是对应的解释,这个方法在查询的时候时间复杂度可以达到O(1). 如果使用C++,可以考虑使用map,key是单词,value是解释.
安岳县15839599961: lua 怎么被c++调用的,或者怎么调用c++的? - ?
占追经舒: 使用tolua++即可. 1、tolua++简介tolua++是一种第三方的软件包,可以为Lua提供面向对象的特性,这样我们就可以使用Lua来编写使用C++语言库的脚本文件. 2、tolua++的编译.tolua++的主页在http://www.codenix.com/~tolua/,目前的最新版...
安岳县15839599961: 请问怎么用C++或者C语言编写一个小软件 - ?
占追经舒: 首先是电子元件的数据传进来通过什么方式,比如你说的串口,如果你用的工具是VC,那可以在网上搜索一下VC串口编程的资料和代码.其次是数据的格式,输入是什么格式,输出又要保存成什么格式?例如xml格式的.那么你传进来的数据通过串口接收之后,用C/C++程序进行解析,之后再组合成你要输出的xml文件格式.至于说是实时处理,那要看怎么个实时法.比如你只要求传进来是实时的,而输出可以不是实时的.那么数据在传进来的时候,你可以做一个队列来接收你的数据先,并把接收数据处理的优先级提高.则可以在接收完数据之后再进行格式转化的处理.
安岳县15839599961: 如何用C++实现生成高斯噪声? - ?
占追经舒: 工程中,需要检验某一系统的稳定性是,可以在输入信号中加如微小的噪声.高斯噪声是最常用的一种.请问如何用C/C++编写生成高斯噪声的程序?具体要求:采样点数为1000,噪声均值为0...
安岳县15839599961: 如何用VC++6.0建立一个C语言文件以及建立一个C++文件 - ?
占追经舒: 如果只是建一个文件,可以用上面两位的方法. 如果是想建立一个能运行的C++文件,可以按这样的顺序: 文件->新建->新建项目->win32->win32控制台应用程序->OK->下一步->空项目. 如果想在项目里添加文件,可以选择: 右键点击源文件->添加->新建项->C++文件.
安岳县15839599961: 用C/C++语言编写函数实现double TrimandReverse(char *s) - ?
占追经舒: double TrimandReverse(char * s) { char * sOut = new char(sizeof(s) + 1); memset(sOut, 0, sizeof(sOut)); for ( int i = sizeof(s) - 1, j = 0; i >= 0; i++ ) { if ( s[i] == 32 ) continue; sOut[j] = s[i]; j++ } // 这里做输出处理 }