C++ :: Import A File Containing Words And Numbers To Linked List - Bubble Sorting
Feb 16, 2013
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;
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
This compiles fine but when I run the .exe for the first time an error message comes up saying program has stopped working. If I run the program again without recompiling it seems to work as expected.
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.
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;
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;
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
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.
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;
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 :
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
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.
Basically what i am trying to do is that sort the names of 10,000 movies and then write those names to a text file. It gives an error in the nested loop by underlining arr and tells that no suitable conversion function from std::string to const char* exists.
i'm new to C++ i came across this bubblesort program earlier relating to class templates i was wondering how to make this into simple function templates
#include<iostream> #include<iomanip.h> #include<conio.h> #include<stdio.h> using namespace std; template <class t> class bubble
I'll make a program that consists of "sorting and searching." To cope with the task, it should contain this: Create a field containing 50 random numbers, sort them by a method sort according to Bubble sort and then out the field before and after sorting. [URL]
I'm really struggling with bubble sorting a classed array(an inventory). I understand bubble sorting in theory and implementing it into a static array makes sense. When I get to doing it in a classed array then I struggle. I need to sort the weight by bubble sort (then selection sort for name, and insertion sort the cost; but I want to tackle one problem at a time.
Here is the code:
#include <iostream> #include <iomanip> #include <conio.h> #include "Inventory.h" #define MAX_REC 10 using namespace std; //Class to hold inventory variables/funcitons
My program is almost done all that is left is entering big numbers, the program can add and subtract small numbers but not big numbers because it puts them all in one node, I need the program to store each number in a node.
#include<iostream> #include<string> using namespace std; class Node { public: int value; Node * next;
This program using the selection, insertion, and bubble sorts. The program needs to be able to do the following:
1. Create an array of 1000 population records when the array object is instantiated. Call it unSorted.
2.Open the file called "Population.csv" (on the portal) and invoke a function that loads the population data into the array.
3.Create a second array of 1000 elements that will be used to sort the data using the different algorithms. Name is sortedArray.
4.Write a function that will copy unSorted into sortedArray and execute that function.
5.Using a function, display the unsorted array.
6.Invoke the insertionSort () function that will sort the sortedArray using the insertion sort algorithm. Sort the population data on the rank field in ascending order. Alternatively, you can sort in descending order on population.
7.Using the display function, display sortedArray.
8.Display the number of iterations it took to do the sort using this algorithm.
9.Repeat steps 4-8 for the selection and bubble sort algorithms.
Here is my code so far:
Code: #include <iostream> #include <iomanip> #include <fstream> using namespace std; void loadArray (int unSorted[], int s); void displayArray (const int num [], int size);
[Code] .....
Here is a few lines from the Population.csv file contents:
I'm not sure how to load the data from the file into the array properly, I attempted this. I also don't know how to copy the unSorted into sortedArray.
Let's say that in a txt file named hot.txt, I have this:
12,23,32
And with ifstream I want to take those number, adding one by one as integers in a linked list.
ifstream myList; char* p= new char; cin>>p; myList.open(p);
if(myList.is_open()) { char* x =new char;
[Code] ....
I know this part is quite wrong :
myList.get(x,256,','); // dafaq int num=atoi(x); list->addOrdered(num);
What I wanted to do is to stop before each comma and take that character and store it in the linked list and continue until the end of the file, but I couldnt.