C :: Selection Sort Algorithm For Singly Linked Lists
Feb 24, 2013
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] ......
View 1 Replies
ADVERTISEMENT
Jul 7, 2014
I need some basic code (not for an assignment) to write an add, delete and locate an node via the singly linked list.
I've looked at: [URL] .....
View 1 Replies
View Related
Jun 14, 2014
So i'm trying to implement the selection sort algorithm and it seems that the code is fine but...
Code:
#include <cs50.h>
#include "helpers.h"
void
sort(int values[], int n) {
// TODO: implement an O(n^2) sort
[Code] ....
I keep getting these errors and i don't understand why:
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
View 3 Replies
View Related
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
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
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
Mar 7, 2013
I am having difficulty getting my program to run. I have looked and looked many times trying to figure out what I am doing wrong. I have a hunch that it's my load function that's acting up, but from my perspective, my creation of my singly linked list looks good. From the creation of my singly linked list, does anything stand out as being abnormal?
Code:
typedef struct Node {
char word[LENGTH+1];
struct Node *Next;
} Node;
}
[code]....
View 6 Replies
View Related
Feb 19, 2014
You will write a program that uses a multidimensional array having 3 rows and 8 columns and sorts each of the rows using both a bubble sort and a selection sort.
You must declare the array inside of main. You will have a for loop containing the calls to bubbleSort and selectionSort. You need to pass into function bubbleSort and selectionSort the following: 1) each column of the multidimensional array, 2) the size of the column, and 3) a particular row number of the multidimensional array to be used for printing out the "pass" shown on the following pages.
I keep getting an error that the identifier for bubbleSort and selectionSort is not found. (Error C3861) Also, I feel like I'm missing something in int main() to get it to sort properly.
# include <iostream>
using namespace std;
int main()
{
[Code].....
View 14 Replies
View Related
Oct 19, 2014
add number in a singly linked list? here is my code...
Code:
#include<stdio.h>
#include<stdlib.h>
struct node {
[Code].....
View 2 Replies
View Related
Mar 1, 2013
I want to make a singly linked list of structs with a custom datatype. Sadly, I dont think I understand either.
#include <iostream>
using namespace std;
struct datatype
[Code]....
Basically, I cant figure out how to insert a strcut with a string, random ID number, and a random salary amount, into a linked list.
View 19 Replies
View Related
Jan 20, 2014
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node {
int data;
struct node *next;
[Code] ....
The above code is a SLL. I have a problem in my deleteNode function. I can't delete the exact data on the list. How to do this right?
View 1 Replies
View Related
Oct 18, 2014
I was trying to write a function in C to delete a node(only from the middle) from a Singly Linked List. I wrote one but not sure if the code will work fine under all test conditions. I have tested it and shows no error so far.
Code:
void deleteAt(struct node *root, int number){
while(root->link != NULL) {
if(root->link->item == number) //checks if the next node is the element to be deleted {
root->link = root->link->link; //points the link of the element to be deleted to the element before the element to be deleted
}
else
root = root->link;
} }
Or is there a better way to do this?
View 3 Replies
View Related
Oct 10, 2014
I don't know why my code is not working. It's from line 249 - 266
void SLL::deleteDuplicate(){
Node* temp = head;
Node* p = temp;
[Code].....
View 1 Replies
View Related
Sep 13, 2014
I need to make singly and doubly linked list classes that can insert elements. Once the lists are created, I need to order the linked list elements according to a certain pattern.
order2 pattern:
input: 0 1 2 3 4 5 6 7
output: 1 0 3 2 5 4 7 6
order3 pattern:
input: 0 1 2 3 4 5 6 7 8 9 10 11
output: 2 1 0 5 4 3 8 7 6 11 10 9
sequence order pattern:
input: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
output: 1 0 4 3 2 8 7 6 5 13 12 11 10 9
reverse pattern:
input: 0 1 2 3 4 5 6 7 8 9
output: 9 8 7 6 5 4 3 2 1 0
My instructor has given the description of the required classes in a file interfaces01.h as:
#pragma once
#include <cstdlib>
struct ISingleNode {
ISingleNode() {}
virtual ~ISingleNode() {}
virtual void setValue(int value) = 0;
virtual int getValue() = 0;
[Code] ....
However when I am compiling these files in vs 2013 I am getting the error: cannot open include file Singlenode.h,Singlelist.h.
Also I am not too sure about the sorting and reverse logic. How can I implement these logics.
View 1 Replies
View Related
Aug 6, 2014
I've written this class and struct to create a singly linked list. The data is stored in the binary file which I've opened and read. I'm trying to load said data into a class type array. The errors I'm getting are "incompatible types in assignment of 'StatehoodInfo' to char[3]" Lines 130-134 is what I was working on.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <cstring> //For char[] string functions
[Code] .....
View 2 Replies
View Related
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
Feb 28, 2014
So I have been trying to implement the Dijkstra Algorithm for shortest path in a directed graph using adjacency lists, but for I don't know what reason, it doesn't print out the results (prints the minimum distance as 0 to all nodes).
The code I wrote is:
#include <fstream>
#include <functional>
#include <climits>
#include <vector>
#include <queue>
#include <list>
using namespace std;
struct node {
int vertex;
int weight;
[Code] ....
The input data was:
5 7
1 2 10
1 3 2
1 5 100
2 4 3
3 2 5
4 3 15
4 5 5
It means there are 5 nodes, 7 arcs (directed edges), and the arcs exist from node 1 to 2 with the cost of 10, from 1 to 3 with the cost of 2, and so on.
However, the output is wrong. Where the program might fail. I took the main idea from here: [URL] ....
(At the end it gives the idea for Dijkstra's Algorithm using a priority_queue).
View 3 Replies
View Related
Feb 18, 2014
How to sort the last name instead of first name. Everything works so far and sorting is done on the first character.
#include <iostream>
#include <cstring>
using namespace std;
void sort(int, char[10][40]);
int main() {
char twoD[10][40];
int input = 0;
[Code] .....
View 4 Replies
View Related
Mar 25, 2014
I'm trying to code a Singly-Linked List(Double Pointers) selection sort program and I can't tell what the problem is. My compiler says no errors but when I run the program, it crashes right after I enter the values.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node{
int elem;
struct node* link;
[Code] ....
The compiler that I'm using is Dev-C++.
View 2 Replies
View Related
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
Sep 15, 2014
how to put comparision condition in order to caculate total number of comparision in selection sort
static void selectsort(int[] data, int n)
{
int i, j;
for (i = 0; i < n; i++)
[Code]....
View 1 Replies
View Related
Jan 24, 2013
I have written a selection sort algorithm to go sort an array of class objects by age in ascending order, the problem is that the output being given does not match what i think the code should do. when the program runs the 3 records are added to the array and when they are sorted should be outputed in ascending order, the problem is that with my code the last 2 are sorted properly but the first element does not seem to move, it remains the same as the original unsorted value.
My code for the selection sort function and the display method are below:
void selectionSort() {
int i, minIndex, minValue;
for (i = 0; i < (arrlength - 1); i++) {
minIndex = i ;
[Code].....
View 1 Replies
View Related
Feb 17, 2013
Question: What is the efficiency and big O of the selection sort algorithm when the input happens to already be in nondecreasing order?
Answer: Not sure... since the input is in nondecreasing order, such that example 0, 1, 1, 2, 3, 4, 4, 5 then there will be no swap, Just comparisons of emelemts. So it is big O of n
View 2 Replies
View Related
May 4, 2014
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:
Code:
Alabama,Baldwin County,140415,389
Alabama,Blount County,51024,908
Alabama,Calhoun County,112249,477
Alabama,Colbert County,54984,858
Alabama,Cullman County,77483,653
Alabama,Dale County,49129,927
Alabama,Dallas County,46365,974
Alabama,DeKalb County,64452,753
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.
View 14 Replies
View Related
Aug 12, 2014
What I want to create is a program that sorts through a huge list (millions of lines).
I want it to get rid of any line that contains a word that isn't in an English dictionary.
Example list:
00sdfdsf
ahdadsg
angel
ksjflsjdf
green
green000
carrot
and it would go through millions like that, giving me only:
angel
green
carrot
as my new list.
How could I go about this? What programs would I need? And at the very least how can I remove unwanted things like numbers, double letters, underscores etc.?
View 3 Replies
View Related
Feb 19, 2014
You will write a program that uses a multidimensional array having 3 rows and 8 columns and sorts each of the rows using both a bubble sort and a selection sort.
You must declare the array inside of main. You will have a for loop containing the calls to bubbleSort and selectionSort. You need to pass into function bubbleSort and selectionSort the following: 1) each column of the multidimensional array, 2) the size of the column, and 3) a particular row number of the multidimensional array to be used for printing out the "pass" shown on the following pages.
I keep getting an error that the identifier for bubbleSort and selectionSort is not found. (Error C3861)
Also, I feel like I'm missing something in int main() to get it to sort properly.
Code:
# include <iostream>
using namespace std;
int main() {
const int SIZE1 = 3;
const int SIZE2 = 8;
int arr [SIZE1][SIZE2] = { { 105, 102, 107, 103, 106, 100, 104, 101 },
[Code] ....
View 1 Replies
View Related