C++ :: Implementing Double Linked Lists As Array Of Pointers

Dec 27, 2012

I am studying/writing/ code for a future Advanced Data Structure class in C++; however I am not suppose to use STL, Templates and etc (the way I just code "kinda" of resembles what I have to use).

My application is suppose to read/analyze all integers contained in a text file of 5-digit integers (10000 - 99999).

For simplicity I am using the following input (or check the attached):

20007 20008 20009
20010 20010
20012 20012
20013
20014 20010
20015
20016 ....

So far, my code is not displaying/printing the lists separated by the first digit of these 5-digits integers. I am expecting it to display/print logically/similar to the following:

Output:

Results for input file numbers.txt:
27 total integers read from file

The 3 unique integers beginning with digit 1 were
18399 17342 19948

The 6 unique integers beginning with digit 3 were
39485 34710 31298 38221 35893 32791

The 4 unique integers beginning with digit 4 were
43928 49238 45678 43210

The 6 unique integers beginning with digit 6 were
64545 62987 66221 61777 66666 65432

The 2 unique integers beginning with digit 8 were
88888 86861

The 1 unique integer beginning with digit 9 was
98765

There were 22 unique 5-digit integers in the file.

The highest unique count in one list was 6 integers.

My code that will follow soon displays/prints only the LAST 5-digits "group" of integers (in this case the 5-digits starting with 3). I am not sure what's wrong with my code; perhaps I am not designing it correctly. May be my calls in it are on the wrong place or I have to write all integers and then traverse it and output it (if that's the case, I am not sure how).

My code follows:

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int MAX_CELLS = 10;
const int UNIQUE_FIVE_DIGIT = 5;

[Code] .....

View 10 Replies


ADVERTISEMENT

C :: 3D Array Of Pointers To Linked Lists

Aug 8, 2013

I am having some trouble getting a 3d array of pointers to link up with a linked list for each of the array elements. the 3d array of pointers is declared like this:

Code:

struct particle *cell[MAXCELLS][MAXCELLS][MAXCELLS]; and there are linked lists declared like this:
Code: struct particle { /* structure for particles */
double sw[3]; /* square well components */
double hs[3]; /* hard sphere components */
double u[3]; /* unit vector for rotations */
struct particle *link;
};

I want to get the array pointers 'cell[x][y][z]' to point to the first observed particle in a function next to main, something like this:

Code:

