C++ :: Swapping Nodes In Doubly Linked List?
Nov 15, 2014
I've been working on this linked list priority queue . I know that the root of the problem is in my swapUp() function (swapping the positioning of two nodes based on their priority), because the list works great up until it is called. The seg fault is not actually being caused by swapUp(), it's being caused by peekAt(), which returns the element in the node at position n. But the error does not occur unless swapUp() is called first, so that is where the issue is (I think).
There is also a seg fault being caused in the destructor, which I believe may have the same root cause in swapUp().
PRIORITY QUEUE:
#ifndef JMF_PriorityQueue
#define JMF_PriorityQueue
#include <iostream>
#include <string>
template <typename T>
class PriorityQueue{
[Code] .....
Okay, so I've tried implementing SwapUp() in a different new way, but it's still giving me the same problem
template <typename T>
void PriorityQueue<T>::swapUp(Node * target){
Node * partner = target->next; //Partner = target next
[Code] .....
This is such an elementary logic problem I don't know why I'm having so much trouble with it.
View 4 Replies
ADVERTISEMENT
Feb 28, 2013
I have been trying to swap two adjacent nodes for my linked list sort. Its not meant to be optimal, just meant to work. My problem is I either lose nodes or get Access Violation errors.
PHP Code:
void List::sortList() {
Node *current = _head;
Node *rightNode = NULL;
if (_head->_data > _head->_next->_data) {
current = _head;
rightNode = _head->_next;
[Code] .....
View 10 Replies
View Related
Oct 22, 2014
I am working on a program where I sort elements into alphabetical order and then when one is less than the other I swap them. I first did it by swapping the data but they want me to swap the nodes instead and I am having trouble doing that.
Node *add_node( Node *list, const string &s ) {
struct Node *n = new struct Node;
n->word = s; // copy string s to word
n->next = 0;
// add node n to the list
// the list should always be in ascending alphabetical order
n->next = list;
list = n;
[Code] ....
View 2 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
Jul 23, 2014
I wrote function to add to elements in the list, but when I call printList function it returns me empty list ! I'm new with linked list in C
Output:
Empty list
List is empty add element at the begining
New node with packet num 245
List is not empty add element at the end
New node with packet num 486
Linked list: Empty
Main:
Code:
int main(){
struct node * start ;
start = NULL;
int i;
/*Check if list is empty*/
if(start == NULL){
printf("Empty list
[Code]...
View 2 Replies
View Related
Oct 17, 2014
Ok so I am having difficulty adding nodes to my linked list....
how to add a third node while keeping track of the address...Thats where I get lost..I don't know how to keep track of the addresses of the next node..
#include <iostream>
using namespace std;
typedef struct Node{
[Code]......
View 1 Replies
View Related
Apr 18, 2015
I'm simply trying to locate possible logic errors because if I could fill this list properly, I can finish my project very easily. This is just a small portion of a very immersive project.
I am trying to create a linked list that holds objects of type Location *. I have Location defined as
typedef struct location{
char *name;
char *longer;
char *shorter;
[Code].....
I wish to clarify but can not find where to edit the OP. I believe the list is still empty because I attempt to do a simple read through the list by accessing the head and then reassigning the list to the tail of the list. However, the program never enters the while loop
while(world !=0){
View 2 Replies
View Related
Apr 5, 2014
namespace main_savitch_5 {
class polynode {
public:
// CONSTRUCTOR: Creates a node containing a specified initial coefficient (init_coef), initial exponent (init_exponent), and initial links forward and backward (init_fore and init_back).
polynode(
double init_coef = 0.0,
unsigned int init_exponent = 0,
polynode* init_fore = nullptr,
[Code] ....
View 1 Replies
View Related
Feb 26, 2014
I have no problem traversing at both forward and backward but not simultaneously. I can traverse forward, and traverse again forward with no problem. I can also traverse backward and traverse backward again with no problem (take note this is by not exiting the program). Without exiting the program, with the same datas inputed, if i traversed forward, i cannot traverse backward (it only gives me infinite loop of the first data) and vice versa.
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
Node *prev;
[Code] ....
View 2 Replies
View Related
Sep 13, 2013
my code is
#include<stdio.h>
struct node {
int info;
struct node *next, *prev;
};
typedef struct node *nodeptr;
[Code] ....
I have reversed a linked list it is reversing but additional two elements 0,0 are coming, whats going wrong....
View 1 Replies
View Related
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
Oct 25, 2013
I have a problem with inserting a node between two nodes.
Code:
#include <stdio.h>
#include <stdlib.h>
struct listelem {
int nbr;
struct listelem *next;
[Code] ....
View 8 Replies
View Related
Mar 27, 2014
I am coding a singly-linked list container. Of course, internally it uses Node<T>.
Question: what is the correct way to use the allocator given by the user? I ask, because I've read this on the rival C++ Reference:
std::list<T, A> allocates nodes of some internal type Node<T>, using the allocator std::allocator_traits<A>::rebind_alloc<Node<T>>, which is implemented in terms of A::rebind<Node<T>>::other if A is an std::allocator
[URL]...
The above doesn't seem right to do, because then what should pointer and const_pointer be?
using pointer = std::allocator_traits<Alloc>::pointer;
using const_pointer = std::allocator_traits<Alloc>::const_pointer;
// but we're using Alloc<Node<T>> not Alloc<T>
// so maybe do this?
using pointer = value_type *;
using const_pointer = const value_type *;
View 6 Replies
View Related
Jun 25, 2014
I am creating a Linear linked list in c++. I've written all the functions for it, but now I want to try and do them using recursion.
I managed to make functions for adding and displaying nodes on the list, but I am stuck on the function for removing nodes. First I want to try and write a function the removes the last item on the list. But for some reason my code isn't working properly.
Here is the code :
void removeLastItem(node * &head) {
if(!head) {
delete head;
head = NULL;
[Code] ....
NODE - My structure name
NEXT - The pointer to next element.
HEAD - The first (head) pointer.
The couts in the if statements are just for testing. In fact after I Run my program it does as it is supposed - enters the second if /b]case as many times as there are elements and then executes the first [b]if statement. But for some reason it does not delete the actual node.
View 2 Replies
View Related
Mar 6, 2015
I am trying to reverse a Linked list using recursion but whenever I try to print it after reversal it prints only the last element of the list.
Code:
void reverseRecursive(Node **root, Node *temp)
{
Node *next = temp->next;
if(temp->next == NULL)
{
*root = temp;
[Code]...
I have just posted the reverse function to enable easy readability rather than post the entire code
View 7 Replies
View Related
Mar 6, 2015
I am having trouble adding a second node to a doubly linked list. I define my list as follows:
Code:
typedef struct Node {
char command[256];
int length;
struct Node *prev;
struct Node *next;
} userInput;
}
[code]...
Now, when I iterate back through my list (I want to start at the tail and work my way towards the head), I can only ever get the 1st node to print, then the 2nd node is garbage, which means, to me, that I've linked something wrong.
View 3 Replies
View Related
Nov 17, 2013
I'm trying to write a program that manipulates a doubly linked list. My professor wants it to have two structs, one called Node (containing the data, and pointers to the next and previous nodes) and one called DLList, which contains the nodes for the head and tail (which is then passed to all of my functions).
I'm a little confused how to access the head and tail, for instance, if I want to initially set them to null in the main function (he emphasized the need for this), or to use them in my functions. I've tried a lot of variations to call the head and tail, but I keep getting told that head and tail are undeclared in the function.
How might I access my head and tail, for instance in a main function, when they're defined like this? (I took out all of the logic in my functions for clarity)
View 1 Replies
View Related
Sep 19, 2013
I need to delete the Nth node from a doubly linked list. I know I just cant delete it out right. I have all the goodies including a templetized node class. This is the code we're given:
template <typename T>
void doublyLinkedList<T>::deleteKthElement(const int item) {
}
It accepts an int, and should go to the Nth node and delete it.
View 1 Replies
View Related
Oct 17, 2014
i am trying to develop a doubly linked list class. I became stuck at a few of the functions as well as the main.cpp file. i have to insert a character sentence "TRICK OR TREAT" spaces included. I am stuck on the display and the listsize functions and im unsure if my insert has the head node in it. The function parameters were given to us and most of this code was given from a single linked list.
//node.h
#pragma once
class Node
{
publi
[Code].....
View 1 Replies
View Related
Feb 27, 2014
When I run this in main it gives me a windows error message. I believe it has something to do with my insertAtEnd function but I've gone over it a million times....
#include<iostream>
#include<string>
#include<vector>
#include"RhymeGame.h"
using namespace std;
Game::Game() {
head = NULL;
[Code] ....
View 3 Replies
View Related
Jul 7, 2013
I'm not sure why my destructor isn't working..
typedef struct Node;
struct Node {
char data;
[Code]....
View 4 Replies
View Related
Nov 30, 2014
Why is it sufficient to only have a pointer to the last node of a doubly linked list?
View 1 Replies
View Related
Dec 19, 2013
Create a notepad that allows the user to write text on the console. For this purpose, the user should be able to control and track the movement of the cursor. The user can access, add and delete any part of the text. To add or delete a text, the user can take the cursor to that location (using the arrow keys) and perform the required operation. The working of the program (i.e. the movement of the cursor, add and delete operation) must be consistent with the working of the real notepad. However, you do not have to handle word wrapping.
In addition, the user should be able to save and load the data in a text file using S and L respectively. The program will automatically save the data in file save.txt, and load the data from the same file. There is no need to ask the user for the file name. Use Q to quit the notepad. Don’t forget to implement the destructor.
Internally, the notepad is composed of two-dimensional doubly linkedlist. Its implementation is just like a doubly linked list with an additional property that it can grow in two dimensions. Since text can be written on multi lines, each row of the 2D-linkedlist represents one line. Each node contains four links which it uses to connect to node before, after, below and above it. In addition each node can store a character.
View 9 Replies
View Related
Oct 4, 2014
We had to convert a single-line text editor which uses arrays to one that uses OOP and double-linked lists and I have been doing it in steps. I have, for the sake of convenience, put my headers, implementation and main all in one file.
I'm compiling this program in Hercules (the getch function uses C code).
I keep getting the error from the compiler saying "Undefined Symbol" for functions:
insertNode(char, Node);
deleteNode(Node*);
insertHead(char);
The full message is: ld: fatal: Symbol Referencing Errors. No output written to a.out collect2: ld returned with 1 exit status.
// A fake single line text editor program.
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <termios.h>
//#include "main.h"
typedef char Letter;
[code]....
View 3 Replies
View Related
Dec 2, 2014
Should my if statement be
if (tail== NULL) or if(tail->next == tail) ?
View 4 Replies
View Related
Apr 25, 2015
I print the original list, then reverse it, then try to print it again. Here is what I get when I do that:
So that second line should print 2 1 4 5
Here is the reverse function:
void reverseCirListDeque(struct cirListDeque *q)
{
struct DLink *curr = q->Sentinel;
do{
[Code].....
View 2 Replies
View Related