C :: Linked List / Adding Element To Beginning Of List
Dec 31, 2014
Code:
// Write a function called insertEntry() to insert a new entry into a linked list.
Have the procedure take as arguments a pointer to the list entry to be inserted (of type struct entry as defined in this chapter), and a pointer to an element in the list after which the new entry is to be inserted.
// The function dveloped in exercise 2 only inserts an element after an existing element in the list, thereby prenting you from inserting a new entry at the front of the list.
(Hint: Think about setting up a special structure to point to the beginning of the list.)
#include <stdio.h
struct entry1 {
int value;
struct entry1 *next;
};
[code]...
This is a working version of the exercise, but I don't think I'm doing what's asked. I was able to add an element to the beginning of the list using an if statement, not creating a special structure that points to the beginning of the list. How would I go about creating a special structure that points to the beginning of the list to add a new element at the beginning of the list?
View 8 Replies
ADVERTISEMENT
Aug 18, 2013
I've been struggling with linked list for a few days. I've tried performing a few operations. Here is my code. the add_begin() function doesn't seem to work.
#include<iostream>
using namespace std;
struct node {
int data;
node *next;
[Code] .....
View 8 Replies
View Related
Mar 10, 2014
So I have linked list and function which deletes element if next element is bigger, so my code is working but its not working with first element, in the comment I have wrote code which I would code for checking that first element, but when ever I check it is blowing up all program.
#include <iostream>
using namespace std;
struct llist {
int x;
llist *next;
[Code] .....
View 1 Replies
View Related
Jun 6, 2013
This code is from a example in Jumping Into C++ and I understand the example. But it is a practice problem that is to write a program to remove an element from a linked list; the remove function should take just the element to be removed.
Code: #include <iostream>
using namespace std;
struct EnemySpaceShip {
int x_coordinate;
int y_coordinate;
[Code]...
If I got this right I will create a pointer that points to the first SHIP (getNewEnemy) and the other one will not be printed out.
View 7 Replies
View Related
Oct 17, 2014
This is my current code. The code is to remove the element in the list "head" that is "e". The code works if there is only one element in the list but if there are more than 1 element in the list, it doesn't work. The function should return true if there is an element that equals "e" and then it removes it and false if no element equals "e".
bool StringLinkedList::remove(const std::string& e){
StringNode* current = head;
int i = 0;
if (current == NULL)
return false;
[Code] ....
View 1 Replies
View Related
May 2, 2014
I've got this program that I'm working on. Most of the code is from a video tutorial, but I was editing it to be able to search for an element by name. That's working fine, but suddenly the part of the program that prints out all the elements starts in an infinite loop after I input two elements, and search for one.
Here's what I've got:
[code#include <iostream> usingnamespacestd; struct node { int number; string name; node *next; }; bool isEmpty (node *head); char menu (); void insertAsFirstElement (node *&head, node *&last, int number); void insertSorted(node *&head, node *&last, int number); void remove(node *&head, node *&last); void showList (node *current); void showByName (node *current, string name); bool isEmpty (node *head) { if (head == NULL) { return true; } else { returnfalse; } } char menu () { char choice;
[Code] .....
View 8 Replies
View Related
Sep 6, 2014
I'm trying to calculate the total price of books contained in a linked list. I've tried getTotal() function in linked list but error: no match for 'operator+=' in 'total += ptr->Node<Book>::info'| occurred. How can I access the price in the node and then calculate the total price.
Here is part of my code :
Book class
class Book {
protected :
int id;
string title;
double price;
string bookStatus;
[code]....
View 2 Replies
View Related
Nov 11, 2013
I have been trying to implement a way to remove a post from a list of posts implemented with a template doubly linked list. I have thought that the best way to do this might by operator overloading, but I have digressed. I have just thought of using a isEqual that checks equality, but when trying to implement i'm getting weird errors.
This is within my class wall, which is a linked list of wall posts, getPostInfo is within the class WallPost.
bool isEqual(WallPost const & a, WallPost const & b)
{
if(a.getPostInfo() == b.getPostInfo())
return true;
else
return false;
}
I have several instances of the error "void illegal with all types" on line 3. It also is complaining about a not being a arithmetic, unscoped enum, or pointer type. I am assuming that it is because my getPostInfo function is a void.
View 16 Replies
View Related
Feb 23, 2013
I am trying to write a function to return the first element of a link list queue. I am not real sure how to implement this. I have include a copy of the struct for my Node & queue.
Code:
typedef struct event_Node {
void *data;
double arri_time;
double serv_time;
double depart_time;
double start_o_serv;
[Code]...
View 4 Replies
View Related
Sep 7, 2014
I tried to modify staff name with an assignment value of other string but couldn't work it out. I managed to modify it by key in cin >>.
The purpose is that I want to assign string value from other different class to Staff class. An error : no match for 'operator=' in '* updateName = newStaffName' and note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const Staff&' occurred.
#include <iostream>
using namespace std;
class Staff {
friend ostream& operator << (ostream& os, Staff& m) {
os << "[" << m.ID << ", " << m.fName << "]";
[Code]...
View 3 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
Feb 17, 2013
I'm trying to make a function that lets me pass an index and a string and will insert the string at that index in the linkedlist..
Code:
typedef struct node {
char* value;
struct node* next;
}
[code].....
View 4 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
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
Dec 2, 2014
Should my if statement be
if (tail== NULL) or if(tail->next == tail) ?
View 4 Replies
View Related
Sep 22, 2014
I am currently trying to add to a linked list in sorted order but I have reached an impasse. I can get it to add in sorted order if it belongs in the beginning or second in the list. If i were to type in 9 then 4 i would get 49, but if i type in 5 it changes it to 559. I'm just at a loss and need some sort of direction.
#include "singly_linked_list.h"
#include <iostream>
using namespace std;
void add_node(node*& head_ptr, const int& payload){
if (head_ptr == nullptr) {
node* my_node = new node();
my_node->data = payload;
[Code]...
View 2 Replies
View Related
Oct 31, 2013
I have a .cpp file which contains 5 smaller defined classes. In my missile class I have a default constructor and a constructor that I invoke
class Missile{
private:
bool isHuman;
[Code]...
My issue is when creating and adding the pointer object; it doesn't seem to create a new instance of the class-the Missile objects all share the same xPos value which is always the first xPos when the "fire" command is given. The "Missile *missile = new Missile(xPos, yPos, true);" line does not seem to create a new instance of the object with different variables but instead creates a new object with the same variables. Is it necessary for me to always make a separate .cpp and .h file for any class I want to create multiple instances of or can I keep the smaller classes in the same file and still create a new separate instance of the class?
View 3 Replies
View Related
Oct 9, 2014
can we pass two linked lists as argument in a member function to add two linked lists? and how to access them both in the function??
View 1 Replies
View Related
Mar 3, 2013
I am having a bit of difficulty with implementing an object oriented program that uses both linked lists and operator overloading. The program calls for adding and multiplying polynomials together, with each single polynomial being represented as a node of a linked list (which is further a data member of an object of a class I have defined to implement this program). For example:
polynomial A will be: 3x^4 // 1 node of a linked list
polynomial B will be: 5x^2 // 1 node of a linked list
polynomial C will be blank for the time being. // empty list
Now, I need to use operator overloading so that this following line of code can be implemented:
C = A + B;
C should now be: 3x^4 + 5x^2.
The checklist of which parts of my code that work:
constructor works;
copy constructor works;
destructor works;
operator= works;
print function needs work but i can worry about that later;
operator* work on later
Here is my code:
#include <iostream>
using namespace std;
struct termNode {
int exp; // exponent
int coef; // coefficient
termNode * next;
[Code] ....
For the time being I need to add multiple nodes together (with the result being in descending order). So for example:
polyType a(2,3), b(4,5), c(6,7), d;
d = a + b + c;
d.print(); // should print out 7x^6 + 5x^4 + 3x^2, but it will only print out: 3x^2 + 7x^6
View 5 Replies
View Related
Feb 23, 2013
I have a global list that contains smaller lists of char arrays. I have an issue where when I'm reading back the inner lists the last element of one list seems to point to first element of the next.
So my data looks like the below (values separated by commas with the pairs separated by tabs. The last pair in a line is the same as the first). When I read the first list back instead of seeing "456.678,678.98" as the last element in the list. I see "435.67,234.98" twice: at the end of the first list and start of the other. I have debugged when the list is populated and can see the correct values going in so I can't figure what's happening.
456.678,678.98 123.45,345.56 256.67,789.98 456.678,678.98
435.67,234.98 123.65,342.56 987.78,678.34 435.67,234.98
Code to fill the list:
obstacle_list = op_prg_list_create();
while (fgets(line, sizeof(line), obstaclePositions_traj_file) ) {
token = strtok(line, "
"); //Pull the string apart into tokens using the
input = op_prg_list_create();
[Code] ....
View 3 Replies
View Related
Aug 23, 2012
I have an std list of type double.. and the list is always guaranteed to have just 2 elements. I need to get the value of element 2 minus element 1. What is the least amount of code to accomplish that?
I tried this:
Code:
list<double> dList;
dList.push_back(1.0);
dList.push_back(2.0);
list<double>::iterator iter = dList.begin();
list<double>::iterator iter2 = dList.end();
double result = *iter2 - *iter;
But this code does not work. Why not?
View 6 Replies
View Related
May 30, 2013
I'm working on a linked list and was wondering how this looks to everybody else for a deleteList function.
void deleteList(Node* head)
{
Node* iterator = head;
while (iterator != 0)
[code].....
View 6 Replies
View Related
Jun 29, 2013
I have a linked list comprised of chars like so...
Code:
node1 - "p"
node2 - "o"
node3 - "p"
I need a function that will take in three perameters...node *replaceChar(node *head, char key, char *str)Stipulations of this function. head is the head of the list, 'key' and 'str' are guaranteed to contain alphanumeric characters only (A-Z, a-z, and 0-9). str can range from 1 to 1023 characters (inclusively). So if I call this function with these perameters..
Code:
node *head == /*the head of the list to be examined*/
char key == "p"char *str == "dog"The new list will look like this...
node1 - 'd'
node2 - 'o'
node3 - 'g'
node4 - 'o'
node5 - 'd'
node6 - 'o'
node7 - 'g'
All instances of 'p' were replaced with 'dog' I have a toString function which takes in a string and converts it to a linked list and returns the head. So assume that you can call the function on str = "dog" so...
Code:
toString(str) == /*this will return the head to the list made from the str*/
If it's unclear what my question is...I am stumped on how to write the replaceChar function the one that takes in three perameters..
View 3 Replies
View Related
Mar 27, 2013
How can I remove an element in a list when I have only an iterator that points to the object I want to remove. Is there a build in command? remove() takes an object reference as its argument. Is it possible to convert the iterator into a pointer type so it can be deferenced and passed to remove?
This is the code I am working on:
//player.cpp
void Player::CheckCollectableCollisions(std::list<Collectable>& c) {
std::list<Collectable>::iterator i = c.begin();
while(i != c.end()) {
if (Collider::CheckCollision(pNodes_.front().getLocation(), i->getLocation()))
[Code] .....
View 2 Replies
View Related
Nov 26, 2014
Here's my list code:
Color.FromArgb(alfa, 0, 255, 255),
Color.FromArgb(alfa, 0, 255, 0),
Color.FromArgb(alfa, 64, 255, 0),
Color.FromArgb(alfa, 128, 255, 0),
Color.FromArgb(alfa, 192, 255, 0),
Color.FromArgb(alfa, 255, 255, 0),
Color.FromArgb(alfa, 255, 192, 0),
Color.FromArgb(alfa, 255, 128, 0),
Color.FromArgb(alfa, 255, 64, 0),
Color.FromArgb(alfa, 255, 0, 0)
How can I get a specific element value from that list?
For example:
if(true)
{
//get "192" from Color.FromArgb(alfa, 255, 192, 0)
}
I don't see the "Edit" option and I've noticed a slight error in the previous post.There should be, obviously, [code]if(true)
{
//get "192" from Color.FromArgb(alfa, 255, 192, 0)
}
I was missing the closing tag.
View 2 Replies
View Related
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