C++ :: Doubly Linked List Reverse

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


ADVERTISEMENT

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 :: Doubly Linked List

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

C++ :: Doubly Linked List Polynomials

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

C++ :: Traversing Doubly Linked List

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

C :: Reversal Of Doubly Linked List Using Recursion

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

C :: Adding A Second Node To A Doubly Linked List

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

C :: Program To Manipulate Doubly Linked List

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

C++ :: Deleting Nth Node From Doubly Linked List

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

C++ :: Doubly Linked List With Head Node

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

C++ :: Circular Doubly Linked List Code

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

C++ :: Doubly Linked List Header File?

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

C++ :: Pointer To Last Node Of A Doubly Linked List

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

C++ :: Notepad Using Two-Dimensional Doubly Linked List

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

C++ :: Doubly-Linked List And Referencing Errors?

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

C++ :: Adding To The End Of A Doubly Linked Circular List?

Dec 2, 2014

Should my if statement be

if (tail== NULL) or if(tail->next == tail) ?

View 4 Replies View Related

C/C++ :: Reversing A Circular Doubly Linked List

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

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 View Related

C++ :: Traversing Both Ways In A Doubly Linked List?

Mar 22, 2015

I've been working on a doubly linked list project for my Data Structures course. I've got everything to work so far, except for traversing in both directions. Traversing in the forward direction works fine, but when I try choosing to traverse backwards (starting from the tail of the linked list), it just says that the value is not in the list. That being said, the output looks like this:

This is not true, as you can obviously see, the value 26 is in the list, just two elements to the left of the tail (the value in tail for this particular case, obviously, is 36). The output should have been something like, "The value 26 was found 2 elements from the tail node". Here's the code for the findValue() function:

Code:
template <typename T>
bool LinkedListX<T>::findValue()//bool valueFound {
int traverseDirection = 0;
bool found = false;
int key;

[code]....

View 6 Replies View Related

C :: Creating Temp Node For Doubly Linked List

Jan 28, 2015

Code:

#include <stdlib.h>
#include <stdio.h>
typedef struct characterNode {
char c;
characterNode *prev;
characterNode *next;
} CharacterNode;

[Code]...

What's wrong with this? People told me that casting malloc was bad, but I don't understand what struct cn *temp is doing.

Also, could I create something like:

Code: typedef CharacterNode *CharacterNodePtr To be a pointer to my struct type? Why would that be beneficial?

View 1 Replies View Related

C++ :: Move Node To Front Of Doubly Linked List?

Oct 10, 2013

I have to write a method that moves a node to the front of one. Here is what I have:

template<typename T>
void DoublyLinkedList<T>::moveToFront(ListNode<T>* n)
{

[Code].....

I've walked through it a few times, and I feel like this correct. However, my program's output is off and I'm guessing this function is the culprit. I think I just need a different pair of eyes to take a look at it.

View 2 Replies View Related

C++ ::  Find Duplicates Before Inserting Into Doubly Linked List

Mar 8, 2013

I have a text file that needs to be read by command line arguments. The text are all numbers and can have multiple numbers on one line separated by a space. I cannot use an array or sort the numbers.So say I have a text file, listNums.txt:

12
473 8 29
30 1
8
248 17 55
29 84
5

Basically I need to read one number, find out if its odd or even by dividing by 2, search the odd or even doubly linked list that it would go into to see if its in there, if its not then add it to the bottom of the list.

View 2 Replies View Related

C++ :: Creating Custom Templated Doubly Linked List

Feb 20, 2013

I need to create a templated doubly linked list, with an iterator class within the list class. This program is to function just like the STL list class but I only need to implement functions that I am using, My trouble is I am kind of clueless on the iterator part and the fact that the list is templated is giving me syntax grief. I have pasted the code I have done so far.

1. On the syntax implementing the list and iterator functions outside of the class
2. I am not sure when to deference the iterator in the functions, but think I have it right so far
3. For the reverse function can I copy the list into a new list in reverse then re add them to the original list overwriting the same values? I have the code I have so far there
4. For the iterator erase function, I am not sure if I am deleting the node correctly.
5. I am not sure if I need template <typename T> above the iterator functions. Does the iterator class need to be a template? Right now it is not.

// Templated doubly linked list class

#include <iostream>
using namespace std;
template <typename T>
class list {
private:
Node *head;
Node *tail;

[Code] ....

View 7 Replies View Related

C/C++ :: Add Function To Tail Of A Doubly Linked Circular List

Nov 30, 2014

I have a function that I would like to add to tail. It is not correct.

template<class T>
void DoublyLinkedCircularList<T> :: addToTail(T element) {
DoublyLinkedNode<T>* head;
DoublyLinkedNode<T>* temp;
temp->element = element;

[Code] .....

How do I fix it?

View 14 Replies View Related

C/C++ :: Insert Sort Alphabetically Using Doubly Linked List

Mar 16, 2014

is this correct? I used this sorting with numbers i don't know if it is the same with strings. When I run it, there are no errors detected, but when i try to view it, the inputs does not appear.

void add(node **h, node **t){
node *temp, *ptr;
char s[20];
temp = (node*) malloc(sizeof(node));
printf ("-INSERT-");
printf("Fruit: ");
scanf("%s", temp->fruit);

[Code] .....

View 1 Replies View Related

C/C++ :: Freeing Allocated Memory In Doubly Linked List

Feb 21, 2015

I am having issues freeing memory that I allocated when adding a node to a doubly linked list. I have tried adding free() at the end of the remove function from the list with no luck. I have tried using all sorts of temporary nodes and dummy nodes to free without losing node information. Have tried storing current node, moving to next one, then freeing the old current one, without luck. Everytime I try to free a node it destroys the list. It loses important node information and can no longer operate properly and I am met with all sorts of memory crashes. I will post my add and delete nodes functions here:

/**
* Adds a node to a given list
*
* @param q pointer a a list
* @param node pointer to the node to be added
*/
void list_add(list *q, path *node){
path *pn;
if(!(pn = (path*)malloc(sizeof(*pn)))){
perror("malloc");
exit(1);

[Code] ....

Those free's at the end are to get rid of nodes I malloced in find_path. This find_path works really well when run once lol. It finds shortest path and prints it no problem, but doing it over and over again will be problematic as it is leaking almost every bit of memory it uses />.

So in short, how to free an allocated node when I remove it from a list while still being able to use it? I have tried moving the remove function to different locations like the end of the file and still no luck. I even tried allocating a new current_node each iteration of while loop, using it, then freeing it at the end of the while loop and took out the allocation in the list_add() function. This didn't work either />. How to stop the leakage.

View 8 Replies View Related







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