- When i'm trying to delete the first number of list, something went wrong when i print the list?
- Can anyone help me? Thanks so much T_T.
Here my code:
include <stdio.h>include <conio.h>include <stdlib.h>struct node {
int inf;
node *next;
};node *insert_head(node *first, int data);
node *insert_last(node *first, int data);
int is_empty(node *first);
int find_max(node *first);
int counting_ele(node *first);
int print_list(node *first);
node *detele(node *, int dat);int main(){
node *head = NULL;
char choose;
while(1){
system("cls");
printf("\t Menu\n 1.Insert to head\n 2.Insert to last\n 3.Number of elements\n 4.Delete\n 5.Print list\n 0.Exit!");
printf("\n Your choose (1-6): "); scanf("%c", &choose);switch(choose){ case '1': system("cls"); int value; printf("-> Enter value you want to add to head of list: "); scanf("%d", &value); head = insert_head(head, value); printf("\nSuccessfull, press any key to return menu!"); getch(); break; case '2': system("cls"); int value1; printf("-> Enter value you want to add to last of list: "); scanf("%d", &value1); head = insert_last(head, value1); printf("\nSuccessfull, press any key to return menu!"); getch(); break; case '3': system("cls"); if (is_empty(head)) printf("->List is empty!"); else printf("->List has %d elements!", counting_ele(head)); getch(); break; case '4': system("cls"); printf("Enter number: "); int num; scanf("%d", num); //std::cout << num; detele(head, num); printf("\nSuccessfull, press any key to return menu!"); getch(); break; case '5': system("cls"); printf("-> Printing all elements: "); printf("\nResult: "); if (!print_list(head)) printf("List is empty!"); getch(); break; case '0': return 1; default: break; } }
}
node *insert_head(node *first, int data){
node *tmp;
tmp = new node;
tmp->inf = data;if (!first){ first = tmp; tmp->next = 0; } else { tmp->next = first; first = tmp; } return first;
}
node *insert_last(node *first, int data){
node *tmp, *run;
tmp = new node;
tmp->inf = data;
if(!first) {
tmp->next = 0;
first = tmp;
}
else {
run = first;
while(run->next) {
run = run->next;
}
run->next = tmp;
tmp->next = 0;
}
return first;
}int is_empty(node *first){
return !first;
}int find_max(node *first){
node *run = first;
int max = 0;
while (run){
if (run->inf > max) max = run->inf;
run = run->next;
}
return max;
}node *detele(node *head, int dat){
node *tmp;
if (head->inf == dat){
tmp = head;
head = head->next;
delete tmp;
} else {
node *run = head;
while(run->next && run->next->inf != dat){
run = run->next;
}
if(!run->next){
printf("Can't not find!");
return head;
} else {
tmp = run->next;
run->next = tmp->next;
delete tmp;
}
}
return head;
}int counting_ele(node *first){
node *run;
int count = 0;
run = first;
while(run){
count++;
run = run->next;
}
return count;
}int print_list(node *first){
node *run;
if (is_empty(first)) return 0;
else {
run = first;
while(run){
printf("%d ", run->inf);
run = run->next;
}
return 1;// run = first;
// if(run) print_list(run->next);
// printf("%d ", run->inf);
// return 1;
}
}
something went wrong
That's not very helpful for anyone out here trying to help you.
Could you please be a little more specific?
- hhm, it can't be deleted and changed by another one.
- You can run it bt DEVC++ orwell 5.6.3, and check it.
- Someone can edit my code, it look trouble :3 :3.
- Thanks
A few things to note before proceeding:
- In modern C++, the headers for the standard library eliminate the
'.h'
extension, and the legacy C headers all begin with the letter 'c'. So the headers should now be<cstdio>
and<cstdlib>
. - The
<conio.h>
is not a standard header at all; it is specific to certain older DOS and Windows compilers. We generally recommend avoiding it. - Using
system()
to clear the screen is both slow and non-portable. I mention this because I am running on a Linux system, where the command to clear the screen isclear
, notcls
. It is best just not to use such a command in the first place, or if you must, find some solution that works better. If all else fails, put the command in a function, so you can change it easily when you port the code from one system to another. - I noticed that you had the C++ I/O calls in some of your comments, but used the C-style I/O calls in the actual code. Was there a particular reason for this?
- Also, do you have the compiler and linker warnings at maximum when you build this? You should, as you'll get a number of them in this code, and you should always treat warnings as errors unless you are absolutely certain that the warning doesn't apply to your code.
Getting to the actual problems, the first one that comes up is that in the following code:
case '4':
printf("Enter number: ");
int num;
scanf("%d", num);
delete_el(head, num);
printf("\nSuccessful, press any key to return menu!");
break;
... you dropped the pointer reference ('&'
) on the value being written to; it should be:
case '4':
printf("Enter number: ");
int num;
scanf("%d", &num); // note the ampersand
delete_el(head, num);
printf("\nSuccessful, press any key to return menu!");
break;
This is a small and easy to miss problem, one that comes up often enough the the g++ compiler warns about it. You should have gotten that warning when you compiled the code.
That seems to be the main issue, so here's the code again, this time properly formatted, with some minor editing done:
#include <cstdio>
#include <cstdlib>
struct node {
int inf;
node *next;
};
node *insert_head(node *first, int data);
node *insert_last(node *first, int data);
int is_empty(node *first);
int find_max(node *first);
int counting_el(node *first);
int print_list(node *first);
node *delete_el(node *, int dat);
int main() {
node *head = NULL;
char choose;
while(1){
printf("\t Menu\n 1.Insert to head\n 2.Insert to last\n 3.Number of elements\n 4.Delete\n 5.Print list\n 0.Exit!");
printf("\n Your choose (1-6): ");
scanf("%c", &choose);
switch(choose){
case '1':
int value;
printf("-> Enter value you want to add to head of list: ");
scanf("%d", &value);
head = insert_head(head, value);
printf("\nSuccessfull, press any key to return menu!");
break;
case '2':
int value1;
printf("-> Enter value you want to add to last of list: ");
scanf("%d", &value1);
head = insert_last(head, value1);
printf("\nSuccessfull, press any key to return menu!");
break;
case '3':
if (is_empty(head))
printf("->List is empty!");
else
printf("->List has %d elements!", counting_el(head));
break;
case '4':
printf("Enter number: ");
int num;
scanf("%d", &num);
delete_el(head, num);
printf("\nSuccessful, press any key to return menu!");
break;
case '5':
printf("-> Printing all elements: ");
printf("\nResult: ");
if (!print_list(head))
printf("List is empty!");
break;
case '0':
return 1;
default:
break;
}
}
}
node *insert_head(node *first, int data){
node *tmp;
tmp = new node;
tmp->inf = data;
if (!first){
first = tmp;
tmp->next = 0;
}
else {
tmp->next = first;
first = tmp;
}
return first;
}
node *insert_last(node *first, int data){
node *tmp, *run;
tmp = new node;
tmp->inf = data;
if(!first) {
tmp->next = 0;
first = tmp;
}
else {
run = first;
while(run->next) {
run = run->next;
}
run->next = tmp;
tmp->next = 0;
}
return first;
}
int is_empty(node *first){
return !first;
}
int find_max(node *first){
node *run = first;
int max = 0;
while (run){
if (run->inf > max) max = run->inf;
run = run->next;
}
return max;
}
node *delete_el(node *head, int dat){
node *tmp;
if (head->inf == dat){
tmp = head;
head = head->next;
delete tmp;
} else {
node *run = head;
while(run->next && run->next->inf != dat){
run = run->next;
}
if(!run->next){
printf("Cannot find!");
return head;
} else {
tmp = run->next;
run->next = tmp->next;
delete tmp;
}
}
return head;
}
int counting_el(node *first){
node *run;
int count = 0;
run = first;
while(run){
count++;
run = run->next;
}
return count;
}
int print_list(node *first){
node *run;
if (is_empty(first)) return 0;
else {
run = first;
while(run){
printf("%d ", run->inf);
run = run->next;
}
return 1;
}
}
@Schol-R-LEA: - Oh, that's so helpful. I'm trying to fix my old code in C, and may i post it to wrong box xD xD xD. This is CPP box.
- But it's not my problems, can you build and run it again and add 2 or 3 number to the list, then delete the first number of the list. Finally, print the list and you will see my problems.
- Thanks for helping me :)
Ah, I see what's happening, now, but only due to the serendipitous fact that my code doesn't clear the screen. Because I can see the results, I can see that it is in fact inserting and deleting correctly with the change I recommended; unfortunately, this is being lost when you print it, because the menu prints a second time, overwriting what was there.
This is not an unusual symptom, and is has to do with the way scanf()
works. What is actually occuring is that when scanf()
reads the from the input buffer one character at a time, the characters it reads include the newline character from when you hit the Enter key. Thus, it reads the first entry in the buffer, which is the character you entered, but then on the next pass, it reads the newline rather than waiting for the next char as you expected.
The solution is to use fgets()
to read in a whole line of data into a buffer (including the newline), then parse the data locally using sscanf()
. That should avoid the issue entirely.
Oh, I did see one other issue: you don't re-assign the head when you call your deletion function. This causes a problem with the list possibly getting corrupted.
#include <cstdio>
#include <cstdlib>
#define BUFSIZE 128
struct node {
int inf;
node *next;
};
node *insert_head(node *first, int data);
node *insert_last(node *first, int data);
int is_empty(node *first);
int find_max(node *first);
int counting_el(node *first);
int print_list(node *first);
node *delete_el(node *, int dat);
int main() {
node *head = NULL;
char choose;
char buffer[BUFSIZE];
while(1){
printf("\n\n\t Menu\n 1.Insert to head\n 2.Insert to last\n 3.Number of elements\n 4.Delete\n 5.Print list\n 0.Exit!");
printf("\n Your choose (1-6): ");
fgets(buffer, BUFSIZE, stdin);
choose = buffer[0];
switch(choose){
case '1':
int value;
printf("-> Enter value you want to add to head of list: ");
fgets(buffer, BUFSIZE, stdin);
sscanf(buffer, "%d", &value);
head = insert_head(head, value);
printf("\nSuccessfull, press any key to return menu!");
break;
case '2':
int value1;
printf("-> Enter value you want to add to last of list: ");
fgets(buffer, BUFSIZE, stdin);
sscanf(buffer, "%d", &value1);
head = insert_last(head, value1);
printf("\nSuccessfull, press any key to return menu!");
break;
case '3':
if (is_empty(head))
printf("->List is empty!");
else
printf("->List has %d elements!", counting_el(head));
break;
case '4':
printf("Enter number: ");
int num;
fgets(buffer, BUFSIZE, stdin);
sscanf(buffer, "%d", &num);
head = delete_el(head, num);
printf("\nSuccessful, press any key to return menu!");
break;
case '5':
printf("-> Printing all elements: ");
printf("\nResult: ");
if (!print_list(head))
printf("List is empty!");
break;
case '0':
return 1;
default:
break;
}
}
}
node *insert_head(node *first, int data){
node *tmp;
tmp = new node;
tmp->inf = data;
if (!first){
first = tmp;
tmp->next = 0;
}
else {
tmp->next = first;
first = tmp;
}
return first;
}
node *insert_last(node *first, int data){
node *tmp, *run;
tmp = new node;
tmp->inf = data;
if(!first) {
tmp->next = 0;
first = tmp;
}
else {
run = first;
while(run->next) {
run = run->next;
}
run->next = tmp;
tmp->next = 0;
}
return first;
}
int is_empty(node *first){
return !first;
}
int find_max(node *first){
node *run = first;
int max = 0;
while (run){
if (run->inf > max) max = run->inf;
run = run->next;
}
return max;
}
node *delete_el(node *head, int dat){
node *tmp;
if (head->inf == dat){
tmp = head;
head = head->next;
delete tmp;
} else {
node *run = head;
while(run->next && run->next->inf != dat){
run = run->next;
}
if(!run->next){
printf("Cannot find!");
return head;
} else {
tmp = run->next;
run->next = tmp->next;
delete tmp;
}
}
return head;
}
int counting_el(node *first){
node *run;
int count = 0;
run = first;
while(run){
count++;
run = run->next;
}
return count;
}
int print_list(node *first){
node *run;
if (is_empty(first)) return 0;
else {
run = first;
while(run){
printf("%d ", run->inf);
run = run->next;
}
return 1;
}
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.