| | |
merge sort/linked lists
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 108
Reputation:
Solved Threads: 1
Hello,
I need to make this program sort the numbers entered with merge sort then print them. I am having trouble calling merge sort and getting it to print.
Here is the code:
I need to make this program sort the numbers entered with merge sort then print them. I am having trouble calling merge sort and getting it to print.
Here is the code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include "queueLinkedList.cpp" using namespace std; /*struct Item : Data { int value; Item *next; };*/ class mergeSort : public Queue { public: Data* divide(Data*); Data* merge(Data*, Data*); Data* mergesort(Data*); Data* mergesort(void); Queue queue; }; Data * mergeSort::divide(Data *a) { Data *b, *c; b = a; c = a->next; c = c->next; while(c != NULL) { c = c->next; b = b->next; if(c != NULL) c = c->next; } c = b->next; b->next = NULL; return c; } Data * mergeSort::merge(Data *p, Data *q) { Data *r, *start; int num; if(p->value <= q->value) { r = p; num = p->value; addToQueue(num); p = p->next; print(); } else { r = q; num = q->value; addToQueue(num); q = q->next; } start = r; while(p != NULL && q != NULL) { if(p->value <= q->value) { r->next = p; r = p; p = p->next; } else { r->next = q; r = q; q = q->next; } } if(p == NULL) r->next = q; else r->next = p; return start; } Data * mergeSort::mergesort(Data *p) { Data *q; if(p != NULL) { if(p->next != NULL) { q = divide(p); p = mergesort(p); q = mergesort(q); p = merge(p, q); } } return p; } Data * mergeSort::mergesort() { mergeSort(top); } int main() { int number = -1; Data *list; mergeSort sort; while(number != 0) { cout << "Enter a number: " << endl; cin >> number; if(number != 0) { sort.addToQueue(number); } } cout << "Results: " << endl; sort.print(); sort.mergesort(); cout << "Sorted results: " << endl; sort.print(); system("pause"); }
C++ Syntax (Toggle Plain Text)
/********************************************** * A program that stores data to a linked list * * Tests if linked list is full or empty * * Adds or removes data from linked list * * Author: Kimberlie Davis * * Version: 3/26/09 * ***********************************************/ #include <iostream> using namespace std; struct Data { int value; Data *next; }; class Queue { private: int fill, remove; Data *rear; public: Queue(void); void initialize(void); int takeAway(void); bool full(void); bool empty(void); void addToQueue(int); Data *top; int print(void); //Data * returnTop(void); }; /* * Constructor * Initializes fill, remove and count */ Queue::Queue(void) { top = NULL; rear = NULL; } /* * method initialize * Reinitializes the variables */ void Queue::initialize() { } /* * Method takeAway * Removes items from the list */ int Queue::takeAway() { Data *p; char letter; initialize(); if(top == NULL) cout << "Queue Overflow" << endl; else { p = top; letter = p->value; top = p->next; delete p; if(top == NULL) rear = NULL; return letter; } } /* * method full * Returns true if list is full */ bool Queue::full() { if(rear == NULL) { cout << "Queue Overflow" << endl; return true; } else return false; } /* * method empty * returns true if list is empty */ bool Queue::empty() { if(top == NULL) { cout << "Queue Empty" << endl; return true; } else return false; } /* * method addToQueue * Takes in value from user * Adds the value to the list */ void Queue::addToQueue(int number) { Data *p; p = new Data; p->value = number; if(top == NULL) { top = p; rear = p; } else { rear->next = p; rear = p; } p->next = NULL; } /*Data * Queue::returnTop() { return top; }*/ int Queue::print() { Data *p; p = new Data; p = top; // Data *p; //p = new Data; int num; while(top != NULL) { num = p->value; top = p->next; p = p->next; cout << num << endl; } }
![]() |
Similar Threads
- Merge and sort 2 linked lists (Pascal and Delphi)
- Merge sort using linked lists (C++)
- Help with Linked List Project! (C++)
Other Threads in the C++ Forum
- Previous Thread: Linked Lists problem
- Next Thread: C++ and BigNums
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count database delete deploy desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph homeworkhelp homeworkhelper iamthwee ifstream input int integer lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





