C++ :: Linked List Sorting Algorithm?
May 31, 2013This is the algorithm I have so far and it works great. I was wondering what other people think? Comments/Critiques??
void LinkedList::sort()
{
if (head != 0)
{
[Code].....
This is the algorithm I have so far and it works great. I was wondering what other people think? Comments/Critiques??
void LinkedList::sort()
{
if (head != 0)
{
[Code].....
I have a list of latitude and longitude coordinates which are supposed to trace the outline of a city. Somehow they got scrambled so that they are now out of order. I'd like to write a program to arrange them such that one could follow from one coordinate to the next and trace the perimeter. However, I've run out of ideas for algorithms. What I did so far was a simple search for the nearest coordinate, starting from the first coordinate pair in the array. This produced local regions which worked rather well, but globally there were large jumps as the algorithm ran out of nearby coordinates and was forced to jump across the map.
Is there already a developed algorithm to perform this function?
I am trying to write a delete algorithm for an ordered linked list. Search and traverse I think I have down but delete is giving me fits. It crashes every time.
I have a private pointer to a ListNode strcuture called head in my TestLL class. The ListNode structure contains int Key, double dataValue, and ListNode *next. The function returns true if the node to delete was found and deleted or false if it was not found.
Code:
bool TestLL::Delete(int Key) {
ListNode *back = NULL, *temp = head;
//Search for the node to delete
while((temp != NULL) && (key != temp -> key))
[Code] .....
I am needing to code a shorted path algorithm through a sort of sudo-linked list. I say sudo because its not a true linked list as in *next *prev. The Data in memory that i need to do this through is in memory via a class.
The important parts of the class is one element is just an int that tells me the current node number, the second is an int that tells me how many neighbors this node has, and the third is a vector of ints containing the number of the neighboring nodes.
The vector was needed because each node type can have a different amount of neighbors.
For example if the class data looks like this:
0 1 1
that means that it is node 0, it has 1 neighbor, and the neighboring node is 1
another example:
8 3 1 2 3
means node 8, 3 neighbors, and the neighbors are nodes 1 2 3
I need to find a way to get from 1 node to another using this linked list, and the shortest path if we plan the data points well enough can be determined by how many nodes there are from start to finish.
Program to implement Dijkstra's Algorithm. Have Problems storing graph info into linked-list . The input is in the following format
5
1 2 9.0
1 3 12.0
2 4 18.0
2 3 6.0
2 5 20.0
3 5 15.0
0
1 5
The first number is the number of vertexes in the graph. Then next lines up to 0 are the edges of the graph. With the first and second numbers being the vertexes and the third being how far the edge is between them. Trying to read in the data and store the edges into there locations in the List adjacency for that vertex. This example would make a graph with five vertexes with edges from 1 to 2&3. 2 to 4&3&1 etc. It also stores in the opposites ex 2 1 9.0.
When reading it in and printing it back out it seems that it is only storing the last data entered for that vertex. EX. When trying to print out the data read in i only get 1 3 12.0, 2 5 20.0, 3 5 15.0, ( 4 2 18.0, 5 3 15.0 would show up if `if(i<n)` was removed it is there so it does not show duplicate edges).
#include <cstdio>
using namespace std;
struct ListCell {
ListCell* next;
int vertex;
double weight;
ListCell(int v, double w, ListCell* nxt)
[Code] ....
I know how some parts of sorting linklist works but not all of it what does the rest of the code do
Code:
#include <iostream>
#include <math.h>
using namespace std;
struct linklist {
int data;
struct linklist * pnext;
[Code] .....
what does this part of the code do
I have an algorithm and I want to make it as efficient as possible. Basically it just involves putting numbers in order. I have two options, but which one would be more efficient:
1. Using a doubly linked list. Every time a user wants to add a new number, the algorithm will start searching the correct place for the number from the beginning of the list. This is efficient per se, but once there are about a million numbers and a number has to be put in at the end of the list, the algorithm must go through all the 999 999 numbers before it.
2. Using a container to store all the numbers first, then sorting the numbers. In this case, adding all the numbers is fast, but the actual sorting will take a long time.
Which option would be more efficient? I was thinking of using maybe merge sort or quick sort in option 2. Yes, I'm aware I could just use vector and sort, but that's not my goal here.
I am at a lost of how to sort names in alphabetical order after having the user input them into a linked list. I manage to create a program that did have the person input names into the linked list and when it is printed it displays the names in the order of how the user inputted the names.
#include <cstdlib>
#include <iostream>
using namespace std;
#include <string>
class NodeType {
public:
string NodeValue;
[Code] .....
I have two arrays of characters that I want to combine and sort according to an internal variable (init) using a forward-iterating linked list. The two arrays must stay separated, as one of the arrays (the enemies) is contained within the object (encounter), the other is passed in via pointers (the players). The array inside the object will be destroyed later (when the encounter is over and the enemies are hopefully dead) while the one that is passed in must survive to be passed into other objects at a later time (the next encounter). My thought is to sort each array by linked list separately first, then iterate through and combine the two lists, But how to do this and no support IRL.
// DECLARATION OF CLASSES //
class character{
public:
character(); // Constructor
[Code]....
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] .....
While I know that linked lists seem to be a fairly common problem area among beginner C programmers, most examples that I have come across abstract the sorting of a linked list to a separate function which is sadly not what I am trying to do.
The code below is my attempt to have the nodes inserted into their correct place so that once all nodes have been inserted they are already in a sorted order.
Code:
int main(int argc, char *argv[])
{
struct node_t* dict_head;
struct node_t* traversor;
struct node_t* newnode;
int list_size = 0, insert_key, search_key, delete_key, x;
char insert_word[WORDLEN];
/*Opening the dictionary file provided by the command line argument */
FILE *fp;
fp = fopen(argv[1], "r");
[Code]....
The problem is as follows. When you provide it with input in which the first word scanned is any other word than that which would be placed at the front of the list, it works as intended. For example.
7 world
0 ant
3 kodak
1 best
6 the
2 is
Produces ant -> best->is->kodak->best->world
However, swapping ant and world in the above input gives:
world->best->is->kodak->best->world
In regards to why I have my head node set as a node without a word or a key, it was suggested that I make it so that the first node inserted is set to be the head of the list and then changed as sorting required, yet this caused only additional problems.
Write a program that creates a forward linked list of at least 20 elements, where each element holds a random integer between 0 and 99. Print the list.
Write the function "returnMiddleList" to find the middle element of the linked list in one pass. Print the integer value of this element and the position of this element (starting at zero) in relation to the head (where the head = 0, the element pointed to by the head = 1, the element pointed to by the previous one = 2, etc).
Split the list in half at the middle element to create two entirely separate* linked lists of near equal size (+/- 1) and print the two lists. Modify the "returnMiddleList" function to accomplish this, returning the head of the second linked list and setting the link of the element pointing to that head to null. Then print the two sums of the integers stored in the elements of both lists.
Sort the two lists from least to greatest and print them out (printing at this step is optional depending on the sort approach taken). Then combine the two lists while sorting them again from least to greatest and print out the new list. (HINT: you can subdivide the lists further and sort them on a scale of one to two element lists before sorting and combining the first two unsorted lists. What is this sort called?)
I have got #1 and #2 working, but #3 and #4 is where the issue is beginning. When I split my link list into two lists and print the individual lists out, my first link list prints out 9 numbers when it should be printing out 10 (the 10th number somehow disappears?), but when I do the sum of the first list right after that, the number that has disappeared gets added in the sum! I do not know why it is disappearing, and this is one issue. Another issue is in the second list, a random "0" gets added to the list and one of the numbers is lost. My last issue is about #4 as the merge algorithm I have used does not seem to work (I am merging the list together while sorting them, but I am not using a recursion sort because we have not learned that yet).
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
struct nodeType {
int data;
nodeType *link;
[Code] .....
I'm trying to sort the elements in a linked list which contain a variable amount of data in any given case. In the sample code, the code is more static, but I plan on adding it to much more dynamic code once I have it figured out. My main problem is that I am not sure how to sort the linked list while still keeping the correct pointers to the nodes. I thought about writing my own custom quick sort instead of using the C-standard library function, but how I would keep the pointers to the next nodes correct eluded me. Here is my code so far :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
[Code]......
I was trying to implement sorting in a linked list. However, when i run the sortList() function the program abruptly terminates. Here's the complete code:
Code: /*
* {
* SingleLinkedList.c
*
* Created on: 28-Nov-2014
[Code].....
I need to sort the Linked list from highest to lowest or in this case. The highest Bribe gets the higher priority on the list. I have looked all over the internet and have found some pretty decent examples but I still don't truly understand how to sort it. I think from looking at so many examples I have confused myself even more. I was reading about using Doubly Linked list but I don't even know if were allowed to use that.
1. The program runs perfectly at the moment. It prints out the list but does not sort it.
2. How to make sure that I am deleting the allocated memory correctly in the deconstructor.
Header File
#include <iostream>
#ifndef PERSON_H
#define PERSON_H
struct PersonRec;
class PersonList {
[Code] .....
I am relatively new to C++ and am trying to bubble sort my linked list that creates 100 random integers. Everything works, but I am unsure how to continue this to include a bubble sorting method.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Node{
public:
int data; //set data
[Code] ....
I have a code able to import a file containing words and numbers to a linked list, but I also need to sort this linked list alphabetically. I've done research on this involving bubble sorting, but no explanationcan achieve this objective.
Below is the code that can only put the file into linked list:
Code:
#include<iostream>
#include<conio.h>
#include"U:C++WordClass2WordClass2WordClass.cpp"
#include<fstream>
#include<vector>
#include<string>
using namespace std;
[Code] .....
So I am working on a dual pivot quicksort, I can correctly sort the array the first time around however in the main it is called again on the sorted array and in the function I get a seg fault at line 17 in sort.cpp, for the life of me I cant figure it out as the values are the same when I pass it in the first time. Here is the code:
main1.cpp
#include <iostream>
#include "movement.h"
#include "sort.h"
using namespace std;
int main() {
const int size = 10;
T array[size] = {6, 5, 1, 8, 4, 7, 2, 9, 6, 3};
[Code] ....
I am fairly new to dynamic memory allocation and I keep getting a segmentation fault in this code of mine. This is what the method should do:void sort StringsByReversePoints(char **myWords): This function sorts the char* values (i.e. strings) of myWords in descending order of point value by calling getWordPoints as a helper function and comparing adjacent words. This simple (but inefficient) sorting algorithm starts at the beginning of myWords array and sweeps to the end comparing adjacent values and swapping if they are out of order. After N (length of the array) sweeps the array is fully sorted. Note that efficiency can be improved by a factor of 2 by shortening each successive sweep by one, since the first sweep will have guaranteed the minimum point value word is the last element of the array, the next sweep guarantees the last two elements are correct, and so on....Additionally, if a given sweep results in zero swaps then the array is sorted and you can return immediately.
View 5 Replies View Related#include <iostream>
using namespace std;
class CD {
public:
static const int num = 100;
char publisher[num], title[num], location[num];
[Code] .....
I'm currently trying to code a sorting algorithm program.
let's asume I have a string given: aa, aaa, bbb, bas, zya!
I first of all want to split the given string on commas and '!' tells the program the string ends here and is no part of the last word. lower and upper case is not important at the moment. trying to implement everything with standard libary
output should be like that ofc:
aa
aaa
bas
bbb
zya
I already looked into the bubble sort algorithm and I think it benefits my needs. Just wanted to know how I should start out with the string split.
This program is sorting a randomized array of integers using the bubblesort algorithm.
I am trying to modify n correct the source code,so that the swapping of two values will be done by a function called swap values() by using call-by-reference but function should have as arguments only the two array elements that must be exchanged. (Note: do not pass the whole array to the function!) .We consider an array with the first element containing the number of elements in the array, followed by 10 randomly initialized integers (elements).
The code must sort the 10 elements in ascending order.
Compile: g++ -Wall sorting.cpp -o sorting
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
int main() {
const int SIZE=10;
[Code] .....
I have an school assignment that asks me to measure the most famous sorting algorithms for performance in terms of number of steps and CPU running time. ( Here I'm testing for running time)
I decided to test for bubble sort first:
#include <iostream>
#include <ctime>
using namespace std;
void bubbleSort(int ar[], int size) {
int temp;
[Code] ....
So basically what I want to know is:
1. Is this clock function giving the correct CPU running time?
2. Is there any way to write code that would measure the number of steps for each algorithm?
3.I need to test it for number of integers=100 then 200, then 300... Well you get my point, and I don't want to have to actually input 200 numbers with my keyboard. Is there any way to generate as many entries as I want?
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]......
I've written code for a selection sort algorithm to sort through a singly linked list, However, it only brings the smallest node to the front, but doesnt swap positions of any of the remaining nodes, even though they are not in order.
Input list: 3 6 17 15 13 15 6 12 9 1 2 7 10 19 3 6 0 6 12 16
Sorted list: 0 6 17 15 13 15 6 12 9 1 2 7 10 19 3 6 3 6 12 16
Code:
#include<stdio.h>
#include<stdlib.h>
struct node{
int val;
struct node *next;
[Code] ......
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..