Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
October 6th, 2013, 01:53 PM
 tendoeschate
Registered User

Join Date: Aug 2013
Posts: 7
Time spent in forums: 59 m 21 sec
Reputation Power: 0
Program problem

in the following linked list program i input three nos for eg 123 and i want to display 312 but the output is 012 how to overcome this problem
the program is given below:

#include<stdio.h>

struct node {
int info;
struct node *next;
};
typedef struct node *nodeptr;

nodeptr q;
nodeptr p;

struct queue {
nodeptr front, rear;

};

nodeptr getnode(void)
{
nodeptr p;
p = (nodeptr) malloc(sizeof(struct node));
return p;
}

void freenode(nodeptr p)
{
free(p);
}

int main()
{
int j;
int y;
int x;
struct queue *b;
nodeptr *plist;
nodeptr *plist1;
nodeptr s;
nodeptr a;
nodeptr d;
q = *plist;

for (j = 0; j < 3; j++) {
printf("enter value");
scanf("%d", &y);
p = getnode();
p->info = y;
if (*plist == NULL)
*plist = p;

else {
q->next = p;
q = q->next;
}
}
q = *plist;
while ((q->next) != NULL) {

q = q->next;
printf("%d", q->info);
}
printf("enter element");
scanf("%d", &x);
s = NULL;
q = *plist;
while (q->next != NULL) {
s = q;
q = q->next;
if (q->info == x)
break;
}

a = q;
for (q = *plist; q->next != NULL; q = q->next);
q->next = *plist;
d = a->next;
a->next = NULL;
*plist = d;
q = *plist;
while ((q->next) != NULL) {

q = q->next;
printf("%d", q->info);
}
return 0;
}

#2
October 6th, 2013, 07:44 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,140
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 17 m 40 sec
Reputation Power: 455
Wow.

Read all of dwise1_aol's posts until you come to one admonishing those who ignore compiler warnings. It won't take long because so many of you are either new programmers or *nasty word removed by mom*.
Code:
```c.c:41:11: warning: variable ‘s’ set but not used [-Wunused-but-set-variable]
c.c:40:12: warning: unused variable ‘plist1’ [-Wunused-variable]
c.c:38:17: warning: unused variable ‘b’ [-Wunused-variable]
c.c:44:7: warning: ‘plist’ is used uninitialized in this function [-Wuninitialized]```
Do the unused variable warnings matter? Not here. Is it important that the first statement in main dereferences uninitialized memory?

YES!

Since you provided a broken program with probably an algorithm is implemented incorrectly, I can't tell what you intend. What if the input were

99 errors here

? What do you expect?

Suppose the input were a more reasonable

9 4 21

what would you expect for output?

How is 123 three numbers?
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : October 6th, 2013 at 07:47 PM.

#3
October 6th, 2013, 08:16 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,856
Time spent in forums: 3 Months 1 Day 5 h 6 m 32 sec
Reputation Power: 2199
And also, use code tags! By not using code tags, you've reduced your program to an unreadable mess. Which nobody would bother to read. That you used that K&R indenting style that hides all the open braces at the ends and even in the middle of lines and hence absolutely depends on proper indentation only makes matters worse -- you might consider using the vastly more readable Allman Style instead.

Here is what your program looks like with its formatting restored (indenting recovered via the Reply button):
Code:
```#include<stdio.h>

struct node {
int info;
struct node *next;
};
typedef struct node *nodeptr;

nodeptr q;
nodeptr p;

struct queue {
nodeptr front, rear;

};

nodeptr getnode(void)
{
nodeptr p;
p = (nodeptr) malloc(sizeof(struct node));
return p;
}

void freenode(nodeptr p)
{
free(p);
}

int main()
{
int j;
int y;
int x;
struct queue *b;
nodeptr *plist;
nodeptr *plist1;
nodeptr s;
nodeptr a;
nodeptr d;
q = *plist;

for (j = 0; j < 3; j++) {
printf("enter value");
scanf("%d", &y);
p = getnode();
p->info = y;
if (*plist == NULL)
*plist = p;

else {
q->next = p;
q = q->next;
}
}
q = *plist;
while ((q->next) != NULL) {

q = q->next;
printf("%d", q->info);
}
printf("enter element");
scanf("%d", &x);
s = NULL;
q = *plist;
while (q->next != NULL) {
s = q;
q = q->next;
if (q->info == x)
break;
}

a = q;
for (q = *plist; q->next != NULL; q = q->next);
q->next = *plist;
d = a->next;
a->next = NULL;
*plist = d;
q = *plist;
while ((q->next) != NULL) {

q = q->next;
printf("%d", q->info);
}
return 0;
}```
b49P23TIvg agrees: It's a mess with code tags too.

#4
October 7th, 2013, 12:27 PM
 tendoeschate
Registered User

