C++ :: Deleting A Linked List?

Mar 27, 2014

If I have a linked list, how can I delete it at the end of my program to free the memory taken up. I know I have to use delete but not sure how to use it in this case.

Here is the code that I have:

#include <iostream>
#include <cstdlib>
using namespace std;
struct Numlist {
int num; //Number
Numlist * next; //Pointer to the next node

[code]....

View 4 Replies


ADVERTISEMENT

C :: Deleting A Node From Linked List

Apr 19, 2013

I have a struct with some select student information. My wish is to be able to have the user type in a surname from the list, and for that entry to be deleted. However I am slipping up for some reason.

Here is the start of my program showing my struct:

Code:
#include <stdio.h>
#include <string.h>
#define NAME_LEN 30
#define LINE_LEN 80
struct record {

[Code] ....

I get the following errors in my while loop as well as the if statement below that loop:

-invalid operands to binary !=
-invalid type of argument '->'

View 7 Replies View Related

C++ ::  linked List - Deleting A Node

Mar 2, 2013

If p is a pointer pointing a node to be deleted, then what's wrong in the following code:

cout << "Do you want to delete this record? (y/n) ";
if (getch() == 'y' || getch() == 'Y'){// Delete record.
if (p == *ph){//If first node is to be deleted.
*ph = (*ph)->next;
delete p;

[Code] .....

View 2 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++ :: Deleting Middle Node In Linked List

May 1, 2013

I am trying to delete a node from the end of a Linked List but I have some problems. Every node has it's unique code. Here is what I do:

1. Ask the user for the unique code of the node.
2. Ask him if he wants to change the data in it or delete the whole node.
3. If he chooses to delete it, I do this:

//let's say that temp1 points to the node
List *temp2 = temp1;
temp1 = temp1->next;
delete temp2;

And it's just not deleting the node.

View 2 Replies View Related

C++ :: Deleting Head Node In Linked List

Sep 8, 2014

I would like my function to return the data that was deleted. I have this code at the moment, where value_type is just an alias for int at the moment:

node::value_type linkedlist::removeFromHead(){
node* temp_head;
node::value_type data;
data = head_ptr->getData();

[Code] .....

When I try to use this function though, I get a segfault.

View 19 Replies View Related

C++ :: Deleting Linked List Nodes Recursively

Jun 25, 2014

I am creating a Linear linked list in c++. I've written all the functions for it, but now I want to try and do them using recursion.

I managed to make functions for adding and displaying nodes on the list, but I am stuck on the function for removing nodes. First I want to try and write a function the removes the last item on the list. But for some reason my code isn't working properly.

Here is the code :

void removeLastItem(node * &head) {
if(!head) {
delete head;
head = NULL;

[Code] ....

NODE - My structure name
NEXT - The pointer to next element.
HEAD - The first (head) pointer.

The couts in the if statements are just for testing. In fact after I Run my program it does as it is supposed - enters the second if /b]case as many times as there are elements and then executes the first [b]if statement. But for some reason it does not delete the actual node.

View 2 Replies View Related

C# :: Deleting LIST From CSV?

Mar 7, 2014

Basically, I have a LIST that is saved in a CSV file, and I need to remove a particular record after using the search feature (which populates the fields from the CSV file... i.e. ID, Name, etc...).

I am able to get the details that are in the text fields by using a search feature. This is then put in:

logic.remove(tempIndividual)

In class myLogic.cs, then I have this method:

this.individual.Remove(tempIndividual)

Upon debugging, I can see that tempIndividual is populated with the correct data. However, I need to remove this data from the CSV file. Am I on the right track, or totally off? Maybe it is not complete, cause I can't get to the part where it actually removes the data from the CSV, or at least I'm not sure if .Remove is able to do it on its own.

View 9 Replies View Related

C++ :: Adding And Deleting From List

Dec 11, 2014

I am writing a program to list the 8 planets, then you select which planet you want. it gives you the mass of the planet, the radius, then you use the mass and radius of the planet to find the surface area (sphere formula) and the density of the planet. Along with those options, you have to add and delete a planet from the list and then sort them alphabetically. All i am having trouble with is the adding and deleting part. The code the adding and deleting from the list would go in cases 9 and 10.

heres my code as of this point.

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
void print(const std::string& item) {

[Code] ....

View 5 Replies View Related

C++ :: Deleting Node From End Of A List

May 5, 2013

I'M TRYING TO DELETE FROM THE END OF A LINKLIST...Actually I delete the last node but the problem is that I lost the end of the list, in other words my list leaved of pointing to NULL.I really don't know how to fixed, (it was more easy to delete from head)

Code :

void linkedstack::pop() {
if(head==NULL){
cout<<"The linklist is empty"<<endl;
return;}
else {
node *current=head;

[code]....

View 3 Replies View Related

C++ :: List Pointer Not Deleting?

May 30, 2013

LinkedList* listPtr = new LinkedList;

listPtr->addNode(3);
listPtr->addNode(4);
listPtr->addNode(6);
listPtr->print(std::cout);

delete listPtr;

listPtr->addNode(5);
listPtr->print(std::cout);

When I run the program it outputs
{ 3, 4, 6 }
{ 5 }

Why does the LinkedList object that listPtr is pointing to not get destroyed??

View 8 Replies View Related

C/C++ :: Adding And Deleting From List

Dec 11, 2014

i am writing a program to list the 8 planets, then you select which planet you want. it gives you the mass of the planet, the radius, then you use the mass and radius of the planet to find the surface area(sphere formula) and the density of the planet. along with those options, you have to add and delete a planet from the list and then sort them alphabetically. All i am having trouble with is the adding and deleting part. the code the adding and deleting from the list would go in cases 9 and 10.

heres my code as of this point.

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
void print(const std::string& item) {

[Code] ....

View 3 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 :: Insert Linked List Into Another Linked List

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

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

C++ :: Linked List Delete List?

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

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

May 7, 2013

I read an article on linked list here: C Linked List Data Structure Explained with an Example C Program

Code:

struct test_struct* search_in_list(int val, struct test_struct **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;
}

[code].....

What is "if(prev)"? Wouldn't "prev" always have the same value? Secondly, if tmp is NULL (which will be the case when the loop if(ptr->val == val) finds a match the first time it is run), is *prev assigned a NULL?

View 1 Replies View Related

C :: Double Linked List

Jan 2, 2014

I'm having a small issue here with my linked list.I built a linked list with strings and it worked perfectly.Now since i'm using strtok() to separate the string.for now here's what i've got:

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct dict_word *word;
typedef struct node *Node;
typedef struct double_linked_list *DLL;
}

[code]....

View 1 Replies View Related

C :: Static Linked List

Jan 6, 2014

I have the codes for the functions: "Given the functions that we have discussed and defined in the class and the code that we have created, create the code for the Delete Node function and the Search function. Create an application that will incorporate all the functions that we have discussed and the new ones that you need to create. The application should allow the linked list to be built initially from a text file. The application should then allow for the user select an operation to perform. The acceptable operations are

- to search for an item
- to delete an item
- to enter a new item
- to exit the application

After the initial build and loading of data from the textfile, the application should print a listing of each item in the list and the node that it is located in. after a delete or insert, the application should display an output of the list again showing each item in the list and the node that it resides in.

The data item for this problem will be the inventory structure that we created in class and the data is in the inventory file that you already have downloaded."

View 4 Replies View Related

C :: Singly Linked List

Mar 7, 2013

I am having difficulty getting my program to run. I have looked and looked many times trying to figure out what I am doing wrong. I have a hunch that it's my load function that's acting up, but from my perspective, my creation of my singly linked list looks good. From the creation of my singly linked list, does anything stand out as being abnormal?

Code:

typedef struct Node {
char word[LENGTH+1];
struct Node *Next;
} Node;
}

[code]....

View 6 Replies View Related

C :: Sorted Linked List

Apr 2, 2013

I been ask to create a sorted linked list module, and to test each function with 1 unit, 2 +.so i wrote my addtofront function( we have to use the same as the teacher)

Code:

struct MyList{
char * name;
Sorted * AddmyChar;
struct MyList * next;
}

[code]....

but i cant seem to test it because it always display a seg fault.... i dont know what i did wrong....how do i call this function in main to display a string i want?

View 9 Replies View Related

C++ :: Linked List Using Templates

May 27, 2014

I decided to make a linked list program using classes and templates.I implemented my linked list and added some basic functions, everything worked just fine. Then I proceeded to add templates...First, the error list:

#pragma once
template<class T>
class Node {
private:
Node* next;

[code]....

To sum up: Something's wrong with the templates that I used in "List.cpp" and what it could be.

View 5 Replies View Related

C++ :: Linked List Won't Take Big Numbers

Jan 2, 2013

My program is almost done all that is left is entering big numbers, the program can add and subtract small numbers but not big numbers because it puts them all in one node, I need the program to store each number in a node.

#include<iostream>
#include<string>
using namespace std;
class Node {
public:
int value;
Node * next;

[Code] .....

View 11 Replies View Related

C++ :: Traversing A Linked List

Mar 30, 2013

When I am traversing a linked list ...i suppose we can do it only with pointers

like p is a head node pointer then

p
p->next
p->next->next
p-next->next->next

so on represents a link...

if i have to pass head node as struct variable itself...can i traverse a link using dot operator??

p and p.next and how to retrieve third and remaining links through this method if we can traverse like this???

View 1 Replies View Related

C++ :: Merging Two Linked List Into One

Mar 3, 2014

I have created this program to merge two linked list into one ,but however everything is working fine but on execution it providing a segmentation fault error ( on calling merging function )

code :

#include<iostream>
#include<stdio.h>
using namespace std;
struct node{
int data;
node* link;

[code].....

View 1 Replies View Related







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