带有类和链表的简单C++程序
Simple C++ Program with Class and Linked List
有人可以帮我解决这个问题吗?我有一个班级项目,我应该在课堂和链表中编写一个 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
相关文章:
- 在c++中用vector填充一个简单的动态数组
- (C++)分析树以计算返回错误值的简单算术表达式
- 我的简单if-else语句是如何无法访问的代码
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 一种在C++中读取TXT配置文件的简单方法
- 关于简单C++函数(is_palindrome)的逻辑的问题
- 显示错误输出的简单数组排序程序
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 退出简单while循环时出现问题
- 为什么简单的算术减法在"if"条件下不起作用?
- C++-字符串是否包含一个带有简单循环的单词
- 关于 c++ 函数中指针赋值的简单问题
- 从函数返回任意简单类型的数据
- 如何在没有函数的情况下编写此代码并使C++更简单?
- 有没有办法简单地从 GPU 调用多个 cpp 输出文件?
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 如何使用 samtools C API 构建一个简单的主.cpp文件
- CPU 瓶颈;处理具有许多非静态对象的 3D 场景渲染的简单方法