i wan to know the code for the radix sort using a queue and a radix sort using a linked list
happy8899 0 Newbie Poster
Dani AI
Generated
Good starting point, . As suggested, once you can store numbers in your list, the LSD (least-significant-digit) radix sort is straightforward with queues/lists. Use 10 buckets (0-9). For each digit position, dequeue every element from your main list, drop it into bucket[digit], then concatenate buckets back to the main list in order 0..9. Repeat for each power of 10 until you have processed the largest number of digits. Singly linked lists are fine; you do not need a doubly linked list for this.
Two quick fixes to avoid head-scratchers before sorting: in your addToHead, set tail = head when the list was empty (not tail == head). In deleteFromTail, advance tmp to the node before tail, then delete the old tail, set tail = tmp, and set tail->next = 0. Those keep your structure consistent so the bucket shuffling below works reliably.
Here is a compact, list-only LSD radix sort that uses your IntSLList API (no iterators needed). It also shows a trick hinted at: rotate elements by delete-from-head/add-to-tail to traverse without exposing internals.
void radixSort(IntSLList& a) {
if (a.empty()) return;
IntSLList tmp, buckets[10];
int n = 0, maxv = 0;
// Discover n and maxv without losing order
while (!a.empty()) {
int x = a.deleteFromHead();
if (x > maxv) maxv = x;
tmp.addToTail(x);
++n;
}
while (!tmp.empty()) a.addToTail(tmp.deleteFromHead());
// LSD passes
for (int exp = 1; maxv / exp > 0; exp *= 10) {
for (int i = 0; i < n; ++i) {
int x = a.deleteFromHead();
int d = (x / exp) % 10;
buckets[d].addToTail(x);
}
for (int d = 0; d < 10; ++d)
while (!buckets[d].empty())
a.addToTail(buckets[d].deleteFromHead());
}
} Notes:
- Assumes non-negative ints. For negatives, bucket positives and negatives separately; sort abs(negatives), then append them back in reverse, followed by positives.
- If your instructor requires a queue abstraction, replace IntSLList buckets with your linkedList<int> wrapper; the pass logic stays the same.
Recommended Answers
Jump to Post— Salem 6,009Wow - 3 in a row.
http://www.daniweb.com/techtalkforums/announcement8-2.html
Jump to Post— Grunt 19Where are you stuck?
All 6 Replies
Salem 6,009 Posting Sage
Grunt 19 Junior Poster
Where are you stuck?
happy8899 0 Newbie Poster
Where are you stuck?
i dunno how to develope a program that need to use linked list both the single linked list and double linked list which need to enter the list of number using linked list and using radix sort that is developed using linked list
Grunt 19 Junior Poster
i dunno how to develope a program that need to use linked list both the single linked list and double linked list which need to enter the list of number using linked list and using radix sort that is developed using linked list
Buddy we can't explain everything to you here. For that you have to look into the books. If you have a specific doubt, post here. Definetly someone here will help you out.
Salem 6,009 Posting Sage
Well can you do the "populate a linked list with some data" part of the problem?
There's no point talking about the "sort" side of things until you can do that.
I mean, "here is my linked list code", how do I radix sort it would at least show some effort on your part.
happy8899 0 Newbie Poster
Well can you do the "populate a linked list with some data" part of the problem?
There's no point talking about the "sort" side of things until you can do that.
I mean, "here is my linked list code", how do I radix sort it would at least show some effort on your part.
first linked list
# include <iostream>
using namespace std;
#ifndef DLL_QUEUE
#define DLL_QUEUE
#include <list>
template <class T>
class linkedList
{
public:
linkedList()
{
}
void clear()
{
lst.clear();
}
bool empty() const
{
return lst.empty();
}
T& front()
{
return lst.front();
}
T dequeue()
{
T el = lst.front();
lst.pop_front();
return el;
}
void enqueue(const T& el)
{
lst.push_back(el);
}
private:
list<T> lst;
};
#endif
# ifndef INT_LINKED_LIST
# define INT_LINKED_LIST
using namespace std;
class IntSLLNode
{
public:
int info;
IntSLLNode *next;
IntSLLNode (int el, IntSLLNode *ptr = 0)
{
info = el;
next =ptr;
}
};
class IntSLList
{
public:
IntSLList()
{
head = tail = 0;
}
~IntSLList();
int empty()
{
return head ==0;
}
void addToHead(int);
void addToTail(int);
int deleteFromHead();
int deleteFromTail();
void deleteNode(int);
bool isInList(int) const;
void insertNode(int&);
void print();
int get();
private:
IntSLLNode *head, *tail;
int t;
};
IntSLList::~IntSLList()
{
for(IntSLLNode *p; !empty();)
{
p = head->next;
delete head;
head = p;
}
}
void IntSLList::addToHead(int el)
{
head = new IntSLLNode(el, head);
if(tail == 0)
tail == head;
}
void IntSLList::addToTail(int el)
{
if(tail!= 0)
{
tail->next = new IntSLLNode(el);
tail = tail->next;
}
else head = tail = new IntSLLNode(el);
}
void IntSLList::deleteNode(int el)
{
if(head!=0)
if(head == tail && el == head->info)
{
delete head;
head = tail =0;
}
else if (el == head->info)
{
IntSLLNode *temp = head;
head = head->next;
delete temp;
}
else
{
IntSLLNode *pred, *tmp;
for (pred = head, tmp = head->next; tmp!=0 && !(tmp->info == el);
pred = pred->next, tmp = tmp->next);
if (tmp!=0)
{
pred->next = tmp->next;
if(tmp == tail)
tail = pred;
delete tmp;
}
}
}
int IntSLList::deleteFromHead()
{
int el = head->info;
IntSLLNode *tmp = head;
if(head == tail)
head = tail = 0;
else head = head->next;
//cout<<" "<<head->info;
delete tmp;
return el;
}
int IntSLList::deleteFromTail()
{
int el = tail->info;
if (head == tail)
{
delete head;
head = tail = 0;
}
else
{
IntSLLNode *tmp;
for (tmp = head; tmp->next !=tail; tmp = tmp->next)
delete tail;
tail = tmp;
tail->next=0;
}
return el;
}
void IntSLList::print()
{
IntSLLNode *current;
current = head;
while (current != NULL)
{
cout<< current->info<<" ";
current = current->next;
}
}
int IntSLList::get()
{
IntSLLNode *current;
IntSLLNode *yy;
current = head;
yy = current;
cout<<"get";
t = yy->info;
cout<<endl<<t<<" ggggg "<<endl;
yy = yy->next;
cout<<yy->info;
//current->next = NULL;
//head = head->next;
//delete current;
return t;
}
#endif
here is my linked list how to use it in radix sort to sort a list
Edited by Dani because: Formatting fixed
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.