C++ :: Linked List Sorting / Splitting And Printing

Oct 3, 2014

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] .....

View 9 Replies


ADVERTISEMENT

C++ :: Splitting In A Linked List Class

Mar 9, 2013

I'm trying to develop a program that stores a list of hw assignments in the form of "Date,Assignment name,grade". I have functions that add,remove, and delete from a list, but I would also like to search and delete a particular date found in the list. From here, split - Splitting a string in C++ - Stack Overflow I've tested Evan Teran's method to split a string, but I'm unsure of how to implement it in my code.

List.h Code: #ifndef LIST_H
#define LIST_H
#include <string>
using namespace std;
class List{

[Code] .....

View 14 Replies View Related

C++ :: Splitting Linked List Into Two Separate Lists

Feb 14, 2013

I've been given a ton of code (5 files worth) that does various things with a linked list. I'm supposed to add a function to split this list into two separate lists by testing whether or not each number is <,=, or > a certain element. I am completely lost and don't understand linked lists at all. I'm also a bit lost on pointers since it's been almost a year since I've looked at programming, but I could probably figure that out if I could just get this linked list thing down.

Here's my SplitLists function so far:

void UnsortedType::SplitLists () {
// Pointers for each new list
NodeType* list1Ptr;
NodeType* list2Ptr;

[Code] ....

In other places in the code, I have GetNextItem, ComparedTo, PutItem, and ResetList that were already given to me.

View 1 Replies View Related

C++ :: Printing Powerset From Linked List?

Dec 10, 2013

I am trying to get to print a powerset from my linked list however I am not sure how to go about it. I need to create a void function for it so I know that much.

Any ways this is my code.If you do not know what a powerset is here is an example.

say you have a set {1,2,3}the power set would be the following:{{}, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}} another example say you have the power set {1,2,3,4,5} the result should be this:{{}, {1}, {2}, {3}, {4}, {5}, {1,2}, {1,3}, {1,4}, {1,5}, {2,3}, {2,4}, {2,5}, {3,4}, {3,5}, {4,5}, {1,2,3}, {1,2,4}, {1,2,5}, {1,3,4}, {1,3,5}, {1,4,5}, {2,3,4}, {2,3,5}, {2,4,5}, {3,4,5}, {1,2,3,4}, {1,2,3,5}, {1,2,4,5}, {1,3,4,5}, {2,3,4,5}, {1,2,3,4,5}}
#ifndef TEST1_H_INCLUDED
#define TEST1_H_INCLUDED
class List{

[code]....

View 5 Replies View Related

C++ :: Sorting A Linked List

Apr 29, 2013

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

View 5 Replies View Related

C++ :: Search For Element By Name - Printing Linked List

May 2, 2014

I've got this program that I'm working on. Most of the code is from a video tutorial, but I was editing it to be able to search for an element by name. That's working fine, but suddenly the part of the program that prints out all the elements starts in an infinite loop after I input two elements, and search for one.

Here's what I've got:

[code#include <iostream> usingnamespacestd; struct node { int number; string name; node *next; }; bool isEmpty (node *head); char menu (); void insertAsFirstElement (node *&head, node *&last, int number); void insertSorted(node *&head, node *&last, int number); void remove(node *&head, node *&last); void showList (node *current); void showByName (node *current, string name); bool isEmpty (node *head) { if (head == NULL) { return true; } else { returnfalse; } } char menu () { char choice;

[Code] .....

View 8 Replies View Related

C :: Printing Info From Nodes In A Linked List

Oct 16, 2013

I created a linked list, but I wanted to make sure it was storing each word appropriately and going over the text. It compiles and runs, so I know it works. I'm just not sure if it's doing what I want...

Code:
#include <stdio.h>
#include <stdlib.h>
//creating the linked list
struct node {

[Code].....

View 3 Replies View Related

C++ :: Linked List Or Sorting An Array?

Feb 2, 2015

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.

View 4 Replies View Related

C++ :: Sorting Names Within Linked List

Oct 29, 2014

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] .....

View 1 Replies View Related

C++ :: Sorting Array By Using Linked List

Apr 3, 2014

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]....

View 1 Replies View Related

C++ :: Linked List Sorting Algorithm?

May 31, 2013

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].....

View 14 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 :: Sorting A Linked List Upon Node Insertion

Aug 30, 2013

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.

View 10 Replies View Related

C :: Quick Sorting Array Of Nodes In A Linked List?

Oct 19, 2013

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]......

View 3 Replies View Related

C :: Sorting A Linked List - Program Abruptly Terminates?

Nov 29, 2014

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].....

View 3 Replies View Related

C++ :: Sorting Nodes In Linked List From Highest To Lowest

Nov 21, 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.

Header File

#include <iostream>
#ifndef PERSON_H
#define PERSON_H
struct PersonRec;
class PersonList {

[Code] .....

View 6 Replies View Related

C++ :: Bubble Sorting Linked List That Creates 100 Random Integers?

Feb 1, 2015

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] ....

View 1 Replies View Related

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;

[Code] .....

View 5 Replies View Related

C++ :: Splitting Directory Path Up - Function Call Missing Argument List

Feb 14, 2014

Code:
void CFileManager::SplitHeader(std::string sFilePath) {
std::string sDrive(255, ');
std::string sDirectory(255, ');
std::string sFileName(255, ');
std::string sExtension(255, ');

_splitpath_s(&sFilePath[0], &sDrive[0], sDrive.size, &sDirectory[0], sDirectory.size, &sFileName[0], sFileName.size, &sExtension[0], sExtension.size);
}

Which gives me error

Error 1 error C3867: 'std::basic_string<char,std::char_traits<char>,std ::allocator<char>>::size': function call missing argument list; use '&std::basic_string<char,std::char_traits<char>,st d::allocator<char>>::size' to create a pointer to member.

View 9 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/C++ :: Printing Doubly Linked Lists In Reverse

Apr 1, 2014

I'm currently learning how to work with linked lists, specifically, doubly linked lists, and I have come across a problem with my program when I attempt to print it backwards. Here is the portion of the code :

#include <iostream>
using namespace std;
struct node {
int data; //int to store data in the list
node *next; //pointer to next value in list
node *prev; //pointer to previous value in list

[Code] ....

The purpose of this program is to create a list, print it forwards, backwards, insert an element, erase an element, and then destroy the list. I have chopped it down to just the create, print forward, and print backward functions.

The issue I have is that in the createList function I am modifying both begin and end but I can only return one or the other. This means that whichever I don't return is still NULL in the main function and therefore does not point to anything. I've tried setting the begin/current/end to not equal NULL but createList won't work if I do that.

How I could modify both? Just to be clear, the list HAS TO be created in a function, it would be very easy to just initialize it in the main...

View 2 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 :: 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

C++ :: Printing Out List Using OOP

May 17, 2013

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
#include "Course.h"
#include <list>

[Code] ....

The program works well, except for the fact when it prints out the list (starting at the for-loop on line 65) each line is the same as the last, like it overwrites itself every time it traverses the while(true) loop.

View 9 Replies View Related

C :: Merge Sorting The Linked Lists - Runtime Error

Aug 10, 2013

The following program throws runtime error.

Code:
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;

[Code] .....

View 3 Replies View Related

C++ :: Linked List Delete List?

May 30, 2013

I'm working on a linked list and was wondering how this looks to everybody else for a deleteList function.

void deleteList(Node* head)
{
Node* iterator = head;
while (iterator != 0)

[code].....

View 6 Replies View Related







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