sorting in linked list

avonuts 2006-11-04 11:43:02
有一个链表,想对里面number这个element从小到大进行排序,请各位高手帮忙,

文件:
number lastname firstname rate
012452 Wong Andrew 23.56
891756 Simmons Angela 24.94
268174 Tindall Jeremy 18.56
718756 Ting Susan 27.86
261786 Murdock Brian 25.98

这是我的code,可是不work.
void sorting(record **headptr, int number)
{
record *temptr = NULL;
record *prv = NULL;
record *loc = NULL;
record *curr= NULL;
record *pos = NULL;

if (*headptr != NULL)
{
*headptr = temptr;
(*headptr) -> next = prv;
}

while( prv != curr)
{
for (pos = *headptr; pos != NULL; pos++)
{
if( prv->number > curr->number)
{
curr->next = loc;
temptr->next = curr;
curr->next = prv;
prv->next = loc;
}
}
temptr = *headptr;
}
}
...全文
352 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wukexin 2006-11-07
  • 打赏
  • 举报
回复
/*
如果是学习,可以重写算法,如果只是应用用C++标准库吧,又安全高效,又漂亮。

想要读一个文件到链表,再打印,可是结果老是打261786这个号码,请各位高手帮忙解决以下.
文件:
012452 Wong Andrew 23.56
891756 Simmons Angela 24.94
268174 Tindall Jeremy 18.56
718756 Ting Susan 27.86
261786 Murdock Brian 25.98
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
//
//
class Crecord
{
public:
unsigned int index() const { return m_number;}
double rate() const { return m_rate; }
protected:
friend std::istream& operator>> (std::istream& in, Crecord& aim);
friend std::ostream& operator<< (std::ostream& out, const Crecord& source);
private:
unsigned int m_number;
std::string m_last_name;
std::string m_first_name;
double m_rate;
};
// The date will be read from a stream in the following form Crecord
std::istream& operator>>(std::istream& in, Crecord& aim){
using namespace std;
in>>aim.m_number>>aim.m_last_name>>aim.m_first_name>>aim.m_rate;
return in;
}
//
std::ostream& operator<< (std::ostream& out, const Crecord& source){
using namespace std;
out<<source.m_number<<"\t"<<source.m_last_name<<"\t"
<<source.m_first_name<<"\t"<<source.m_rate;
return out;
}
//
//排序规则可以自定义为一个Rule类
class Rule : public std::binary_function< Crecord, Crecord, bool >{
public:
bool operator() (const Crecord& arg1, const Crecord& arg2)
{
//return ( arg1.index() < arg2.index() ); //按学好排序
return ( arg1.rate() < arg1.rate() ); //按分数排序
}
};
//
int main(int argc, char *argv[])
{
using namespace std;
if( 2 == argc ){
fstream in(argv[1]);
Crecord record;
vector<Crecord> ls;
while( in>>record ){
ls.push_back(record);
}
sort(ls.begin(),ls.end(),Rule() );
std::copy(ls.begin(),ls.end(),std::ostream_iterator<Crecord>(std::cout,"\n") );
return EXIT_SUCCESS;
}
return EXIT_SUCCESS;
}
avonuts 2006-11-05
  • 打赏
  • 举报
回复
多谢楼上指教,现在可以了,不过还有一个问题,我把上面的number改成rate后,就变成
Number Last Name First Name Rate
-------+--------------------+--------------------+-----
12452 Wong Andrew 23.56
268174 Tindall Jeremy 18.56
891756 Simmons Angela 24.94
261786 Murdock Brian 25.98
718756 Ting Susan 27.86
这样了,第一个和第二个无法倒过来.排的不正确.
avonuts 2006-11-05
  • 打赏
  • 举报
回复
up
avonuts 2006-11-04
  • 打赏
  • 举报
回复
指针这样操作,不行吧

用 for (pos = *headptr; pos != NULL; pos++)是否可以用于指针上??

熟悉一下冒泡排序吧
---
相邻两个比较,(是否)交换位置,每趟循环求出一个最大(小)值

恩,这个算法我很多同学都用,可是我搞不出来.
lei001 2006-11-04
  • 打赏
  • 举报
回复
指针这样操作,不行吧
飞哥 2006-11-04
  • 打赏
  • 举报
回复
熟悉一下冒泡排序吧
---
相邻两个比较,(是否)交换位置,每趟循环求出一个最大(小)值

飞哥 2006-11-04
  • 打赏
  • 举报
回复
楼主会冒泡排序吗?
---
一个意思
飞哥 2006-11-04
  • 打赏
  • 举报
回复
关键就是这段了·
---
Linklist bubblesort(Linklist head)
{ Node *p,*q,*tail,*s;
tail=NULL;
while(head->next!=tail)
{
p=head;
q=p->next;
while(q->next!=tail)
{
if(p->next->data>q->next->data)
{ s=q->next;
p->next=q->next;
q->next=q->next->next;
p->next->next=q;
q=s;
}
p=p->next;
q=q->next;
}
飞哥 2006-11-04
  • 打赏
  • 举报
回复
过程跟冒泡排序就是一个
甚至你把那个代码拿过来改改就行

关键就是交换元素的地方不一样而已,至于循环的那个条件就不算不一样了把

--
经典算法------单链表冒泡排序


#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}Node,*Linklist;
Linklist createlist(int n)
{ Node *p,*q,*head;
int i=0,x;
head=(Node *)malloc(sizeof(Node));
head->next=NULL;
q=head;
printf("请输入排序的数字:\n");
for(i=0;i<n;i++)
{ scanf("%d",&x);
p=(Node *)malloc(sizeof(Node));
p->data=x;
p->next=NULL;
q->next=p;
q=p;
p=NULL;
}
return head;
}
Linklist bubblesort(Linklist head)
{ Node *p,*q,*tail,*s;
tail=NULL;
while(head->next!=tail)
{
p=head;
q=p->next;
while(q->next!=tail)
{
if(p->next->data>q->next->data)
{ s=q->next;
p->next=q->next;
q->next=q->next->next;
p->next->next=q;
q=s;
}
p=p->next;
q=q->next;
}
tail=q;
}

}
void output(Linklist head)
{ Linklist p;
p=head->next;
while(p)
{printf("%d\t",p->data);
p=p->next;
}
}
main()
{int n;
Node *head;
printf("请问你要输入几个排序数:\n");
scanf("%d",&n);
head=createlist(n);
printf("排序前:\n");
output(head);
bubblesort(head);
printf("\n排序后:\n");
output(head);
}
This book is about the usage of data structures and algorithms in computer programming. Designing an efficient algorithm to solve a computer science problem is a skill of Computer programmer. This is the skill which tech companies like Google, Amazon, Microsoft, Adobe and many others are looking for in an interview. This book assumes that you are a C++ language developer. You are not an expert in C++ language, but you are well familiar with concepts of references, functions, arrays and recursion. In the start of this book, we will be revising the C++ language fundamentals that will be used throughout this book. We will be looking into some of the problems in arrays and recursion too. Then in the coming chapter, we will be looking into complexity analysis. Then will look into the various data structures and their algorithms. We will be looking into a linked list, stack, queue, trees, heap, hash table and graphs. We will be looking into sorting, searching techniques. Then we will be looking into algorithm analysis, we will be looking into brute force algorithms, greedy algorithms, divide and conquer algorithms, dynamic programming, reduction, and backtracking. In the end, we will be looking into the system design that will give a systematic approach for solving the design problems in an Interview. Table of Contents CHAPTER 1: INTRODUCTION - PROGRAMMING OVERVIEW CHAPTER 2: ALGORITHMS ANALYSIS CHAPTER 3: APPROACH TO SOLVE ALGORITHM DESIGN PROBLEMS CHAPTER 4: ABSTRACT DATA TYPE & C++ COLLECTIONS CHAPTER 5: SEARCHING CHAPTER 6: SORTING CHAPTER 7: LINKED LIST CHAPTER 8: STACK CHAPTER 9: QUEUE CHAPTER 10: TREE CHAPTER 11: PRIORITY QUEUE CHAPTER 12: HASH-TABLE CHAPTER 13: GRAPHS CHAPTER 14: STRING ALGORITHMS CHAPTER 15: ALGORITHM DESIGN TECHNIQUES CHAPTER 16: BRUTE FORCE ALGORITHM CHAPTER 17: GREEDY ALGORITHM CHAPTER 18: DIVIDE-AND-CONQUER, DECREASE-AND-CONQUER CHAPTER 19: DYNAMIC PROGRAMMING CHAPTER 20: BACKTRACKING AND BRANCH-AND-BOUND CHAPTER 21: COMPLEXITY THEORY AND NP COMPLETENESS CHAPTER 22: INTERVIEW STRATEGY CHAPTER 23: SYSTEM DESIGN
Title: Problem Solving in Data Structures & Algorithms Using C++: Programming Interview Guide Language: English Publisher: CreateSpace Independent Publishing Platform Publication Date: 2017-01-08 ISBN-10: 1542396476 ISBN-13: 9781542396479 This book is about the usage of data structures and algorithms in computer programming. Designing an efficient algorithm to solve a computer science problem is a skill of Computer programmer. This is the skill which tech companies like Google, Amazon, Microsoft, Adobe and many others are looking for in an interview. This book assumes that you are a C++ language developer. You are not an expert in C++ language, but you are well familiar with concepts of references, functions, arrays and recursion. In the start of this book, we will be revising the C++ language fundamentals that will be used throughout this book. We will be looking into some of the problems in arrays and recursion too. Then in the coming chapter, we will be looking into complexity analysis. Then will look into the various data structures and their algorithms. We will be looking into a linked list, stack, queue, trees, heap, hash table and graphs. We will be looking into sorting, searching techniques. Then we will be looking into algorithm analysis, we will be looking into brute force algorithms, greedy algorithms, divide and conquer algorithms, dynamic programming, reduction, and backtracking. In the end, we will be looking into the system design that will give a systematic approach for solving the design problems in an Interview.

33,318

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