Join Date: Aug 2013
Posts: 7
Time spent in forums: 59 m 21 sec
Reputation Power: 0
Quote:
 Originally Posted by b49P23TIvg Read all of dwise1_aol's posts until you come to one admonishing those who ignore compiler warnings. It won't take long because so many of you are either new programmers or *nasty word removed by mom*. Code: ```c.c:41:11: warning: variable ‘s’ set but not used [-Wunused-but-set-variable] c.c:40:12: warning: unused variable ‘plist1’ [-Wunused-variable] c.c:38:17: warning: unused variable ‘b’ [-Wunused-variable] c.c:44:7: warning: ‘plist’ is used uninitialized in this function [-Wuninitialized]``` Do the unused variable warnings matter? Not here. Is it important that the first statement in main dereferences uninitialized memory? YES! Since you provided a broken program with probably an algorithm is implemented incorrectly, I can't tell what you intend. What if the input were 99 errors here ? What do you expect? Suppose the input were a more reasonable 9 4 21 what would you expect for output? How is 123 three numbers?

#include<stdio.h>

struct node
{
int info;
struct node *next;
};
typedef struct node *nodeptr;

nodeptr q;
nodeptr p;

struct queue
{
nodeptr front,rear;

};

nodeptr getnode(void)
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
return p;
}
void freenode(nodeptr p)
{
free(p);
}

int main()
{
int j;
int y;
int x;
struct queue *b;
nodeptr *plist;
nodeptr *plist1;
nodeptr s;
nodeptr start;
nodeptr a;
nodeptr d;
q=*plist;

for(j=0; j<3; j++)
{
printf("enter value");
scanf("%d",&y);
p=getnode();
p->info=y;
if(*plist==NULL)
*plist=p;

else
{
q->next=p;
q=q->next;
}
}
q=*plist;
while((q->next)!=NULL)
{

q=q->next;
printf("%d",q->info);
}
start=*plist;
printf("enter element");
scanf("%d",&x);
s=NULL;
q=*plist;
while(q->next!=NULL)
{
s=q;
q=q->next;
if(q->info==x)
break;
}

a=q;
for(q=*plist; q->next!=NULL; q=q->next)
;
q->next=start;
d=a->next;
a->next=NULL;
*plist=d;
q=*plist;
while(q!=NULL)
{

printf("%d",q->info);
q=q->next->next;
}
return 0;
}

#5
October 7th, 2013, 12:49 PM
 tendoeschate
Registered User

Join Date: Aug 2013
Posts: 7
Time spent in forums: 59 m 21 sec
Reputation Power: 0
Quote:
 Originally Posted by tendoeschate #include struct node { int info; struct node *next; }; typedef struct node *nodeptr; nodeptr q; nodeptr p; struct queue { nodeptr front,rear; }; nodeptr getnode(void) { nodeptr p; p=(nodeptr)malloc(sizeof(struct node)); return p; } void freenode(nodeptr p) { free(p); } int main() { int j; int y; int x; struct queue *b; nodeptr *plist; nodeptr *plist1; nodeptr s; nodeptr start; nodeptr a; nodeptr d; q=*plist; for(j=0; j<3; j++) { printf("enter value"); scanf("%d",&y); p=getnode(); p->info=y; if(*plist==NULL) *plist=p; else { q->next=p; q=q->next; } } q=*plist; while((q->next)!=NULL) { q=q->next; printf("%d",q->info); } start=*plist; printf("enter element"); scanf("%d",&x); s=NULL; q=*plist; while(q->next!=NULL) { s=q; q=q->next; if(q->info==x) break; } a=q; for(q=*plist; q->next!=NULL; q=q->next) ; q->next=start; d=a->next; a->next=NULL; *plist=d; q=*plist; while(q!=NULL) { printf("%d",q->info); q=q->next->next; } return 0; } NOW I AM GETTING OUTPUT AS 3 1 BUT NOT 2 PLEASE HELP

ENHANCEMENT OF THIS
#include<stdio.h>

struct node
{
int info;
struct node *next;
};
typedef struct node *nodeptr;

nodeptr q;
nodeptr p;

struct queue
{
nodeptr front,rear;

};

nodeptr getnode(void)
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
return p;
}
void freenode(nodeptr p)
{
free(p);
}

int main()
{
int j;
int y;
int x;
struct queue *b;
nodeptr *plist;
nodeptr *plist1;
nodeptr s;
nodeptr start;
nodeptr a;
nodeptr d;
q=*plist;

for(j=0; j<3; j++)
{
printf("enter value");
scanf("%d",&y);
p=getnode();
p->info=y;
if(*plist==NULL)
*plist=p;

else
{
q->next=p;
q=q->next;
}
}
q=*plist;
while((q->next)!=NULL)
{

q=q->next;
printf("%d",q->info);
}
start=*plist;
printf("enter element");
scanf("%d",&x);
s=NULL;
q=*plist;
while(q->next!=NULL)
{
s=q;
q=q->next;
if(q->info==x)
{
a=q;
break;
}
}

for(q=*plist; q->next!=NULL; q=q->next)
;
q->next=start;
d=a->next;
a->next=NULL;
*plist=d;
q=*plist;
while(q!=NULL)
{

printf("%d%d",q->info,q->next->next->info);
q=q->next->next->next;
}
return 0;
}

