C++ :: Shortest Path Algorithm Through Sudo-linked List
Feb 9, 2013
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.
View 2 Replies
ADVERTISEMENT
Mar 26, 2014
I'm working on a maze solving program. So far I got the program to solve a maze using the recursive backtracking algorithm. I represent the maze as vector<vector<Square>> where Square is an enum that contains the kind of square (empty, wall, etc.). I use a class Point that contains 2 ints which are used for subscripting the vector of vectors. I have a Point begin and Point end. Now I want the program to solve the maze using the shortest path. I can either use the A* algorithm, Dijkstra's algorithm, or the breadth first search algorithm.
View 6 Replies
View Related
Apr 19, 2012
Code that finds a shortest path in a dense graph, using Dijkstra's algorithm in the simple array version. You write a function
struct listnode shortest path(int n, int s, int t, int *dist)
with
struct listnode f struct listnode * next; int vertexnumber; g ;
Being used to return the list of vertices on the shortest path. Your function has the following arguments:
- n: the number of vertices of the graph,
- s: the start vertex,
- t: the target vertex
- dist: the matrix of edgelengths.
The vertices are numbered from 0 to n -1, so s and t are numbers in that range. dist is a pointer to the n * n matrix of edgelengths; vertices which are not connected will be joined by an edge of length 9999. To access the array element dist[i][j], we can use *(dist + i*n + j). Your function should return the list of vertices on the shortest path from s to t, starting with s and ending with t.
View 4 Replies
View Related
Feb 24, 2013
given matrix A find shortest path between vertices using matrix power multiplications untill u get nonzero values,use adjacency matrix
View 1 Replies
View Related
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
Oct 19, 2013
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] .....
View 4 Replies
View Related
Mar 20, 2014
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] ....
View 5 Replies
View Related
Jan 29, 2013
How do I get the path for a selected item in a list control?
View 5 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 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
View Related
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
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
Mar 6, 2013
I have a list and a vector, for example like
list<int> mylist = {1, 1, 1, 0, 0, 1, 0, 0}
vector<int> myvec;
I want to create an efficient algorithm to erase all element of value "1", and give these elements to a vector. My code (not efficient) is as follows
for(auto it = mylist.begin(); it != mylist.end(); it++) {
if(*it == 1) {
myvec.push_back(*it);
it = mylist.erase(it);
if(it != mylist.begin())
it--;
}
}
View 8 Replies
View Related
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
Jul 4, 2012
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?
View 1 Replies
View Related
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
May 15, 2013
I need to make the functions using these function prototypes. I am mainly having problems with GetFirst() and SwapData() but how to do it..
Header File with Prototypes
Code:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
/**
* @file
* This file provided a doubly-linked list implementation capable of storing any arbitrary data.
* The list implementation relies on a chain metaphor: a list is merely a sequence of links
* (ListItems) and there is no separate construct to represent the entire list, each ListItem in it
[Code]....
View 14 Replies
View Related
May 7, 2013
I read an article on linked list here: C Linked List Data Structure Explained with an Example C Program
Code:
struct test_struct* search_in_list(int val, struct test_struct **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;
}
[code].....
What is "if(prev)"? Wouldn't "prev" always have the same value? Secondly, if tmp is NULL (which will be the case when the loop if(ptr->val == val) finds a match the first time it is run), is *prev assigned a NULL?
View 1 Replies
View Related
Jan 2, 2014
I'm having a small issue here with my linked list.I built a linked list with strings and it worked perfectly.Now since i'm using strtok() to separate the string.for now here's what i've got:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct dict_word *word;
typedef struct node *Node;
typedef struct double_linked_list *DLL;
}
[code]....
View 1 Replies
View Related
Jan 6, 2014
I have the codes for the functions: "Given the functions that we have discussed and defined in the class and the code that we have created, create the code for the Delete Node function and the Search function. Create an application that will incorporate all the functions that we have discussed and the new ones that you need to create. The application should allow the linked list to be built initially from a text file. The application should then allow for the user select an operation to perform. The acceptable operations are
- to search for an item
- to delete an item
- to enter a new item
- to exit the application
After the initial build and loading of data from the textfile, the application should print a listing of each item in the list and the node that it is located in. after a delete or insert, the application should display an output of the list again showing each item in the list and the node that it resides in.
The data item for this problem will be the inventory structure that we created in class and the data is in the inventory file that you already have downloaded."
View 4 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
Apr 2, 2013
I been ask to create a sorted linked list module, and to test each function with 1 unit, 2 +.so i wrote my addtofront function( we have to use the same as the teacher)
Code:
struct MyList{
char * name;
Sorted * AddmyChar;
struct MyList * next;
}
[code]....
but i cant seem to test it because it always display a seg fault.... i dont know what i did wrong....how do i call this function in main to display a string i want?
View 9 Replies
View Related
May 27, 2014
I decided to make a linked list program using classes and templates.I implemented my linked list and added some basic functions, everything worked just fine. Then I proceeded to add templates...First, the error list:
#pragma once
template<class T>
class Node {
private:
Node* next;
[code]....
To sum up: Something's wrong with the templates that I used in "List.cpp" and what it could be.
View 5 Replies
View Related
Jan 2, 2013
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;
[Code] .....
View 11 Replies
View Related
Mar 30, 2013
When I am traversing a linked list ...i suppose we can do it only with pointers
like p is a head node pointer then
p
p->next
p->next->next
p-next->next->next
so on represents a link...
if i have to pass head node as struct variable itself...can i traverse a link using dot operator??
p and p.next and how to retrieve third and remaining links through this method if we can traverse like this???
View 1 Replies
View Related
Mar 3, 2014
I have created this program to merge two linked list into one ,but however everything is working fine but on execution it providing a segmentation fault error ( on calling merging function )
code :
#include<iostream>
#include<stdio.h>
using namespace std;
struct node{
int data;
node* link;
[code].....
View 1 Replies
View Related