Okay. This weeks 'problem' is that I need to simulate a game. I have to make two singly linked lists which will contain a name of player, his attack (double/float) number and his defence number and so on for as many players as the user inputs. The point of the game is to try to break the other 'chain'. You do that by running into 'enemies' chain and trying to break it, you break it if your attack is greater then defence of two players that you are trying to separete. If you separate them you can pick one of …

Member Avatar
Member Avatar
+0 forum 8

So here's my problem I have inner structure inside of client. How can I know while reading from txt file that I'm reading item data or client data for my list? struct item { char item_name[30]; char item_state[30]; float item_price; char item_status[30]; float item_price_if_not; struct item *next; }; struct client { char client_name[30]; char client_last_name[30]; struct item *item_data; struct client *next; }; void savetxt(struct client *head) { FILE *f; f = fopen("data.txt","w"); if(f == NULL) { printf("error"); } struct item *CurrentItem = head->item_data; while(head != NULL) { fprintf(f,"%s %s\n",head->client_name,head->client_last_name); while(CurrentItem != NULL) { fprintf(f,"%s %s %f %s %f ",CurrentItem->item_name,CurrentItem->item_state,CurrentItem->item_price,CurrentItem->item_status,CurrentItem->item_price_if_not); CurrentItem …

Member Avatar
Member Avatar
+0 forum 18

I'm trying to make make project for my programming class. All that's left is deleting elements and editing elements. Basicaly my question is how would I do it for my inner list? Or how would I delete single client from list? Would I first have to free the client's inner list if so how should I proceed to do so? #include <stdlib.h> #include <time.h> #include <string.h> #include <stdio.h> struct date { int day; int month; int year; struct date* next; }; struct item { char item_name[30]; /* char item_state[30]; double item_price; char item_status[30]; double item_price_if_not; struct date *issue_date;*/ struct item …

Member Avatar
Member Avatar
+0 forum 10

Im writing project for my programming lessons and right now got some problems with output from function AddItemToClient, basicaly its mainly with date, in first node its correct one but after this its just random. Could someone explain what I'm doing wrong here? #include <stdlib.h> #include <time.h> #include <string.h> #include <stdio.h> struct date { int day; int month; int year; struct date* next; }; struct item { char item_name[30]; char item_state[30]; double item_price; char item_status[30]; double item_price_if_not; struct date *issue_date; struct item *next; }; struct client { char client_name[30]; char client_last_name[30]; struct item *item_data; struct client *next; }; //ADD CLIENT// …

Member Avatar
Member Avatar
+0 forum 2

Hi I've tried the code below to merge 2 linked list which f1 and f2 are the first nodes in the initial lists and the "first" is the first node in the result list. But it doesn't work! [CODE] node *merge(node *first,node *f1, node *f2,int n1,int n2){ int n=n1+n2; while(n-- > 0){ if(n1-- > 0)first=insert(first,f1->info); if(n2-- > 0)first=insert(first,f2->info); } return first; }[/CODE] Which insert is the following function that construc a list: [CODE] node *insert(node *first, int x){ if(first==NULL){ first=new node; first->info=x; first->next=NULL; } else{ node *Temp=first; while(Temp->next!=NULL) Temp=Temp->next; Temp->next=new node; Temp=Temp->next; Temp->info=x; Temp->next=NULL; } return first; } [/CODE] Looking …

Member Avatar
Member Avatar
+0 forum 6

So, I'm using allegro game programming library. Before I move on to bigger and better things I wnat to get a handle on pointers. Specifically with things like structs, linked lists and maybe binary trees in the future. I posted a program that uses a linked list to keep tracks of a variable number of objects on the screen. GDB tells me it fails in the main also, the new style of posting is kinda buggy, for example I can move the cursor past the program code. gdb: Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: …

Member Avatar
Member Avatar
+0 forum 3

I need to know how to create methods to compare two sets of singly linked lists. The only problem is that I can't put a linkedlist inside the method parameters. Please help [CODE]import java.util.*; public class SLinkedList { protected Node head; // head node of the list protected Node tail; protected int size; // number of nodes in the list /** Default constructor that creates an empty list */ public SLinkedList() { head = new Node(null, null); // create a dummy head size = 0; } public boolean(SLinkedList obj){//tells me identifier is expected return true; } [/CODE]

Member Avatar
Member Avatar
+0 forum 2

Hello I am a new to C++ and have some questions. I have two classes called Point and Square like this: [CODE] class Point { int x, y; public: int getx() const { return x; } int gety() const { return y; } Point(int a, int b) { x = a; y = b; } }; class Square { public: int x, y, length, width; SquareSpec(){return x, y, length, width;} Square(Point, int, int); }; Square::Square(Point A, int lngt, int wdth){ length = lngt; width = wdth; x = A.getx(); y = A.gety(); } [/CODE] Now the thing is that somehow …

Member Avatar
Member Avatar
+0 forum 14

Hey all, I've been programming an ISBN system of sorts and I'm a little stuck (again). I want to output my linked list to a text file, so I setup the code like this: [code] void saveList(char fileName[40]) { for ( list< Publication* >::iterator it = pubList.begin(); it != pubList.end(); it++) { try{ std::ofstream ouf(fileName); if( !ouf ) throw std::ios::failure( "File Error" ); ouf<<"Publication,"<<(*it)->getPublisher()<<","<<(*it)->getIsbn(); ouf.close(); }catch( const std::exception& e){ std::cerr <<e.what()<<endl; } } }[/code] Now saving like this works alright with a small issue. I can only access the public functions of the base class object, I can't access the …

Member Avatar
Member Avatar
+0 forum 8

Hey all, I'm attempting to use an STL List to store a list of objects. Now I realise it's quite trivial to store a list of objects of my own definition. My real question is, how do I fill the list with objects that inherit from a base class. E.g. I have a class called Book which has the derived class Publication. How would I get the list to accept the base class aswell as derived classes? currently I am doing it something like this: [CODE] list<Book> objectList; //define list Book temp("Title", "ISBN"); Publication pubTemp("Title", "ISBN", "Publisher"); objectList.push_back(temp); objectList.push_back(pubTemp); [/CODE] …

Member Avatar
Member Avatar
+0 forum 4

I just cant get my head around it. Is there another way going through a singly linked list without using some kind of helper function? The header file wants me to implement: [CODE]size_t priority_queue::size() { /* code here */ }[/CODE] Now I just could create a helper function that has a node as parameter and as long as node->get_next() != 0 I recursively call the helper and add 1 to a counter. But is there actually a way to get through a singly linked list without a helper function? I hope I was clear enough.

Member Avatar
Member Avatar
+0 forum 4

Hello all, here is the code I came up with for implementing a circular linked list. I get a segmentation fault when I create the list with just one node. With size > 1 circular list works fine! Im not sure but I think it may have something to do with the head pointer pointing to itself.. any ideas? [CODE] #include <stdio.h> using namespace std; #ifndef CIRCULARLINKEDLIST_H_ #define CIRCULARLINKEDLIST_H_ class Node { public: char* value; Node* next; int index; Node(int in, char* val,Node* nextNode){ index = in; value = val; next = nextNode; } }; class CircularLinkedList { public: Node* …

Member Avatar
Member Avatar
+0 forum 9

There are n persons numbered from 0 to n - 1 standing in a circle. The person number k, counting from the person number 0, is executed. After that the person number k of the remaining persons is executed, counting from the person after the last executed one. The process continues until only one person is left. This person is a survivor. The problem is, given n and k detect the survivor's number in the original circle. 'Quoted from [URL="http://acm.uva.es/archive/nuevoportal/data/problem.php?p=3521"]http://acm.uva.es/archive/nuevoportal/data/problem.php?p=3521[/URL] :)I've written the code below.It works properly unless the user enters '1' as the period of counting:$ [CODE] // Joseph …

Member Avatar
Member Avatar
+0 forum 2

i am trying to move the node in the first item from the list and add it to the back of the list 1->2->3 becames 2->3->1 instead i get 3 1 2 [CODE] #include <iostream> using namespace std; struct nodeType { int info; nodeType *link; }; void createList(nodeType*& first, nodeType*& last); void printList(nodeType*& first); void moveFront2Back(nodeType*& first, nodeType*& last); int main() { nodeType *first, *last; int num; createList(first, last); printList(first); moveFront2Back(first,last); system("PAUSE"); return 0; } void createList(nodeType*& first, nodeType*& last) { int number; nodeType *newNode; first = NULL; last = NULL; cout<<"Enter an integer (-999 to stop): "; cin>>number; cout<<endl; …

Member Avatar
Member Avatar
+0 forum 4

Hello everyone. I've been trying to implement a linked list in C++. I found this implementation on the web, where they created a struct for the list's nodes. When trying to add a new node to the list, I get this error: [B]List.C: In member function `bool Linked::addNode(Point)': List.C:23: error: no matching function for call to `Linked::node::node()' List.H:35: note: candidates are: Linked::node::node(const Linked::node&)[/B] And here's my Code, and thank you very much.. :) [B]List.H[/B] [CODE]#ifndef _AUXILIARY_H_ #define _AUXILIARY_H_ #include <string.h> #include <math.h> class Linked { public: // Constructor: initializes a set of nodes Linked(); // Linked methods bool addNode(Point p); …

Member Avatar
Member Avatar
+0 forum 5

Hello everybody! I have to Questions to ask please: 1) How can I find the maximum value in a single linked-list recursively? this is what I tried to do: int findMax(int key){ Node max=head; while (max != null){ if (max < max.getKey()) return(max.getNext()); } } it ends up to an error.. :( 2) How can check palindrome in a single linked-list recursively? Thank you for All ^ ^

Member Avatar
Member Avatar
+0 forum 18

Pretty interesting problem about linked list. This program is about autobuses. Each autobus is given by brand, registration number, number of seats, kilometres and year when the bus was made. The program is organised as menu and when you hit number it calls some function. Problematic function: it should NOT PASS autobuses with the same registration number but this function(int povtorenie()) doesn't work properly if you type equal registration numbers on positions different from 1 and 2. [CODE]int povtorenie(struct node* call) { char* a=new char[20]; strcpy(a,call->regnomer); call = call->next; while(call!=NULL) { if(strcmp(call->regnomer,a)==0) return 1; else return 0; call = call->next; …

Member Avatar
Member Avatar
+0 forum 2

[CODE] private Node<T> addAt(Node<T> node, T value) { if (node == null) { // special case return new Node<T>(value, null); } else if (node.getNext() == null) { // other special case node.setNext(new Node<T>(value, null)); } else if (node.getstuff().getName().compareTo(value.getName()) > 0) { node.setNext(new Node<T>(value, node.getNext())); } else addAtEnd(node.getNext(), value); } return node; } [/CODE] so my problem is that im trying to call a method of the object in a particular node, but it keep telling me that the method inside my object class is not recognized, cannot find symbol, in this case, the getName method, any ideas?

Member Avatar
Member Avatar
+0 forum 9

I have been working on a number of card games. Important, of course, is shuffling the deck. I have come up with a few different ways to do this. Currently I do not shuffle, but to deal a card, I lay out the deck in order, selecting a random number between 1 and the number of cards remaining and selecting that card. I then remove that card from the deck so that another card can be picked. There are two main ways to do this, and I am trying to figure out which one is optimal. The first is to …

Member Avatar
Member Avatar
+0 forum 1

how do i read in files from a ".txt" file into a linked list heres my code. [CODE] import java.io.*; public class LinkedList1 { class Node { String element; // list element Node next; // successor link Node(String el, Node n) { element = el; next = n; } Node(String el) { element = el; next = null; } } public static main (String[] args) { Node myList = new Node("1"); // AT THIS PART OF MY CODE I WANT TO READ IN INTEGERS FROM A ".txt" FILE // USING FILE IO HOW DO I DO THAT? } } [/CODE]

Member Avatar
Member Avatar
+0 forum 1

The End.