NOW THE OUTPUT IS 3 1 2 0

#6
October 7th, 2013, 01:23 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,856
Time spent in forums: 3 Months 1 Day 5 h 6 m 32 sec
Reputation Power: 2199
Indent your code and use code tags to preserve that indentation! What part of that are you incapable of understanding? If you post an unreadable mess, then we won't bother to try to read it! What part of that are you incapable of understanding?

b49P23TIvg pointed you to a fatal flaw in your code, but you chose to ignore him!
Quote:
 Originally Posted by b49P23TIvg Is it important that the first statement in main dereferences uninitialized memory? YES!

Here is the offending code (note the use of indenting and of code tags!):
Code:
```int main()
{
int j;
int y;
int x;
struct queue *b;
nodeptr *plist;
nodeptr *plist1;
nodeptr s;
nodeptr start;
nodeptr a;
nodeptr d;

q=*plist;

for(j=0; j<3; j++)
{
printf("enter value");
scanf("%d",&y);
p=getnode();
p->info=y;
if(*plist==NULL)
*plist=p;
else
{
q->next=p;
q=q->next;
}
}```

Direct your attention to the line in red. What is plist pointing to? IOW, what is the value of plist? You don't know, do you? Neither do I, because it is an uninitialized pointer! That means that it contains any possible random garbage. It could be pointing anywhere. Using an uninitialized pointer is like standing in the middle of a crowded room, closing your eyes and pointing a loaded pistol in any random direction, and squeezing the trigger. Who in their right mind would do such a thing? Who in their right mind would use an uninitialized pointer? Both acts are just as reckless, irresponsible, and reprehensible. As is ignoring somebody's warnings when you are about to perform such a deed, especially when you went through the motions of asking that person for advice.

If plist is supposed to point to the start of the list, then, since there is no list at the start of the program, it needs to be initialized to indicate that there is no list. Then after that, you cannot change it or else you will have dropped that list. Consider this:
Code:
```    a=q;
for(q=*plist; q->next!=NULL; q=q->next);
q->next=start;
d=a->next;
a->next=NULL;
*plist=d;
q=*plist;```

You just dropped part of that list. Besides the fact that that is a major memory leak (a very insidious type of bug that is notoriously difficult to find, even though it is easy to correct), you have also lost a big chunk of data. Maybe you have a good reason to do that, but given the absence of comments, meaningful variables, or any kind of explanation of what your program is supposed to do, we have no way of knowing that. As b49P23TIvg commented about your code: "It's a mess with code tags too." Communication is an essential skill in programming. You need to write your code so that another programmer can pick it up and immediately be able to understand it. Meaningful variable names and informative comments are an essential part of that communication, as is making your code readable through proper formatting, which includes indenting your code and taking the proper steps to preserve that indentation (which here involves the use of code tags).

And what is that nonsense about with making plist a pointer to a pointer? All you ever do with it is dereference it! All you're doing is making your code even more confusing than it already is.

If we cannot understand what you are trying to do in that code, then we cannot help you.

If you ignore what we tell you, then we cannot help you.

PS
This is what I get when I compile your code (MingW gcc on WinXP):
Quote:
 C:TEST>gcc -Wall tendo2.c tendo2.c: In function `getnode': tendo2.c:22: warning: implicit declaration of function `malloc' tendo2.c: In function `freenode': tendo2.c:28: warning: implicit declaration of function `free' tendo2.c: In function `main': tendo2.c:39: warning: unused variable `plist1' tendo2.c:37: warning: unused variable `b' C:TEST>

You knew to #include stdio.h in order to provide the prototypes for the standard I/O functions, scanf and printf. So why didn't you do the same for malloc and free? Or for NULL? malloc and free are prototyped in two header files: malloc.h and stdlib.h. In addition, NULL is declared in stdlib.h.

The effects of not #include'ing a library function's header file is unpredictable. Sometimes you can get away with it. We have also seen it cause the program to crash, or at least for the function being called to completely fail to function properly.

Why neglect to provide the compiler with the information that it needs to do its job? Why neglect to #include the header files that it needs?

Why did you ignore the warnings you got because of that oversight? Only a fool ignores warnings.

Are you compiling with warnings turned off? That is even more foolhardy!

Until your program compiles cleaning (no errors or warnings which warnings turned on and up!), don't even bother to try to run it. Your program is broken. Any output or behavior your broken program would produce is absolutely meaningless.

If your program is broken, then fix it! Only after you have fixed it can you try to run it and only then can you deal with unexpected output.

Last edited by dwise1_aol : October 7th, 2013 at 01:37 PM.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Program problem