void generate_list(){
int i,j,k,l;
/* determine the number of cells to decompose the simulation box in each of x, y and z directions*/
int(cells_x) = floor(boxX/cell_size);
int(cells_y) = floor(boxY/cell_size);
int(cells_z) = floor(boxZ/cell_size);
/* initialise the array of pointers to NULL */
for (j=0;j<cells_x;j++){

[Code]...

I am getting a pointer type cast error when I compile "assignment from incompatible pointer type",

View 6 Replies View Related

C :: Free Memory From A Dynamically Allocated Array Of Pointers To Linked Lists

Feb 26, 2013

Having some frustrating issues trying to free memory from a dynamically allocated array of pointers to linked lists. I think the problem is in how I initialize the pointers to NULL. Is there a more elegant way to have the program recognize that the list is empty so it knows to create a head node for the linked list in the function 'add_end_stub_to_array'?

I ran the code through Valgrind and it says that memory is definitely lost from this array.

This is the structure definition.

Code: struct stub_edge {
int loc_id;
int anim_type;
int mkt;
struct stub_edge *next_node;
};

Here is the code snippet from main allocating and deallocating memory to the array.

Code:

struct stub_edge **stub_list = (struct stub_edge **)malloc( sizeof(struct stub_edge *) * 12);
for (i = 0; i < 12; i++)
{
stub_list[i] = (struct stub_edge *)malloc(sizeof(struct stub_edge));
stub_list[i] = NULL;
}
stub_list = add_end_stub_to_array(end_stubs, stub_list);
destroy_end_stub_array(stub_list);

Here the function for adding nodes to the lists by reading through a dynamically allocated 2D array. (The end_stubs array is ordered by month and each linked list represents events occuring within the month).

Code:

struct stub_edge **add_end_stub_to_array(int **end_stubs, struct stub_edge **list)
{
long int i = 0;
int mon = 0;
struct stub_edge *current_node1;
struct stub_edge *new_node1;
int break1 = 0;
while(i < num_edges && break1 == 0 && mon < 12)

[Code]...

Here is the function for freeing memory from the list.

Code:

void destroy_end_stub_array(struct stub_edge **list)
{
if(list != NULL)
{
int mon = 0;
struct stub_edge *current_node1;
struct stub_edge *new_node1;
for(mon = 0; mon < 12; mon++)

[Code]...

View 1 Replies View Related

C/C++ :: Implementing Stack Using A Double Linked List

May 12, 2014

So im trying to write a code that will take data in from a user and when that user enters specific character then i want to pop the top of the stack and place that node on a new stack. Then if the user enters a different specific character then i want to place what was just popped off the stack back on to the original stack. Anyways i'm just testing what i have without all the complicated stuff with the specific characters, but i get an error and i'm not not well versed in exception handling. So this is what i have so far. the push seems to work well but the pop seems to be giving me problems.

Stack::Stack(){
top = NULL;
bottom = NULL;
}
//method to push a line of text onto the stack
void Stack::push(string data)

[Code] .....

View 4 Replies View Related

C :: Why Most Examples Pass Double Pointer When Manipulating Linked Lists

Mar 1, 2014

Why do most C examples pass a double pointer when manipulating linkedlists? Why can not we just pass a single pointer to the struct?I think using an external reference accessor for a linked list would be a more appropriate solution, yes or no?

View 1 Replies View Related

C/C++ :: Implementing Tree From Array - Use Of Pointers?

Jan 23, 2015

I'm trying to implement a tree from an array an first I used add_left_node() and add_right_node methods (as requested in the problem I have). But I'm a bit confused about the use of pointers in this. Is using &node the same as defining *pointer=node and then using "pointer" in place of &node?

I get only the value of the first node I created. For the node's left and right values I get long numbers displayed, so I think I have interfered with the addresses.

This is my main method:-

int main(){
bintree_node q = bintree_node(1);
bintree_node *poin=NULL;
poin = &q;
add_left_child(poin, 5);
add_right_child(poin, 6);

[Code] ....

View 6 Replies View Related

C++ :: Array Based Implementation Of Linked Lists

Jul 19, 2014

I first want to say that i am trying to solve my code without Pointers.

My goal is to..
1. Construct an empty 2D array with a capacity of 25. (list[25][2];)
2. Empty(): test if the stack is empty
3. Push(): add a value to the stack in the (list[i][0] = value;) position and (list[i][1] = previous list[i][0] position)
4. Top(); read the value(list[i][0]) at the top(count) of the stack
5. Pop(); remove the value at the top of the stack (list[i]= 0;)
6. Display(); displays all the elements in the stack going from the top to bottom order. (shows array index, data value, and next array index)

I have hit a road block and don't know what to fix or where to go from here.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
char name[5];
srand(time(0));
//Protoypes
void construction(int table[25][2]);

[Code]...

View 1 Replies View Related

C :: Make A Hash Table Array Of Linked Lists / Separate Chaining Technique

Jul 5, 2013

how to create a hash table array and use linked lists inside the elements.

View 5 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 :: Implementing And Manipulating Polynomial ADT Using Linked List

Sep 23, 2014

Implementing and manipulating a Polynomial ADT using a linked list.

So far I have:

poly_ADT.h
Code: typedef struct nodeT{
int coef;
int powr;
struct nodeT *next;
} node;

[Code]...

I need to create a function that creates the polynomial using input first.

poly *poly_create (num,...) ;return a new polynomial with num terms terms are listed in order of lowest ordered-term to highest. i.e., to initialize poly 15x^6 + -9x^4 + 3x^2 call poly_create(3, 3,2, -9,4, 15,6 );

Once I do that I need to implement various functions that can manipulate the polynomial itself but I'm having trouble just with creating the polynomial itself, how to do that using a linked list and nodes?

View 3 Replies View Related

C++ :: Linked Lists - Trying To Revive

Dec 27, 2013

I'm trying to set up a simple implementation of a double linked list. I can't make it fly.

Code:
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

[Code] ...

I seem to create a root vertex, but I can't establish if I connect sub nodes to my list.

View 13 Replies View Related

C :: Merging Two Linked Lists?

Mar 6, 2015

I am merging two linked list but the output of the program is not printing insert

Code:
typedef struct Merge
{
int info;

[Code].....

View 1 Replies View Related

C :: Dynamic Linked Lists

Oct 14, 2013

In our homework assignment it states to, "write a set of dynamic linked lists" and so on. Now, the problem I'm confusing myself is at the storage inside of each node. To my understanding, each node contains the prev and next nodes, but also a pointer to the data, in this case being a string. He has asked us to manage these strings as rows of chars such as

char[0] = c // first node being addressed here
char[1] = a
char[2] = t
char[3] =
char[4] = d // second node starting here
char[5] = o
char[6] = g
char[7] =

I have written my code where each node is holding a string, not separated as shown above... my question is to how you can build your doubly linked list where each node is being address a set of chars.

View 1 Replies View Related

C++ :: Nodes In Linked Lists?

Feb 26, 2015

I created a bunch of nodes and I made each one before one another. Im having trouble adding another node to the last one.

#include <iostream>
using namespace std;
struct nodeType{
int info;
nodeType *link;
};
void printList(nodeType *head) {
nodeType *current = head;

[code]....

The node with the value of 400 is the node that has to be last.. How would that work?

View 3 Replies View Related

C/C++ :: Working With Linked Lists?

May 11, 2014

I'm trying to do is let the user type in something like A654321 (note: these are ALL the characters, i.e. 'A', '6', '5', '4', etc..) Then I just want to display that current number back to them and I am getting some weird pointer memory allocation error..

#include<iostream>
using namespace std;
//To rename data-type "int" or integer to "element" (Both are equivalent)..
typedef int element;
//To declare an appropriate SENTINEL.
char const SENTINEL = '#';

[code]....

It seems like the first part that it bugs out at is in my Clean(); function..

Unhandled exception at 0x01383FBB in VegMe.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0.

It doesn't like head = head->next; ?Also, ignore the ReverseList(); function, it is not being used as of now.

View 7 Replies View Related

C :: Reading A File To Linked Lists?

Jun 20, 2013

I have to write a program that reads from a text file, which contains a list of stock hourly prices and the company names. Something like this:

78.52 82.56 75.10 71.97 Water Company
22.40 25.68 21.37 22.96 Mega Shipping Inc

Each company data will be kept in a structure that contains: a pointer for the name, an array of the prices, and the average price for the day. The structures will be kept in a linked list. Each node structure will contain a pointer to one company data, and a pointer to the next node structure.This is all I have so far..

Code:

typedef struct {
char *companyName;
double hourlyPrices[NUM_PRICES];
double avgPrice;
} COMPANYDATA;

}

[code]...

We have to sort the nodes alphabetically. My question is, how do I read the data from the file one line at a time, and insert them alphabetically one by one?

View 7 Replies View Related

C++ :: Compare Single Value With A Value From Linked Lists

May 2, 2013

I'm trying to compare a single value with a value from my Linked list and if they are same, I want to add the value from the list to new list. In other words I want to create a new List with values with the first one. Here is the code that I made, but it's not working.

This is the code with which I search in the first list for a node with a value. In the main() function I have A.find_city(), so it can start from the start_pointer from the first list:

void List::find_city() {
List *temp1;
int b = 0;
char city[20];
cout << "Enter city: ";
cin >> city;
temp1 = start_ptr;

[Code] ....

This is the code with which I add a node to the new list:

void List::in(List *temp1) {
List *temp2;
if(start_ptr == NULL)
start_ptr = temp1;

[Code] ....

View 9 Replies View Related

C++ :: Stack Implementation Of Linked Lists

Jun 7, 2014

I have this code that I need to memorize for my final. Memorizing code is easy for me, but I'm trying pretty hard to fundamentally understand the functions, and what they are doing (even using pen and paper, to draw and trace).For example, in the push function below, I understand everything, except why I'm setting ptr = p. I feel like p should be equal to NULL, then the next node I push should be equal to p, etc.

Stack & Stack::push(double x)
{
Node * p = NULL;
try
{
p = new Node;
}

[code].....

Also, are LL Queues that hard to implement once you can do them w/stacks - That will probably be something I have to code for my final, as well. Below is the full code for my Stack class.

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
class Stack
}

[code]....

View 1 Replies View Related

C++ :: Templates And Ordered Linked Lists

Feb 10, 2014

A static method named readFromFile that takes a C-string as the first parameter, and an orderedLinkedList<MemberDO> object as the second parameter. The first argument is the filename that contains data for existing members. This method should read the data for each individual member from the input file (one line of data per member), create a new MemberDO object, and insert this object into the linked list specified in the second argument.

How do I take the second parameter in, do I need to create the Linked List first? Here is the code I have so far

#include<iostream>
#include<string>
#include <fstream>
using namespace std;

class MemberDO {

[Code] ....

What to do with the readFromFile static method?

View 1 Replies View Related

C++ :: Can Compare vectors With Linked Lists?

Jan 31, 2014

Can we compare vectors with linked lists?

View 4 Replies View Related

C++ :: Merge Two Linked Lists Without Sort

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

C++ :: Merge Sort And Linked Lists

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

C++ :: Searching For Objects In Linked Lists

Jun 18, 2013

So I have a Class named Subject, wich I used linked list to keep the objects. Now I have another Class called Tests which I should register a test by the Subject class, so I made a search like this:

List obj; //it's the object to access the List class, which manipulates the
//nodes from Subject.

obj.currentPtr=obj.firstPtr;
while (obj.currentPtr!=NULL) {
if(obj.currentPtr->nameSubject==value) //searching if there's a
return obj.currentPtr; //name equal to provided
obj.currentPtr=obj.currentPtr->nextPtr;
}
return 0;

I've made a similar process for Subject. But in this when I access the firstPtr, it's shown to be 0, once I have already the list of Subject and it should not be zero. What's wrong?

View 3 Replies View Related

C/C++ :: Function To Concatenate Two Linked Lists

Mar 6, 2014

The objective of the project is to become familiar with linked lists by writing a series of basic functions that allow us to manipulate a set of linked lists. I've completed all of the functions except for one.

The function I am stuck on is supposed to do the following:

concatenates list1 and list2 to form a new list,
and returns a pointer to this new list;

Note: new list contains copies of all nodes in list1 and list2 */

View 7 Replies View Related

C/C++ :: Maintaining Two Distinct Linked Lists?

Mar 21, 2015

The assignment is apparently pretty classic: given a starting state and a goal state, show the optimal path to solve a 3x3 sliding puzzle.

I have the code in place.Where I'm experiencing major issues is in maintaining two distinct linked lists. One should contain the frontier of the A-star algorithm and the other records the optimal path. In execution, however, it seems that only one list is in local memory in any one function. Therefore, when my function recurses, the lists begin to get confused and the whole algorithm veers off course.

Here is my structure definition in list.h:

typedef struct node {
int value;
int state[3][3];
struct node *next;
} Node;
typedef struct list {
int count;
Node *first;
} List;

Here is my List_create function in list.c (do I need malloc here?):

void List_create(List *list)
{
list->first = NULL;
printf("LIST CREATED
");
}

And here's how I'm declaring the lists in main.c:

int main(void)
{
List open;
List closed;
List_create(&open);
List_create(&closed);

Here's the general flow of the program, without posting a wall of code: both lists are passed to another function in main.c, expand(), with this line:

expand(current, gState, &open, &closed);

The lists are later passed from expand() to another main.c function, generateOptions(), in this line:

generateOptions(&array, goalState, open, closed);

generateOptions() returns back to expand(), which recurses on itself with the first item in the open list and continues the expansion:

expand(open->first, goalState, open, closed);

Strangely, the open list prints out correctly before generateOptions() returns to expand(), and the closed list prints out correctly after generateOptions() returns to expand(). But when I try to print the open list in expand(), I'm instead given the closed list. Like I said at the outset, it appears that only one list exists at a time!

View 2 Replies View Related

C++ :: Merging 2 Linked Lists Into Third One Via Recursion

Apr 17, 2012

I am having to write a program that takes 2 sorted linked lists (x and y) and merge those to sorted into a third one (z). This has to be done via recursion. I think I am close but there is a logic error.

Code:
void SortedMergeRecur(Node*& xHead,Node*& yHead,Node*& zHead)
{Node* temp = 0;
if(xHead == 0) {
zHead = yHead;
yHead = 0;
}

[Code]...

View 3 Replies View Related







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