C++ :: Writing Delete Algorithm In Ordered Linked List

Oct 19, 2013

I am trying to write a delete algorithm for an ordered linked list. Search and traverse I think I have down but delete is giving me fits. It crashes every time.

I have a private pointer to a ListNode strcuture called head in my TestLL class. The ListNode structure contains int Key, double dataValue, and ListNode *next. The function returns true if the node to delete was found and deleted or false if it was not found.

Code:
bool TestLL::Delete(int Key) {
ListNode *back = NULL, *temp = head;
//Search for the node to delete
while((temp != NULL) && (key != temp -> key))

[Code] .....

View 4 Replies


ADVERTISEMENT

C++ :: Linked List Sorting Algorithm?

May 31, 2013

This is the algorithm I have so far and it works great. I was wondering what other people think? Comments/Critiques??

void LinkedList::sort()
{
if (head != 0)
{

[Code].....

View 14 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++ :: Should Delete Every Node In Linked List?

Jul 10, 2014

Still toying with my self-coded linked list class and now another question: should I delete each node of my class with the delete in the class destructor or is it done automatically when the main() function ends?

View 4 Replies View Related

C/C++ :: Delete Value At Nth Position In Linked List

Sep 30, 2014

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct ddpl{
string author, title, show_title, choice, user_name, borrowed_book_title, borrowed_book_author;

[Code] ....

I don't know how to do this if this was deleting by position would have been easier, but the user don't know and don't have to know the position of every book in the list it should delete the books by there title and i'm kinda lost of the delete function at line 47 and particularly at line 55 and 62, it kind difficult to traverse with the values or the data of the node for the position is simpler.

View 14 Replies View Related

C++ :: How To Delete All Values In The Linked List

Feb 15, 2012

I have made this program for linked list, but i have one problem with it. I want to make a function that will delete all same values I inuput in the list.

Example: if user inputs 5 I want to delete all 5 for the list. I have tried doing this with the loop inside regular function that erase single value.

This is function that erases single value:

Code:
void list::Delete (int a) {
node *temp=head;
if (temp==NULL)
return ;
if (temp->getNext()==NULL) {
delete temp;

[code]....

View 3 Replies View Related

C++ :: Shortest Path Algorithm Through Sudo-linked List

Feb 9, 2013

I am needing to code a shorted path algorithm through a sort of sudo-linked list. I say sudo because its not a true linked list as in *next *prev. The Data in memory that i need to do this through is in memory via a class.

The important parts of the class is one element is just an int that tells me the current node number, the second is an int that tells me how many neighbors this node has, and the third is a vector of ints containing the number of the neighboring nodes.

The vector was needed because each node type can have a different amount of neighbors.

For example if the class data looks like this:

0 1 1

that means that it is node 0, it has 1 neighbor, and the neighboring node is 1

another example:

8 3 1 2 3

means node 8, 3 neighbors, and the neighbors are nodes 1 2 3

I need to find a way to get from 1 node to another using this linked list, and the shortest path if we plan the data points well enough can be determined by how many nodes there are from start to finish.

View 2 Replies View Related

C/C++ :: Dijkstra Algorithm - Reading In Data To A Linked List

Mar 20, 2014

Program to implement Dijkstra's Algorithm. Have Problems storing graph info into linked-list . The input is in the following format

5
1 2 9.0
1 3 12.0
2 4 18.0
2 3 6.0
2 5 20.0
3 5 15.0
0
1 5

The first number is the number of vertexes in the graph. Then next lines up to 0 are the edges of the graph. With the first and second numbers being the vertexes and the third being how far the edge is between them. Trying to read in the data and store the edges into there locations in the List adjacency for that vertex. This example would make a graph with five vertexes with edges from 1 to 2&3. 2 to 4&3&1 etc. It also stores in the opposites ex 2 1 9.0.

When reading it in and printing it back out it seems that it is only storing the last data entered for that vertex. EX. When trying to print out the data read in i only get 1 3 12.0, 2 5 20.0, 3 5 15.0, ( 4 2 18.0, 5 3 15.0 would show up if `if(i<n)` was removed it is there so it does not show duplicate edges).

#include <cstdio>
using namespace std;
struct ListCell {
ListCell* next;
int vertex;
double weight;
ListCell(int v, double w, ListCell* nxt)

[Code] ....

View 5 Replies View Related

C :: Delete Function In Singly Linked List

Jan 20, 2014

Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node {
int data;
struct node *next;

[Code] ....

The above code is a SLL. I have a problem in my deleteNode function. I can't delete the exact data on the list. How to do this right?

View 1 Replies View Related

C :: Delete A Node From Singly Linked List

Oct 18, 2014

I was trying to write a function in C to delete a node(only from the middle) from a Singly Linked List. I wrote one but not sure if the code will work fine under all test conditions. I have tested it and shows no error so far.

Code:
void deleteAt(struct node *root, int number){
while(root->link != NULL) {
if(root->link->item == number) //checks if the next node is the element to be deleted {
root->link = root->link->link; //points the link of the element to be deleted to the element before the element to be deleted
}
else
root = root->link;
} }

Or is there a better way to do this?

View 3 Replies View Related

C++ :: Process To Delete Everything From Single Linked List

Aug 23, 2013

what is the process to delete everything from a singly linked list. Like S= 1->2->3->... I want to remove all the values from S and reuse it again to store new datas.

View 2 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++ :: Singly Linked List - Delete Duplicate Nodes

Oct 10, 2014

I don't know why my code is not working. It's from line 249 - 266

void SLL::deleteDuplicate(){
Node* temp = head;
Node* p = temp;

[Code].....

View 1 Replies View Related

C/C++ :: Unable To Delete A Node In Double Linked List

Aug 22, 2014

I am trying this without a head/start pointer which would generally hold the address of first node. I have 3 nodes here from which I am trying to delete last node, but its not happening. I might be wrong in my logic and this is my first linked list program.

#include <stdio.h>
#include <stdlib.h>  
struct dll{
            struct dll *prev;
            int data;
            struct dll *next;

[Code] ....

output::::

add of p1::0x9605008 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::(nil) add of p1->next::0x9605018 add of p2->prev::0x9605008 add of p2->next::0x9605028 add of p3->prev::0x9605018 add of p3->next::(nil)

no of nodes 3

enter the addresss of node to delete it
0x9605028

after deletion attempted

add of p1::0x9605028 add of p2::0x9605018 add of p3::0x9605028 add of p1->prev::0x9605018 add of p1->next::(nil) add of p2->prev::0x9605008 add of p2->next::0x9605028 add of p3->prev::0x9605018 add of p3->next::(nil)

no of nodes 3

In this example i am trying to delete the node 3 which is p3, by deleting its address 0x9605028. But after deletion the node count is still 3 and addresses are like unexpected!

View 1 Replies View Related

C++ :: Searching An Ordered List

Oct 3, 2013

Make an ordered array. Put a bunch of random numbers into it. Do 20 linear searches and 20 binary searches of that array. (Code for linear search and binary search are found in the book.)Print out how many elements were checked before the item is found

I'm not looking for answers as it is homework. However I don't understand what I'm doing. This course is being held online(no other options) and that's not how I learn so I'm struggling. From my understanding I have to make a templated ordered array (which I don't understand at all) and then do searches (which I also don't understand).

View 1 Replies View Related

C++ :: Doubly Linked List That Will Store Strings - Delete Function

Sep 23, 2014

I am creating a doubly linked list that will store strings, for example:

struct node{
string data;
node* next;
node* prev;
};

node* head; //first element from left to right (global)
node* tail; // last element from left to right (global)

And here is the function:

void deleteNode(int n){
struct node* temp1 = head;
if (n == 1){
head = temp1->next;
free(temp1);

[Code] .....

View 3 Replies View Related

C/C++ :: Linked List Delete Function Freezing Program When Called

Aug 12, 2014

My program seems to be working fine, except for when I call on my delete function. I don't receive any errors, but when I call the delete function my program outputs nothing and freezes. As in, my print function (which is called before the delete function) doesn't even work. I've tried removing bits of the function to see if I could pinpoint where exactly the issue is, but I've had no luck.

#include <iostream>
#include <string>
using namespace std;
class List{
private:
struct node{
string _data;

[Code] ....

And the function that is causing me trouble:

void deleteNode(string data){
node* del = NULL;
t = h;
n = h;
while(n != NULL && n->_data != data){

[Code] .....

View 5 Replies View Related

C++ :: Can't Get Specifically Ordered List To Print Properly

Dec 21, 2014

I'm writing a program where the user inputs a number of rows, and that many rows of @ symbols are printed, where the first row has the same number of columns as rows, and each next row decrements the number of columns by 1. It prints out a totally wrong output and whatever I try it won't print the right thing- currently when I input 4 it prints out 5 @ symbols on the first row, and then 3 on all the other rows.

Code:

#include <iostream>
using namespace std;
int main ( ) {
int rows;
cout << "How many rows? ";
cin >> rows;

[Code] .....

View 2 Replies View Related

C :: List - Why Delete Function Doesn't Delete

Dec 9, 2014

I have to manage a Clinic. I need to delete a booking (for example, if John said he's coming on March 22nd at 15:30 but then he say he's not going to come, I have to delete that booking so another person can use it).

idSearched: the id of the person that is not going to come. I have a lot of specialties and each one has a list. So I ask for the speciality to delete the node (the node contains John's booking). If I don't find it, I return (-1). searchSpecByID return a pointer to the list where the speciality is. So head will point to the first node of the list. In nodeToDelete I have the node I want to delete.

The program detects OK when is the first in the list and when not, but it doesn't delete the node.

Code:

typedef struct bookingList{
tSpecialty specialty;
struct nodo* intro;
} tList;

[Code].....

View 7 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 :: Selection Sort Algorithm For Singly Linked Lists

Feb 24, 2013

I've written code for a selection sort algorithm to sort through a singly linked list, However, it only brings the smallest node to the front, but doesnt swap positions of any of the remaining nodes, even though they are not in order.

Input list: 3 6 17 15 13 15 6 12 9 1 2 7 10 19 3 6 0 6 12 16

Sorted list: 0 6 17 15 13 15 6 12 9 1 2 7 10 19 3 6 3 6 12 16

Code:

#include<stdio.h>
#include<stdlib.h>
struct node{
int val;
struct node *next;

[Code] ......

View 1 Replies View Related

C++ :: Add / Delete And Locate Node Via Singly Linked Lists

Jul 7, 2014

I need some basic code (not for an assignment) to write an add, delete and locate an node via the singly linked list.

I've looked at: [URL] .....

View 1 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++ :: Efficient Algorithm On List Iterator

Mar 6, 2013

I have a list and a vector, for example like

list<int> mylist = {1, 1, 1, 0, 0, 1, 0, 0}
vector<int> myvec;

I want to create an efficient algorithm to erase all element of value "1", and give these elements to a vector. My code (not efficient) is as follows

for(auto it = mylist.begin(); it != mylist.end(); it++) {
if(*it == 1) {
myvec.push_back(*it);
it = mylist.erase(it);
if(it != mylist.begin())
it--;
}
}

View 8 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++ :: List Of Latitude And Longitude Coordinates - Sorting Algorithm

Jul 4, 2012

I have a list of latitude and longitude coordinates which are supposed to trace the outline of a city. Somehow they got scrambled so that they are now out of order. I'd like to write a program to arrange them such that one could follow from one coordinate to the next and trace the perimeter. However, I've run out of ideas for algorithms. What I did so far was a simple search for the nearest coordinate, starting from the first coordinate pair in the array. This produced local regions which worked rather well, but globally there were large jumps as the algorithm ran out of nearby coordinates and was forced to jump across the map.

Is there already a developed algorithm to perform this function?

View 1 Replies View Related







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