Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
C++
#include<iostream>
#include<string>
using namespace std;

struct node
{
string name;
node* next;
};

void print(node* head)     //print linked lists
{
for (;head; head = head->next)
 {
	cout << head->name << endl;
 }
}

node* addnode(node* head)     //add linked list at the first 
{
node* 1NODE = new node;
1NODE->next = NULL;
cout << "name" << endl;
cin >> 1NODE->name;
name->next = head;
head = 1NODE;
return head;
}

int delete_node( node* head,node* pointer)   //delete linked lists that user input 
{
node* NEXT = head;
pointer = head;
return -1;
while (pointer->next != NULL)
{
	pointer = pointer->next;
	if (1NODE->name == pointer->name)
	{
	delete(1NODE);
	}
}

if (pointer == head)
{
	node* pointer = head;
	head = head->next;
	delete(pointer);
}
if (pointer->next == NULL)
{
	delete(pointer->next);
	pointer->next = NULL;
}
else
{
	node* n = pointer->next;
	pointer->next = 1NODE->next;
	delete(pointer);
}

return -1;
}
int main()

{

node* head = NULL;
node* 1NODE=NULL;
string name;
unsigned short numbers = 0;
cin >> numbers;
cout << endl;
for (unsigned short i = 0; i < numbers; i++)
{
	head = addnode(head);

	cout << endl;
}

cout << "LIST : " << endl;
cout << endl;
cin >>1NODE->name;   //here is the user input to delete node but it won't run
delete_node(head,1NODE);

print(head);

return 0;
}


What I have tried:

I wrote a program that print and save some information in linked lists and then it can delete (d function in this code)and add(add function
Posted
Updated 9-Dec-20 13:14pm
v4
Comments
BabyYoda 7-Dec-20 14:11pm    
What do you want us to do with all of this code? Guess what it is supposed to do? And then fix it?

No thank you. Next?
yasiagh 7-Dec-20 14:16pm    
I wrote a program that print and save some information in linked lists and then it can delete (d function in this code)and add(add function)
BabyYoda 7-Dec-20 14:30pm    
OK. It sounds like all you have to do is debug your code and you'll find the issue pretty quickly.
yasiagh 7-Dec-20 14:53pm    
I edited that,now it is easier to read
BabyYoda 7-Dec-20 15:06pm    
But we still do not know what the problem is. You said it does not work. Why not? We don't know what it is supposed to do and what it is not doing.

Read your code: look at your delete function, it does not do anything.
C++
int delete_node( node* head,node* pointer)   //delete linked lists that user input 
{
node* NEXT = head;
pointer = head;
return -1;
// any following code will now be ignored.
 
Share this answer
 
I'm not going to even try to work out what is wrong with that code: it's just too much trouble of even start reading it, much less work out what parts of it are and aren't relevant.
If you want help, then make it easy for us to help you.
1) Don't dump you whole code on us and expect us to pick out the relevant bits. Show us just the bits that are invloved.
2) INDENT YOUR CODE. When I see stuff like this:
C++
void print(node* head)

{

for (; head; head = head->next)

{

	cout << head->e << endl;

}
}
I lose any interest I might have had in wading through code. pick an indentation style and apply it to you whole code to make it a lot easier for us to read, and don't double space it to make it look bigger or more important - again it makes it harder to read:
C++
void print(node* head)
   {
   for (; head; head = head->next)
      {
      cout << head->e << endl;
      }
   }

3) Stop using one letter names - they make it far to easy to make a mistake, and they make code - yet again - harder to read. Use meaningful names that describe concisely what the variable or function does, not "d", "t", "e" ...
4) Saying "it doesn't work" is completely pointless - it tells us absolutely nothing because we have no idea what it is supposed to do, what you did to get it to go wrong, let alone what you have noticed that might be wrong with it.

In essence, help us to help you!
 
Share this answer
 
Unless you are paid with number of lines in source code, skipping lines like you do offer no interest.
Scattering comments in code is much more useful in the long term.
C++
int d( node* head,node* loc)

{

node\* n = head;

loc = head;




return -1; // one can suspect this line to prevent any thing from happening further on code


while (loc->next != NULL)

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
-----
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include <iostream>
#include <string>
using namespace std;

struct node
{
    string e;
    node* next;
};

void print(node* head)
{
    for (; head; head = head->next)
    {
        cout << head->e << endl;
    }
}

node* add(node* head)
{
    node\* n = new node;
    n->next = NULL;
    cout << "e" << endl;
    cin >> n->e;
    n->next = head;
    head = n;
    return head;
}

int d( node* head,node* loc)
{
    node\* n = head;
    loc = head;
    return -1;
    while (loc->next != NULL)
    {
        loc = loc->next;
        if (n->e == loc->e)
        {
            delete(n);
        }
    }
    if (loc == head)
    {
        node\* loc = head;
        head = head->next;
        delete(loc);
    }
    if (loc->next == NULL)
    {
        delete(loc->next);
        loc->next = NULL;
    }
    else
    {
        node\* n = loc->next;
        loc->next = n->next;
        delete(loc);
    }
    return -1;
}

int main()
{
    node\* head = NULL;
    node\* n=NULL;
    string e;
    unsigned short t = 0;
    cin >> t;
    cout << endl;
    for (unsigned short i = 0; i < t; i++)
    {
        head = add(head);
        cout << endl;
    }
    cout << "LIST : " << endl;
    cout << endl;
    cin >>n->e;
    d(head,n);
    print(head);
    return 0;
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
Enabling Open Innovation & Collaboration | The Eclipse Foundation[^]
 
Share this answer
 
v2
Other hints:

1) Turn your compiler warnings to a sensible maximum and set warnings as errors (in the case of VS thats /W4 /WX) and pay attention to the warnings. Warnings often indicate bugs in waiting.

2) Use a static analyser, that will also help you avoid writing bugs (VS's inbuilt /analyze or Gimpel's PC-Lint/PC-Lint Plus or Clang Tidy or even CppCheck are all great).

In short, look beyond the specifics of this problem as well, at what you can use:
- Compiler warnings
- Static analysers
- Debuggers
Use all the tools you have available (paying attention to warnings and using static analysis will help you become a better developer - you can unlearn bad habits by paying attention to them).
 
Share this answer
 
Hello guys,

I researched the code shared by this guy on the web and I think I found the original at the link down below. He took the code on this link, cut it and slaughtered it quite a lot. Then he couldn't get out of it :)

C++ Tutorial: Quiz - Linked List - 2020[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900