请问在C++中如何定义一个动态数组?及一些链表的问题

xiaoxiongxyz 2003-08-22 02:17:14
请问在C++中如何定义一个动态数组?
如果不用MFC怎么链表?
...全文
128 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
shishiXP 2003-08-22
  • 打赏
  • 举报
回复
自己写个可扩展链表

或用stl里的<vector>
yzb1000 2003-08-22
  • 打赏
  • 举报
回复
二维数组p[M][N]
int **p;
p=new int *[M];
for(int i=0;i<5;i++)
p[i]=new int[N];
yzb1000 2003-08-22
  • 打赏
  • 举报
回复
#include <iostream.h>
enum boolean{FALSE, TRUE};
template <class T>
struct Node{
T val;
Node *next;
};
template <class T>
class List{
Node<T> *head;
int size;
public:
List()
{
head = NULL;
size = 0;
}
virtual boolean Insert(T val);
boolean Empty();
boolean Delete(T val);
boolean Contains(T val);
void Print();
~List();
};
template <class T>
List<T>::~List()
{
Node<T> *temp;
for (Node<T> *p = head; p;){
temp = p;
p = p->next;
delete temp;
}
}

template <class T>
boolean List<T>::Empty()
{
if (head)
return FALSE;
else
return TRUE;
}
template <class T>
boolean List<T>::Insert(T x)
{
Node<T> *node = new Node<T>;
if (node){
node->val = x;
node->next = head;
head = node;
size++;
return TRUE;
}
return FALSE;
}

template <class T>
boolean List<T>::Delete(T x)
{
Node<T> *temp;
if (Empty())
return FALSE;
if (head->val == x){
temp = head->next;
delete head;
size--;
head = temp;
return TRUE;
}
for (Node<T> *p = temp = head; p; temp = p, p = p->next)
if (p->val == x){
temp->next = p->next;
delete p;
size--;
return TRUE;
}
return FALSE;
}

template <class T>
boolean List<T>::Contains(T x)
{
for (Node<T> *p = head; p; p = p->next)
if (p->val == x)
return TRUE;
return FALSE;
}

template <class T>
void List<T>::Print()
{
for (Node<T> *p = head; p ; p = p->next)
cout << p->val << " ";
cout << "\n";
}

void main()
{
List<int> intlist;
int i;
for (i = 0; i < 10; i++)
intlist.Insert(i);
intlist.Print();
intlist.Delete(1);
intlist.Delete(2);
intlist.Print();

List<float> floatlist;
floatlist.Insert(3.1);
floatlist.Insert(1.5);
floatlist.Print();

List<char *> charlist;
charlist.Insert("program.");
charlist.Insert("my ds ");
charlist.Insert("is ");
charlist.Insert("This ");
charlist.Print();
}
http://expert.csdn.net/Expert/topic/2059/2059607.xml?temp=.5858118
mynick 2003-08-22
  • 打赏
  • 举报
回复
用STL里的vector。
另,二楼有错,()应为[]
zhang,qiuping 2003-08-22
  • 打赏
  • 举报
回复
如果不用MFC怎么链表?
你说的是单链表吗?
//example如果是想知道更多,则应看一下数据结构。
#include <iostream>
#include <conio.h>
using namespace std;

struct student
{
char name[20];
long id;
float score;
student *pNext;
};
student *pHead;
student *create(void)
{
student *pStart;
student *pEnd;
pStart=new student;
cout<<"请输入Id,Name,Score:"<<endl;
cin>>pStart->id>>pStart->name>>pStart->score;
pHead=NULL;
pEnd=pStart;
while(pStart->id!=0)
{
if(NULL==pHead)
{
pHead=pStart;
}
else
{
pEnd->pNext=pStart;
}
pEnd=pStart;
pStart=new student;
cout<<"请输入Id,Name,Score:"<<endl;
cin>>pStart->id >>pStart->name >>pStart->score ;
}//end of while
pEnd->pNext=NULL;
delete pStart;//因为在跳出循环前,pStart被多分配了一个新的空间,所以要回收!
return pHead;
}

void Print(student *head)
{
cout<<"List the items:"<<endl;
while(head!=NULL)
{
cout<<"ID:"<<head->id <<" "<<"Name:"<<head->name <<" Score:"<<head->score <<endl;
head=head->pNext;
}//end of while
}

void main()
{
Print(create());
getch();
}

sevecol 2003-08-22
  • 打赏
  • 举报
回复
链表的问题,可以使用STL里面的。
wbh0360 2003-08-22
  • 打赏
  • 举报
回复
例:
int *p=new int(5);//开辟5个int空间

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