试着写了个链表.cpp
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.29)
|
||||
project(homework2)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
|
||||
add_executable(Test_LinkList LinkLits.cpp
|
||||
Test_LinkList.cpp)
|
||||
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
#include<iomanip>
|
||||
using namespace std;
|
||||
|
||||
typedef struct bookNode {
|
||||
string id;
|
||||
string name;
|
||||
double price;
|
||||
bookNode *next;
|
||||
} book_node, *bookList;
|
||||
|
||||
bool is_list_empty(bookList *l) {
|
||||
if ((*l)->next == NULL) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void display(bookList *l) {
|
||||
if (is_list_empty(l)) {
|
||||
printf("List is empty\n");
|
||||
} else {
|
||||
bookNode *temp = (*l)->next;
|
||||
while (temp != NULL) {
|
||||
cout << temp->id << " " << temp->name << " " << fixed << setprecision(2) << temp->price << endl;
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include "LinkLits.cpp"
|
||||
using namespace std;
|
||||
|
||||
void insert(bookList *l) {
|
||||
bookNode *temp = (*l)->next;
|
||||
while (temp != NULL) {
|
||||
cin >> (*temp).id >> (*temp).name >> (*temp).price;
|
||||
if (!(((*temp).id == "" && (*temp).name == "" && (*temp).price == 0))) {
|
||||
temp = (*temp).next;
|
||||
} else {
|
||||
temp = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
bookList books = new bookNode;
|
||||
books->next = NULL;
|
||||
insert(&books);
|
||||
display(&books);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user