C++高手进来下下
前些日子在网上下了个程序文件,可惜被自己不小心删掉了(已经不能恢复--!)里面的程序是这样写的链表(好像是,当时国庆节,只略看了下,现在特后悔啊)我大概想了下好像是这样的...
前些日子在网上下了个程序文件,可惜被自己不小心删掉了(已经不能恢复- -!)
里面的程序是这样写的链表(好像是,当时国庆节,只略看了下,现在特后悔啊)
我大概想了下
好像是这样的格式
#include <iostream.h>
class NODE
{
private:
//这里好像就是在定义一个结点是的
int data;
NODE *next;
public:
//这里写了很多的成员函数
void create();
void output();
}
void main()
{
//通过成员函数来实现操作
}
这个是我第一次通过class定义一个结点加上还有成员函数来实现各种操作
平时我就是用个struct/class来定义一个结点。
然后再在外面写函数来进行操作,而并没有写到class/struct里面
所以觉得这个很新颖,但是自己又写不来,求高手帮我写个
只需完成简单的操作即可,比如创建(通过键盘输入,非在程序中固定)+插入+输出到屏幕即可
当然我的那个样板肯定有错的地方
我只想得到一个用一个class定义个结点加上同时包含成员函数来实现单链表的操作的函数。
先献上100积分(看我等级也知道我就那么几百积分)
答案好一定增加到200分
用一个class 同时定义结点+成员函数 展开
里面的程序是这样写的链表(好像是,当时国庆节,只略看了下,现在特后悔啊)
我大概想了下
好像是这样的格式
#include <iostream.h>
class NODE
{
private:
//这里好像就是在定义一个结点是的
int data;
NODE *next;
public:
//这里写了很多的成员函数
void create();
void output();
}
void main()
{
//通过成员函数来实现操作
}
这个是我第一次通过class定义一个结点加上还有成员函数来实现各种操作
平时我就是用个struct/class来定义一个结点。
然后再在外面写函数来进行操作,而并没有写到class/struct里面
所以觉得这个很新颖,但是自己又写不来,求高手帮我写个
只需完成简单的操作即可,比如创建(通过键盘输入,非在程序中固定)+插入+输出到屏幕即可
当然我的那个样板肯定有错的地方
我只想得到一个用一个class定义个结点加上同时包含成员函数来实现单链表的操作的函数。
先献上100积分(看我等级也知道我就那么几百积分)
答案好一定增加到200分
用一个class 同时定义结点+成员函数 展开
2个回答
展开全部
#include <iostream>
using namespace std;
struct NODE
{
int data;
NODE *next;
};
class Tdlb
{
private:
NODE *head;
public:
Tdlb(); //¹¹Ô캯Êý ³õʼ»¯
void print(); //Êä³öº¯Êý
};
Tdlb::Tdlb()
{
NODE *p;
int i,j;
int a[5];
cout<<"input data:"<<endl;
for(j=0;j<5;j++) //ÊäÈë5¸öÊý
cin>>a[j];
head=new NODE;
head->next=NULL;
for(i=4;i>=0;i--)
{
p=new NODE; //±íβ²åÈë
p->data=a[i];
p->next=head->next;
head->next=p;
}
}
void Tdlb::print()
{
while(head)
{
cout<<head->data<<' ';
head=head->next;
}cout<<endl;
}
int main()
{
Tdlb list;
list.print();
return 0;
}
using namespace std;
struct NODE
{
int data;
NODE *next;
};
class Tdlb
{
private:
NODE *head;
public:
Tdlb(); //¹¹Ô캯Êý ³õʼ»¯
void print(); //Êä³öº¯Êý
};
Tdlb::Tdlb()
{
NODE *p;
int i,j;
int a[5];
cout<<"input data:"<<endl;
for(j=0;j<5;j++) //ÊäÈë5¸öÊý
cin>>a[j];
head=new NODE;
head->next=NULL;
for(i=4;i>=0;i--)
{
p=new NODE; //±íβ²åÈë
p->data=a[i];
p->next=head->next;
head->next=p;
}
}
void Tdlb::print()
{
while(head)
{
cout<<head->data<<' ';
head=head->next;
}cout<<endl;
}
int main()
{
Tdlb list;
list.print();
return 0;
}
广州奥泰斯工业自动化控制设备有限公司_
2023-03-29 广告
2023-03-29 广告
日本OPTEX FA光电传感器产品已在中国大陆销售了近30年,产品的高性价比已被广大客户所认可。为更好的服务广大客户,日本株式会社(OPTEX FA)于2013年成立广州奥泰斯工业自动化控制设备有限公司,作为OPTEX FA中国区总部。日本...
点击进入详情页
本回答由广州奥泰斯工业自动化控制设备有限公司_提供
展开全部
#include<iostream.h>
class IntSLLNode{
public:
int info;
IntSLLNode *next;
IntSLLNode(int el, IntSLLNode *ptr = 0){
info = el;
next = ptr;
}
};
class IntSLList{
public:
IntSLList(){
head = tail = 0;
}
~IntSLList();
int isEmpty(){
return head == 0;
}
void addToHead(int);
void addToTail(int);
int deleteFromHead();
int deleteFromTail();
void deleteNode(int);
bool isInList(int) const;
void printList();
int reverseList();
void mergeList(IntSLList &, IntSLList &);
private:
IntSLLNode *head, *tail;
};
IntSLList::~IntSLList(){
for(IntSLLNode *p; !isEmpty();){
p = head->next;
delete head;
head = p;
}
}
void IntSLList::addToHead(int el){
head = new IntSLLNode(el,head);
if (tail == 0)
tail = head;
}
void IntSLList::addToTail(int el){
if(tail != 0){
tail->next = new IntSLLNode(el);
tail = tail->next;
}
else head = tail = new IntSLLNode(el);
}
int IntSLList::deleteFromHead(){
int el = head->info;
IntSLLNode *tmp = head;
if(head == tail)
head = tail =0;
else head = head->next;
delete tmp;
return el;
}
int IntSLList::deleteFromTail(){
int el = tail->info;
if(head ==tail){
delete head;
head = tail = 0;
}
else{
IntSLLNode *tmp;
for(tmp = head; tmp->next != tail; tmp = tmp->next);
delete tail;
tail = tmp;
tail->next = 0;
}
return el;
}
void IntSLList::deleteNode(int el){
if (head != 0){
if (head == tail && el == head->info){
delete head;
head = tail = 0;
}
else if (el == head->info){
IntSLLNode *tmp = head;
head = head->next;
delete tmp;
}
else{
IntSLLNode *pred, *tmp;
for (pred = head, tmp = head->next;
tmp != 0 && !(tmp->info == el);
pred = pred->next, tmp = tmp->next);
if (tmp != 0){
pred->next = tmp->next;
if (tmp == tail)
tail = pred;
delete tmp;
}
}
}
}
bool IntSLList::isInList(int el) const{
IntSLLNode *tmp;
for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next);
return tmp != 0;
}
void IntSLList::printList(){
IntSLLNode *tmp;
if (isEmpty())
cout << "The list is empty!" << endl;
else{
for (tmp = head; tmp != 0; tmp = tmp->next){
cout << tmp->info << " ";
}
}
}
int IntSLList::reverseList(){
if(head == 0)
return 0;
else if (head == tail)
return 0;
else {
IntSLLNode *Na, *Nb, *Nc;
Na = head;
Nb = head->next;
while(Nb){
Nc = Nb->next;
Nb->next = Na;
Na = Nb;
Nb = Nc;
}
head->next = 0;
head = Na;
IntSLLNode *tmp;
tmp = head;
head = tail;
tail = tmp;
}
return 0;
}
void IntSLList::mergeList(IntSLList&a, IntSLList &b){
IntSLLNode *pa, *pb;
pa = a.head;
pb = b.head;
while(pa || pb){
if ((pa != 0) && (pb != 0) && (pa->info < pb->info) || pb == 0){
addToTail(pa->info);
pa = pa->next;
}
else{
addToTail(pb->info);
pb = pb->next;
}
}
}
int main()
{
IntSLList list1, list2, newlist;
int num;
cout << "Please input the list1(input -1 to end):" << endl;
do{
cout << "Please input the information: ";
cin >> num;
if (num == -1)
break;
list1.addToTail(num);
}while(1);
cout << "Please input the list2(input -1 to end):" << endl;
do{
cout << "Please input the information: ";
cin >> num;
if (num == -1)
break;
list2.addToTail(num);
}while(1);
cout << "The list1 is " ;
list1.printList();
cout << "\nThe list2 is " ;
list2.printList();
newlist.mergeList(list1, list2);
cout << "\nAfter two lists are merged,the new list is " ;
newlist.printList();
newlist.reverseList();
cout << "\nAfter reversion,the new list is " ;
newlist.printList();
cin >> num;
return 0;
}
我自己写的程序,不是最好的,我想还是可以的,你看着用吧!
class IntSLLNode{
public:
int info;
IntSLLNode *next;
IntSLLNode(int el, IntSLLNode *ptr = 0){
info = el;
next = ptr;
}
};
class IntSLList{
public:
IntSLList(){
head = tail = 0;
}
~IntSLList();
int isEmpty(){
return head == 0;
}
void addToHead(int);
void addToTail(int);
int deleteFromHead();
int deleteFromTail();
void deleteNode(int);
bool isInList(int) const;
void printList();
int reverseList();
void mergeList(IntSLList &, IntSLList &);
private:
IntSLLNode *head, *tail;
};
IntSLList::~IntSLList(){
for(IntSLLNode *p; !isEmpty();){
p = head->next;
delete head;
head = p;
}
}
void IntSLList::addToHead(int el){
head = new IntSLLNode(el,head);
if (tail == 0)
tail = head;
}
void IntSLList::addToTail(int el){
if(tail != 0){
tail->next = new IntSLLNode(el);
tail = tail->next;
}
else head = tail = new IntSLLNode(el);
}
int IntSLList::deleteFromHead(){
int el = head->info;
IntSLLNode *tmp = head;
if(head == tail)
head = tail =0;
else head = head->next;
delete tmp;
return el;
}
int IntSLList::deleteFromTail(){
int el = tail->info;
if(head ==tail){
delete head;
head = tail = 0;
}
else{
IntSLLNode *tmp;
for(tmp = head; tmp->next != tail; tmp = tmp->next);
delete tail;
tail = tmp;
tail->next = 0;
}
return el;
}
void IntSLList::deleteNode(int el){
if (head != 0){
if (head == tail && el == head->info){
delete head;
head = tail = 0;
}
else if (el == head->info){
IntSLLNode *tmp = head;
head = head->next;
delete tmp;
}
else{
IntSLLNode *pred, *tmp;
for (pred = head, tmp = head->next;
tmp != 0 && !(tmp->info == el);
pred = pred->next, tmp = tmp->next);
if (tmp != 0){
pred->next = tmp->next;
if (tmp == tail)
tail = pred;
delete tmp;
}
}
}
}
bool IntSLList::isInList(int el) const{
IntSLLNode *tmp;
for (tmp = head; tmp != 0 && !(tmp->info == el); tmp = tmp->next);
return tmp != 0;
}
void IntSLList::printList(){
IntSLLNode *tmp;
if (isEmpty())
cout << "The list is empty!" << endl;
else{
for (tmp = head; tmp != 0; tmp = tmp->next){
cout << tmp->info << " ";
}
}
}
int IntSLList::reverseList(){
if(head == 0)
return 0;
else if (head == tail)
return 0;
else {
IntSLLNode *Na, *Nb, *Nc;
Na = head;
Nb = head->next;
while(Nb){
Nc = Nb->next;
Nb->next = Na;
Na = Nb;
Nb = Nc;
}
head->next = 0;
head = Na;
IntSLLNode *tmp;
tmp = head;
head = tail;
tail = tmp;
}
return 0;
}
void IntSLList::mergeList(IntSLList&a, IntSLList &b){
IntSLLNode *pa, *pb;
pa = a.head;
pb = b.head;
while(pa || pb){
if ((pa != 0) && (pb != 0) && (pa->info < pb->info) || pb == 0){
addToTail(pa->info);
pa = pa->next;
}
else{
addToTail(pb->info);
pb = pb->next;
}
}
}
int main()
{
IntSLList list1, list2, newlist;
int num;
cout << "Please input the list1(input -1 to end):" << endl;
do{
cout << "Please input the information: ";
cin >> num;
if (num == -1)
break;
list1.addToTail(num);
}while(1);
cout << "Please input the list2(input -1 to end):" << endl;
do{
cout << "Please input the information: ";
cin >> num;
if (num == -1)
break;
list2.addToTail(num);
}while(1);
cout << "The list1 is " ;
list1.printList();
cout << "\nThe list2 is " ;
list2.printList();
newlist.mergeList(list1, list2);
cout << "\nAfter two lists are merged,the new list is " ;
newlist.printList();
newlist.reverseList();
cout << "\nAfter reversion,the new list is " ;
newlist.printList();
cin >> num;
return 0;
}
我自己写的程序,不是最好的,我想还是可以的,你看着用吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询