C++ :: Custom Linked List Sort Method

Apr 3, 2013

I am making a custom linked list (for fun!) and want to implement a sort method that sorts the stored data using the stored types > and < operators (will require the type to have these operators overloaded)

What is the best way to do this? I guess the first thing one might jump to would be to compare the current node with the the node to its "right" see if one is greater than the other. Then keep iterating through the list until you don't swap any more nodes. However I am thinking there is probably a more efficient way to do this. Here is my code so far:

#ifndef LIST_HPP
#define LIST_HPP
#include <string>

[Code].....

Surely there is a way to sort with only one iteration through the list?

View 2 Replies


ADVERTISEMENT

C++ :: Creating Custom Templated Doubly Linked List

Feb 20, 2013

I need to create a templated doubly linked list, with an iterator class within the list class. This program is to function just like the STL list class but I only need to implement functions that I am using, My trouble is I am kind of clueless on the iterator part and the fact that the list is templated is giving me syntax grief. I have pasted the code I have done so far.

1. On the syntax implementing the list and iterator functions outside of the class
2. I am not sure when to deference the iterator in the functions, but think I have it right so far
3. For the reverse function can I copy the list into a new list in reverse then re add them to the original list overwriting the same values? I have the code I have so far there
4. For the iterator erase function, I am not sure if I am deleting the node correctly.
5. I am not sure if I need template <typename T> above the iterator functions. Does the iterator class need to be a template? Right now it is not.

// Templated doubly linked list class

