这篇文章本作者操作字符串时使用的时string类,以下代码若有更精简的思路欢迎大家评论/私聊。
1. 链式存储输入输出
#include <iostream>
using namespace std;
struct BookListNode{
string name;
string bno;
double price;
BookListNode* next;//下一节点指针
};
int main() {
BookListNode *head,*tail,*p;//声明头指针,尾指针,当前指针,为方便解题
head = new BookListNode;//创建头指针
head->next = NULL;//每创建一个节点都将其next指针标空,更规范
int n = 0;//计数器
tail = head;
while (true){
string name;
string bno;
double price;
cin>>bno>>name>>price;
if (name=="0"&&bno=="0"&&price==0)break;//循环结束条件 0 0 0
BookListNode *bookListNode = new BookListNode;
bookListNode->name = name;
bookListNode->bno = bno;
bookListNode->price = price;
bookListNode->next = NULL;
//添加到尾后
tail->next = bookListNode;
tail = tail->next;
n++;
}
cout<<n<<endl;
p = head->next;
while (p!=NULL){
printf("%s %s %.2lf\n",p->bno.c_str(),p->name.c_str(),p->price);//string类的pringtf输出方法:str.c_str()
p = p->next;
}
return 0;
}
2. 冒泡排序
注意到相同的输入,需要考虑到排序方法的稳定性。交换方式直接交换结点的内容,并且封装成方法,增添了指针参数的知识点。
#include <iostream>
using namespace std;
struct BookListNode{
string name;
string bno;
double price;
BookListNode* next;
};
void Swap(BookListNode *p,BookListNode *q){//交换功能 参数为指针才可以保证交换成功
string nameTemp = q->name;
string bnoTemp = q->bno;
double priceTemp = q->price;
q->bno = p->bno;
q->name = p->name;
q->price = p->price;
p->bno = bnoTemp;
p->name = nameTemp;
p->price = priceTemp;
}
int main() {
BookListNode *head,*tail,*p,*q,*count;
head = new BookListNode;
head->next = NULL;
tail = head;
while (true){
string name;
string bno;
double price;
cin>>bno>>name>>price;
if (name=="0"&&bno=="0"&&price==0)break;
BookListNode *bookListNode = new BookListNode;
bookListNode->name = name;
bookListNode->bno = bno;
bookListNode->price = price;
bookListNode->next = NULL;
tail->next = bookListNode;
tail = tail->next;
}
for (count = head ; count!=NULL;count = count->next) {//从头遍历到尾,次数相当于i=0;i<n;i++
p = head->next;
q = p->next;
while (q!=NULL){
if (p->price < q->price){
Swap(p,q);
}
p = p->next;
q = q->next;
}
}
p = head->next;
while (p!=NULL){
printf("%s %s %.2lf\n",p->bno.c_str(),p->name.c_str(),p->price);
p = p->next;
}
return 0;
}
3. 去重
依旧暴力双循环,可总认为这样不好。注意删除结点需要释放空间,尽管题目没有要求。
#include <iostream>
using namespace std;
struct BookListNode{
string name;
string bno;
double price;
BookListNode* next;
};
int main() {
BookListNode *head,*tail,*p,*q,*pre,*temp;
head = new BookListNode;
head->next = NULL;
int n;
cin>>n;
tail = head;
for(int i=0;i<n;i++){/有限输入
BookListNode *bookListNode = new BookListNode;
cin>>bookListNode->bno>>bookListNode->name>>bookListNode->price;
bookListNode->next = NULL;
tail->next = bookListNode;
tail = tail->next;
}
p = head->next;
while (p->next!=NULL){//双循环比对,注意留下前驱结点
pre = p;
q = p->next;
while (q!=NULL){
if (p->name==q->name&&p->bno==q->bno){
temp = q;
pre->next = pre->next->next;//利用前驱结点删除指定结点,并保持链表连续。
q = q->next;
free(temp);
n--;
} else{
pre = pre->next;
q = q->next;
}
}
p = p->next;
}
cout<<n<<endl;
p = head->next;
while (p!=NULL){
printf("%s %s %.2lf\n",p->bno.c_str(),p->name.c_str(),p->price);
p = p->next;
}
return 0;
}
4. 链式逆序存储
#include<iostream>
using namespace std;
struct ListNode{
string name;
string bno;
double price;
ListNode *next;
};
int main(){
ListNode *l,*p;
l = new ListNode;
l->next = NULL;
p=l;
int n;
cin>>n;
for (int i = 0; i < n; ++i) {
ListNode *node = new ListNode;
cin>>node->bno>>node->name>>node->price;
node->next = NULL;
p->next = node;
p = node;
}
p = l->next->next;
l->next->next = NULL;
ListNode *q;
while (p!=NULL){
q = p->next;
p->next = l->next;
l->next = p;
p = q;
}
p = l->next;
while (p!=NULL){
printf("%s %s %.2lf\n",p->bno.c_str(),p->name.c_str(),p->price);
p = p->next;
}
}