带有类和链表的简单C++程序

Simple C++ Program with Class and Linked List

本文关键字:简单 C++ 程序 链表      更新时间:2023-10-16

有人可以帮我解决这个问题吗?我有一个班级项目,我应该在课堂和链表中编写一个 Casewell Bill。在找到此链接之前,我无法弄清楚。它的输出非常棒,但是Idk如何添加cin>>用户输入,例如成人人数并计算食物总数。我试图修复它,但它无法正常工作 http://www.sourcetricks.com/2008/07/c-singly-linked-lists.html#.WYzSxneGOHo

//-----------------------------------------

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
class List
{
private:
int number;
char* word;
List* next;
public:
void inserts(int num, char* text);
void removes(int num);
void print();
};
List* first;
void List::print() {
cout <<"This is our list:"<<endl;
// Temp pointer
List *tmp = first;
// No nodes
if ( tmp == NULL ) {
cout << "EMPTY list" << endl;
return;
}
// One node in the list
if ( tmp->next == NULL ) { //on the first node of the list
cout <<"NUMBER:t "<< tmp->number;
cout <<"tWORD:t"<< tmp->word << endl;
cout <<"--------------------------------"<<endl;
}
else {
// Parse and print the list
while ( tmp != NULL ){ //the rest of the list
cout <<"NUMBER:t"<< tmp->number;
cout <<"tWORD:t"<< tmp->word << endl;
cout <<"--------------------------------"<<endl;
tmp = tmp->next;
}
}
}
void List::inserts(int num, char* word){
// Create a new list
List* newlist = new List; 
newlist->number=num;
newlist->word=word;

newlist->next=NULL;
// Create a temp pointer
List *tmp = first;
if ( tmp != NULL ) {
// Nodes already present in the list
// Parse to end of list
while ( tmp->next != NULL ) {
tmp = tmp->next;
}
// Point the last node to the new node
tmp->next=newlist;
}
else {
// First node in the list
first = newlist;
}
}
void List::removes(int num){
int k = 0;
List* tmp=first;
if(tmp==NULL) {
return;
//Last node of the list
}
if ( tmp->next == NULL && tmp->number==num ) {
delete tmp;
first = NULL;
}
else {
//Parse thru the nodes
List* prev;
prev = new List;
while ( tmp != NULL ) {
if ( tmp->number == num && k == 0) {
first = first->next;
}
if ( tmp->number == num){
break;
prev = tmp;
tmp = tmp->next;
k++;
}
}//end of while 
//Adjust the pointers
prev->next=(tmp->next);
//Delete the current node
delete tmp;
delete prev;
}//end of else
}//end of void remove

int main ()
{
first->print();
first->inserts(1200,"endian");
first->print();
first->inserts(10,"endianness");
first->inserts(1200,"PEEK");
first->inserts(1200,"POKE");
first->inserts(1200,".MIL");
first->print();
first->removes(100);
first->print();
getchar();
}

和带有警告的输出

5 warnings generated.
This is our list:
EMPTY list
This is our list:
NUMBER:     1200   WORD:   endian
--------------------------------
This is our list:
NUMBER:    1200    WORD:   endian
--------------------------------
NUMBER:    10  WORD:   endianness
--------------------------------
NUMBER:    1200    WORD:   PEEK
--------------------------------
NUMBER:    1200    WORD:   POKE
--------------------------------
NUMBER:    1200    WORD:   .MIL
--------------------------------

我想你只是想使用cin,为此你必须声明你想要的明确类型的输入.例如,正如你所说你需要数字,它可以是 intcin的使用

List list;
int number;
cout<<"Append a number n";
cin>>number;
double product = number * 2;

带有输入的正确缩进代码(使用 cin(

#include <iostream>
using namespace std;
// Node class
class Node {
int data;
Node* next;
public:
Node() {};
void SetData(int aData) { data = aData; };
void SetNext(Node* aNext) { next = aNext; };
int Data() { return data; };
Node* Next() { return next; };
};
// List class
class List {
Node *head;
public:
List() { head = NULL; };
void Print();
void Append(int data);
void Delete(int data);
};
/**
* Print the contents of the list
*/
void List::Print() {
// Temp pointer
Node *tmp = head;
// No nodes
if ( tmp == NULL ) {
cout << "EMPTY" << endl;
return;
}
// One node in the list
if ( tmp->Next() == NULL ) {
cout << tmp->Data();
cout << " --> ";
cout << "NULL" << endl;
}
else {
// Parse and print the list
do {
cout << tmp->Data();
cout << " --> ";
tmp = tmp->Next();
}
while ( tmp != NULL );
cout << "NULL" << endl;
}
}
/**
* Append a node to the linked list
*/
void List::Append(int data) {
// Create a new node
Node* newNode = new Node();
newNode->SetData(data);
newNode->SetNext(NULL);
// Create a temp pointer
Node *tmp = head;
if ( tmp != NULL ) {
// Nodes already present in the list
// Parse to end of list
while ( tmp->Next() != NULL ) {
tmp = tmp->Next();
}
// Point the last node to the new node
tmp->SetNext(newNode);
}
else {
// First node in the list
head = newNode;
}
}
/**
* Delete a node from the list
*/
void List::Delete(int data) {
// Create a temp pointer
Node *tmp = head;
// No nodes
if ( tmp == NULL )
return;
// Last node of the list
if ( tmp->Next() == NULL ) {
delete tmp;
head = NULL;
}
else {
// Parse thru the nodes
Node *prev = nullptr;
do {
if ( tmp->Data() == data ) break;
prev = tmp;
tmp = tmp->Next();
} while ( tmp != NULL );
// Adjust the pointers
prev->SetNext(tmp->Next());
// Delete the current node
delete tmp;
}
}
int main()
{
// New list
List list;
int number;
cout<<"Append a number n";
cin>>number;
double product = number * 2;
list.Append(number);
list.Print();
list.Append(product);
list.Print();
// Append nodes to the list
list.Append(100);
list.Print();
list.Append(200);
list.Print();
list.Append(300);
list.Print();
list.Append(400);
list.Print();
list.Append(500);
list.Print();
// Delete nodes from the list
list.Delete(400);
list.Print();
list.Delete(300);
list.Print();
list.Delete(200);
list.Print();
list.Delete(500);
list.Print();
list.Delete(100);
list.Print();
}

输出

Append a number 
2
2 --> NULL
2 --> 4 --> NULL
2 --> 4 --> 100 --> NULL
2 --> 4 --> 100 --> 200 --> NULL
2 --> 4 --> 100 --> 200 --> 300 --> NULL
2 --> 4 --> 100 --> 200 --> 300 --> 400 --> NULL
2 --> 4 --> 100 --> 200 --> 300 --> 400 --> 500 --> NULL
2 --> 4 --> 100 --> 200 --> 300 --> 500 --> NULL
2 --> 4 --> 100 --> 200 --> 500 --> NULL
2 --> 4 --> 100 --> 500 --> NULL
2 --> 4 --> 100 --> NULL
2 --> 4 --> NULL
Program ended with exit code: 0