#include <iostream>
using namespace std;
template <typename T>
class list {
private:
Node *head;
Node *tail;

[Code] ....

View 7 Replies View Related

C/C++ :: Linked List - Method To Add To File

Feb 21, 2013

I am trying to write a program which involves linked list. i have to create a method called add_aa( str ). I am reading from a text file. in the text file it just contains the values for str. what I am trying to do is create the method add_aa( str ) and add what corresponds to str from the file. here is what the output should look like. and what i have is very basic. here is what i have

inline void LList:: add_aa(const string _str) {
    cout << " p = " << std::hex << p << std::dec << '
'; 
}

I'm thinking using a for statement, but how to incorporate it

Attached Images : sample.JPG (35.2 KB)

View 1 Replies View Related

C++ :: Linked List Sort By Age

Mar 22, 2013

I know how to add a node and delete a node from the beginning and from the end of the List. Now I want to sort the List by Age. I tried everything but there must be a very little mistake that I can't find. Here is my code:

#include <iostream>
using namespace std;
struct Node {
char name[20];
int age;
double height;

[Code] ....

View 13 Replies View Related

C++ :: Quick Sort Using Linked List?

Feb 23, 2013

I've been trying to implement a quick sort algorithm for linked list but when i try to run it with 1000000 values my compiler tells me its running too long and ends up not finishing. However when i run it with 10 values it seems to work fine.

Is there anyway to improve this run time?

Code: struct listnode *quicksort( struct listnode *data ) {
size_t length = 0;
struct listnode *pivNode, *temp = NULL, *low = NULL, *high = NULL, *ltail = NULL, *htail = NULL, *prev = NULL, *end = NULL, *next;
// Get length
for( struct listnode *cursor = data; cursor != NULL; cursor = cursor->next ) {
length++;
}

[code].....

View 2 Replies View Related

C :: Merge - Array Sort On Linked List

Oct 15, 2014

This is in response to the bubble sort and selection sorts for linked lists. On my system, (Intel 2600K, 3.4ghz), it sorts a list with 4,194,304 nodes containing 64 bit unsigned integers in about 1.05 seconds.

Code:
#define NUMLISTS 32
/* number of lists */
typedef unsigned long long UI64;
typedef struct NODE_{
struct NODE_ * next;
UI64 data;

[Code]....

View 6 Replies View Related

C :: Sorting Linked List Using Quick Sort

Mar 4, 2014

I am trying to sort a linked list using quick sort in C. Here is my code--Actually, first I am inserting data in the list from a file. For a small file, it's working fine. But for large file it's just not working.

Code:
struct node {
int data;
struct node *link;
struct node *plink;

[Code] .....

View 1 Replies View Related

C++ :: Applying Bubble Sort On Linked List

Feb 27, 2015

I'm trying to apply a bubble sort on a linked list. It works in the first traversal, but then after the code cPtr = nPtr;, it inputs repeated digits at the end of the (semi-sorted) linked lists.

View 1 Replies View Related

C++ :: In Place Merge Sort For Linked List

Oct 3, 2014

This is in-place merge sort, for merge function.

LinkedListNode::LinkedListNode(int value) {
this->next = NULL;
this->value = value;
}
LinkedListNode *mergeSortedLinkedLists(LinkedListNode *firstList, LinkedListNode *secondList)

[Code] ....

View 3 Replies View Related

C++ :: Sort Function For Linked List Program?

Oct 7, 2013

I'm having trouble getting my sort function to work,

void Linkedlist::sort(int num)
{
node *q;
node *a;
int input=num;

[Code].....

I get a crash and I've narrowed it down to the last if statement. Also this function is meant to handle new members after 1 member has been added.

View 7 Replies View Related

C++ :: Linked List - How To Sort Nodes In Ascending Order

Apr 18, 2013

At the line number 65 that's my sort method first i sum up all the value in the nodes after that i want to sort the Nodes In ascending order but the method is not working ...

#include <iostream>
#include <conio.h>
using namespace std;
// Node Class

[Code] ....

View 3 Replies View Related

C/C++ :: Insert Sort Alphabetically Using Doubly Linked List

Mar 16, 2014

is this correct? I used this sorting with numbers i don't know if it is the same with strings. When I run it, there are no errors detected, but when i try to view it, the inputs does not appear.

void add(node **h, node **t){
node *temp, *ptr;
char s[20];
temp = (node*) malloc(sizeof(node));
printf ("-INSERT-");
printf("Fruit: ");
scanf("%s", temp->fruit);

[Code] .....

View 1 Replies View Related

C :: Simple Program To Bubble Sort Values Of A Linked List

Nov 30, 2013

HelI have been tasked with creating a program which (1) takes in integer values from a user (until the user enters -1) and inputs these values into a linked list. This (2)original list is then to be printed out. The program then uses the algorithm "bubble sort" to (3)order the list in descending order before finally printing it out again.

I have managed to do this but I kind of cheated since I do not quite understand how to manipulate a linked list. What did was I took the values in the linked list and transferred them into an array and then did bubble sort on that array.how to do bubble sort on a linked list as well as how to print a linked list.

Code:

#include<stdio.h>#include<stdlib.h>
typedef struct node
{
int info;
struct node *link;

}Node, *NodePointer;
void printList (NodePointer head)
}

[code]...

View 4 Replies View Related

C :: How To Sort Data In Linked List And Arrange Them Highest To Lowest

Aug 12, 2013

i am making a program that reads a file from .txt and print them out using linked list. However, i need to sort them from the highest price to lowest price.

Code:

/* my structs */
typedef struct{
Node *head;
Node *tail;
Node *iterator;
int size;
} List;

[Code]...

i know its long but im afraid that i might miss out some things.

View 12 Replies View Related

C/C++ :: Accept Items In Singly Linked List And Sort Nodes?

Jan 11, 2013

i want to make a program that accepts 15 items in a singly linked and sorts the nodes.

View 1 Replies View Related

C++ :: Using STL Sort With Custom Classes?

Oct 8, 2013

I've written a doubly linked list per my assignment instructions. I've implemented begin() and end() iterators for it, and they work with no problems when traversing the list. However, I need to sort the elements in the list. We are allowed to use the sort function defined in the <algorithm> header since we haven't yet covered sorting algorithms.

But, I'm running into a ton of problems. I figured as long as the begin() and end() iterators were defined for the list, then sort(list.begin(), list.end(), compare) would do the trick. The main errors I'm getting are:

error: no type named iterator_category
error: no type named value_type
error: no type named difference_type
error: no type named pointer
error: no type named reference

And also errors for no match of the + and - operators for the iterator class.

I understand reference, pointer, and value_type, but I have no idea about iterator_category and difference_type. Additionally, I'm a little unsure why the + and - operators need to be overloaded.

View 8 Replies View Related

C++ :: Sort Of Vector Of Custom Objects

Feb 23, 2014

I'm working on a code for ascertaining the minimum penalty of an assignment problem. The basic complication of my code is this: I have a vector of objects of a custom struct. The struct has a member, which is an integer. I need to keep the vector sorted according to that member, even when objects are added to or deleted from the vector. To illustrate the problem, I'll give an example.

Code:

typedef struct examplestruct{int i;
char c;
...} es;
int function(void)
{vector<es> ObjectTable;
//insert an object so that the vector remains sorted according to i
insertobject( newobject, &ObjectTable);
//deleting the top element of the vector
deleteobject(&ObjectTable);
return 0;}

I have tried to do it using bubblesort. But it's too slow. How to make a heap out of it.

The detailed premises of the problem is this: There are a number of jobs, and with each job a completion time and a cost coefficient. We are to ascertain the optimal sequence of jobs for which the penalty is minimum. Now, suppose we are given jobs A, B, C, D and E. We find out the lower bound of penalties for all the jobs.

Suppose we find B has the lowest penalty. Then we find out the lower bound of penalties for BA, BC, BD and BE. We continue this until we have the best value and a complete sequence. The way I have implemented this in a code: I have created two structs. One is Job, with the completion time and cost coefficient as members. The other is Node. Nodes have a Job Array and a Penalty as members. Now, we have a vector of Nodes which we need to keep sorted according to the penalty. We need to insert new Nodes and delete the expanded Nodes.

I have included my code. The pushInTable function inserts the new Nodes in a sorted vector. But it slows down remarkably when we give more than 20 jobs as input.

View 9 Replies View Related

C :: Sorting Strings Using Selection Sort Method?

Dec 7, 2013

I am trying to write a program to sort the characters in a word alphabetically. For example, if you input 'what', the computer will sort it into 'ahtw'. But, it fails to work. I didn't know why.

Code:

#include <stdio.h>
#include <string.h>
main() {

[Code].....

View 8 Replies View Related

C# :: Sort Array Of Random Numbers By Passing It To Method

Jun 28, 2012

I am trying to sort an array of random numbers by passing it to a method Sort(), sort the array, and then pass the entire array back to the calling program. Each time I run this though, the array doesn't seem to sort. I'm not sure if the problem lies with my sorting algorithm or if it has something to do with calling the function.

Code:

static void HighAverage(int[] a) {
for (int i = 0; i < a.Length; i++) {
if ((i + 1) % 10 == 0 && i != 0)

[Code].....

View 5 Replies View Related

C++ :: Creating A Linked List Of Common Elements From Two Other Linked Lists

Apr 29, 2013

I'm trying to write a function that takes two linked lists and creates a third one with only the common elements.

It assumes the first list (the caller) has no dups, but it doesn't seem to be working. The program doesn't crash, it just hangs when it is supposed to display L3 (the third list)..everything else runs and is displayed fine.

template <typename T>
LList <T> LList <T>:: common (LList <T> &B)//common fct
{
Node <T> *hunter1 = Head;

[Code]......

View 10 Replies View Related

C :: Insert Linked List Into Another Linked List

Jun 29, 2013

I have a linked list comprised of chars like so...

Code:

node1 - "p"
node2 - "o"
node3 - "p"

I need a function that will take in three perameters...node *replaceChar(node *head, char key, char *str)Stipulations of this function. head is the head of the list, 'key' and 'str' are guaranteed to contain alphanumeric characters only (A-Z, a-z, and 0-9). str can range from 1 to 1023 characters (inclusively). So if I call this function with these perameters..

Code:

node *head == /*the head of the list to be examined*/
char key == "p"char *str == "dog"The new list will look like this...
node1 - 'd'
node2 - 'o'
node3 - 'g'
node4 - 'o'
node5 - 'd'
node6 - 'o'
node7 - 'g'

All instances of 'p' were replaced with 'dog' I have a toString function which takes in a string and converts it to a linked list and returns the head. So assume that you can call the function on str = "dog" so...

Code:

toString(str) == /*this will return the head to the list made from the str*/

If it's unclear what my question is...I am stumped on how to write the replaceChar function the one that takes in three perameters..

View 3 Replies View Related

C# :: List Number Of Times A Method Is Called

Mar 17, 2015

I want to write how many times a method is called. The first class houses a method that I want to count from the second class how many times it is called. what I am able to achieve so far is that it only writes out how many times it is called but what I want is that it should be like a counter like this: if called ofr 5 times it should print:

1
2
3
4
5
and not just 5.

First class

public static int count = 0;
public void Login(String LoginUsername, String LoginPassword) {
count++;
}
Second class
int i = LoginReusables.count;
i++;
System.IO.File.WriteAllText(@"C:Usersken4wardDesktopTidyWriteLines.txt", i.ToString());

View 11 Replies View Related

C# :: List Initialize In Object Initialization Method

Oct 31, 2014

public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public List<Order> Orders { get; set; }
public Customer() {
Orders = new List<Order>();
}
}

This object is called as

var cust = new Customer
{
Id = 1, Name = "Khatana", [b]Orders.Add(new Order())[/b]
}

I want to ask, Orders.Add(new Order()) is a wrong way, but why..!!... as List<Order> already has been initialized in Customer constructor, then why it is again required to be initialized in object initialization.
Is this a correct way

List<Order> orders = new List<Order> {aaa.....bb....cc};
var cust = new Customer
{
id = 1, Name = "Khatana", Orders = new List<Order> { orders }
}

Why we need to initialize a list in an object initialization, where as the list has been already initialized in object constructor.

View 2 Replies View Related

C++ :: Merge Two Linked Lists Without Sort

Feb 19, 2013

i write code that's merge two linked list without sort ....

node * merage (node * list1, node * list2) {
// check witch list is empty
if (list1 == NULL) return list2;
if (list2 == NULL) return list1;
if ( list1 == NULL && list2 == NULL ) return NULL;

[Code].....

View 1 Replies View Related

C++ :: Merge Sort And Linked Lists

Mar 15, 2014

I am getting an error trying to convert from nodeType<Type> to nodeType<Type>* in my recursiveSort function and why this is happening.

#ifndef H_linkedListIterator
#define H_linkedListIterator
#include <iostream>
template <class Type>
struct nodeType {
Type info;

[Code] ....

View 7 Replies View Related

C :: Linked List / Adding Element To Beginning Of List

Dec 31, 2014

Code:

// Write a function called insertEntry() to insert a new entry into a linked list.

Have the procedure take as arguments a pointer to the list entry to be inserted (of type struct entry as defined in this chapter), and a pointer to an element in the list after which the new entry is to be inserted.

// The function dveloped in exercise 2 only inserts an element after an existing element in the list, thereby prenting you from inserting a new entry at the front of the list.

(Hint: Think about setting up a special structure to point to the beginning of the list.)

#include <stdio.h
struct entry1 {
int value;
struct entry1 *next;
};

[code]...

This is a working version of the exercise, but I don't think I'm doing what's asked. I was able to add an element to the beginning of the list using an if statement, not creating a special structure that points to the beginning of the list. How would I go about creating a special structure that points to the beginning of the list to add a new element at the beginning of the list?

View 8 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved