C++ :: Infinite Loop For Pointer Variable In Linked List
Jan 14, 2014
I am getting and infinite loop for loop pointer variable current which points to head and is incremented by current->next in while loop. I use the pointer variable the same way in my display routine and it works. Here is listing of code.
#include "stdafx.h"
#include<iostream>
struct node{
int data;
node *next;
[Code] .....
View 1 Replies
ADVERTISEMENT
Jan 17, 2014
I was having problems changing the value of my head node I passed it as an argument as head which would be the address. The parameter was defined as struct node *head. like this
bool deleteNode(struct node *head, struct node *delptr)
I tried manipultaing pointer values to change head node value but it did not work. I saw some code online which used pointer to pointers(in code below) to change head node value it worked I dont fully understand why. Would like better understanding of why.
Would also like to know why the argument call needed &head instead of just head.
remove = deleteNode(&head,found); opposed to remove = deleteNode(head,found);
#include "stdafx.h"
#include<iostream>
struct node{
[Code].....
View 1 Replies
View Related
Jan 27, 2015
I've been playing around with making a linked list in C and am having trouble adding to the beginning of the list. For the add_beg() function: the statement "head = newNode" only works locally. This leads me to believe this is a pointer problem. However, the add_end() function works correctly. Ideally, I would like to print "0 1 2 3 4" after using the add_end(root, 0). Currently, print only gives me "1 2 3 4".
#include <stdio.h>
#include <stdlib.h>
//Create a node struct
typedef struct node {
//data
int val;
[Code] .....
View 11 Replies
View Related
Aug 11, 2014
I'm writing a delete function for a linked list, and I'm having issues with this bit of code:
void deleteNode(int data){
node* del = NULL;
t = h;
n = h;
while(n != NULL && n->_data != data){
t = n;
n = n->next;
}
}
Or more precisely, this portion:
&& n->_data != data
n is my new node variable, _data is the storage variable in the private section of my class, and data is the information being searched for that needs to be deleted. Everything works without this section of the code. My assumption is that n->_data is somehow wrong, but I don't see how. I've tried everything I can think of- using parenthesis, using the variable rather than the pointer, I've tried expressing the pointer in a different way, I've tried using my t variable rather than n, and I've found examples online that use this exact same expression without any issues.
View 2 Replies
View Related
Aug 29, 2013
I was trying to implement Floyd's cycle finding algorithm to find loops in linked list. I implemented my code using 2 pointers but I am not able to figure out why my output is always is 0 even though a loop exists in the code.
#include "iostream"
using namespace std;
struct Node{
[Code]....
View 5 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
Nov 30, 2014
Why is it sufficient to only have a pointer to the last node of the list?
View 3 Replies
View Related
Mar 30, 2015
I'm having a bit of an issue here. I have a linked list where each node contains a pointer to a string (which has been malloc'd when the node was created and inserted) and a pointer to the next node in the linked list.
I'm creating a function which will free the node (or effectively delete it). However, I'm receiving a free(): invalid pointer error.
My function:
void removeNode(node *(*nodeToRemove))
{
free((*nodeToRemove)->data);
(*nodeToRemove)->next = NULL;
free(*nodeToRemove);
}
Is this how I should go about freeing this node?
View 5 Replies
View Related
Apr 22, 2013
Having trouble with concatenating a linked list.. When I call the display function for the concatenated list, it loops endlessly?
#include <iostream>
#include <cstdlib>
using namespace std;
#ifndef Null//Null is being defined to 0 here
#define Null 0
#endif
[Code] ....
View 5 Replies
View Related
Jul 6, 2013
I'm trying to run so called student-administration program.I got functions that can read and write student data, but the one saving the data from about 30 students has some problem that I can't figure. (warning: I'm quite new to C programming)so this is the code:..I guess I can't use global variables as function arguments?
Code:
//global variables
static char ReturnName[31];
static char ReturnFirstName[31];
static float ReturnAverage;
}
[code]....
incompatible types when assigning to type 'char[31]' from type 'int'
View 2 Replies
View Related
Feb 27, 2015
I am having trouble modifying a linked list. I am writing a function to delete the last node from the linked list, but it gave me incompatible types error.Here is my struct:
Code:
typedef struct PCB{
int id;
struct PCB *next;
struct PCB *prev;
}PCB_rec, *PCB_p;
Here is my function to delete the last node (given the pointer is pointing at the last node of the list):
Code:
void del_last_node(PCB_p *process_list){
PCB_p temp = process_list;
if (temp->prev != NULL){
temp = temp->prev;
[Code] ....
And here is how I called the function:
Code: del_last_node(&process_list);
It gives me the following errors:
initialization from incompatible pointer type at line:
PCB_p temp = process_list
assignment from incompatible pointer type at line:
process_list = temp
View 14 Replies
View Related
Nov 30, 2014
Why would it be cumbersome to maintain an additional pointer to the first node in a doubly linked list?
View 1 Replies
View Related
May 5, 2014
I'm trying to write a function called 'set' that sets the value of the i'th cell to val on my linkedList, and then returns the previous contents. I am stuck on it due to compiling errors:
This is my Node struct:
#ifndef NODE_H
#define NODE_H
#include <iostream>
template <typename T>
struct Node {
friend std::ostream &operator <<(std::ostream &os, const Node<T> &node) {
[Code] ....
The following is my set function:
template <typename T>
T set(Node<T> *head, int i, const T &val) {
for(int n = 0; n < i; n++)
if(head == val) {
val = i;
} return val;
} #endif
When I try to call it in the main() I get these errors:
node_utils.h: In function ‘T set(Node<T>*, int, const T&) [with T = int]’:
node_demo.cpp:26:38: instantiated from here
node_utils.h:161:2: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
node_utils.h:162:3: error: assignment of read-only reference ‘val’
So, I understand that I can't compare head & val on my if-statement -- But what do I do?
View 7 Replies
View Related
May 19, 2014
I am trying to write a generic linked list in c, but for some reason i keep getting errors saying "incompatible pointer type. This is the code and erros:
#ifndef SLIST_H
#define SLIST_H
#include <stdlib.h>
typedef struct {
void *data;
slist_elem *next;
[code]....
View 2 Replies
View Related
Mar 1, 2013
What I need to do to get rid of the infinite loop?
Code:
do {
printf("Enter the number of tests:");
scanf("%d", &test);
if (test< 0 || test> 4)
printf("Wrong number. Please try again!
");
}
while (test< 0 || test>4);
View 9 Replies
View Related
Feb 24, 2015
my code seems to enter an infinite loop should a user mistakenly enter a character other than a digit. The code is about selecting from a preset number of options, numbered 1- 4. If the user mistakenly enters a letter for example, instead of a number, the infinite loop kicks in...
View 1 Replies
View Related
Jan 27, 2015
So I have to create a program which will print, among other things, a rectangle made of asterisks. The rectangle has to follow this general format:
*****
* *
*****
but with the width and length of the rectangle being set by the user. I've tried every way I can think of to work this out, but I can't seem to get anything to work. The main errors I'm getting are either an infinite loop of asterisks filling my screen or nothing at all, depending on whether I use an && comparison or ||. A screenshot of my code is included below.
View 5 Replies
View Related
Feb 20, 2013
The code below gives me the maximum odd number and the minimum even number of a list of numbers that the user type. However, for the loop to stop the user needs to type 1000. Is there a better way (without having to have a "stopping" number, just in case I need this number to be somewhere on the list) to get the same results?
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main() {
int x, maxi, mini;
[code]...
View 2 Replies
View Related
Sep 5, 2014
I have a small loop that gets the user input, as well as acts as input validation to make sure he or she doesn't enter a value that is not within the specified range.
The user is supposed to enter a month number. E.g. if he or she chooses February, then enter 2, or December, 12.
It works correctly if they type a number that is not in the range, but goes into an infinite loop if, say, they type a string such as "month".
Code:
int main() {
// Variable Declaration(s)/Initialization(s)
int month=0;
// Get input
cout<<"Enter a month";
cin>>month;
[Code] ....
How could I go about revising this?
View 4 Replies
View Related
Nov 17, 2013
I have the program working but when I check if the input to make sure it is not a char it creates a infinite loop. I used an if statement to check for the issue and solve it but its not working. I even tried throwing an exception but I learned later they are not used for things like this.
Code:
#include <iostream>
#include <limits>
#include "contacts.h"
using namespace std;
int main()
[Code].....
View 10 Replies
View Related
Feb 10, 2015
So I learned how to make a basic for loop and I decided to try my best to make an infinite one. Every time I run it, it doesn't say anything and just closes. Visual Studio doesn't say there's anything wrong with my code.
Here's the code
#include <iostream>
#include <conio.h>
using namespace std;
int main () {
int d = 9;
for(int k = 10; k < d; k = k +1) {
cout << "For loop value = " << k << endl;
getch();
} }
View 4 Replies
View Related
Oct 31, 2014
Why I have an infinite loop when reading from a pipe?
i = 0;
while(i < 10) {
test_value = read(pipe3[READING], &message, sizeof(struct MESSAGE));
printf("test_value is %d
", test_value);
//printf("Entering infinite loop
[code]....
View 10 Replies
View Related
Dec 12, 2014
I have more programming before this, but everything else works fine. My else loop has an infinite output. i also want it to output an error message if the value is not an integer and return to the input
cout << "Select number corresponding material for fencing:" << endl;
cout << "wood = 1" << endl;
cout << "stone = 2" << endl;
cout << "aluminium = 3" << endl;
cin >> material;
if (material == 2){
cost = 100 * perimeter;
[code]....
View 7 Replies
View Related
Sep 9, 2012
I currently have a hangman game in the making. Which is giving me debugging issues when I go to pick a letter, it will keep asking for a letter, if I place a break; within the loop it asks for a letter and says you've won. I know all I should need is a couple extra lines somewhere within the code.
/// Play game
public static string playGame() {
Words words = new Words();
[Code].....
View 2 Replies
View Related
Aug 13, 2014
From the example given below, I would like to Generate a matrix who stores a generated array for each iteration. I have an understanding of inputting single elements to a matrix but how can I input an array to a matrix. let's say I would like to input and array of 4 elements that should be stored as a single row of the generated matrix and do this for an infinite while{true} loop.
#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
void printArray(int arr[], int size) {
[Code] ....
View 7 Replies
View Related
Apr 12, 2014
I wondering how to stop a infinite loop. The program clears a file then opens it and refreshes it over and over(the file is going to be modified by another program). I want it to start from the beginning anytime i press enter or escape, it doesn't really matter as long as you can restart it.
Here is the complete code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <windows.h>
using namespace std;
void x() {Sleep(1000);}
[Code] .....
View 2 Replies
View Related