C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

linked list
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
pete
Guest





PostPosted: Sat Jun 09, 2012 8:49 am    Post subject: Re: linked list Reply with quote



Zhang Yuan wrote:
Quote:

----------------------------------------------------------------------------
#include<stdio.h
#include<stdlib.h
struct stu{
int num;
char name[12];
int age[0];
struct stu *next;
};

int main(){

struct stu *curr,*head;

head=(stu *)malloc(sizeof(stu));
curr=head;
scanf("%d",&(head->num));
curr->next=NULL;

while(curr->num!=0){
scanf("%s%d",curr->name,curr->age);
curr=(stu *)malloc(sizeof(struct stu));
scanf("%d",&(curr->num));
curr->next=NULL;
}
return 0;
}

--------------------------------------------------------------------------
it doesn't work.When I debug:

input :
101 zhang 19

the memory information:4 pictures
http://flic.kr/p/ccMQey

why 0x6f17e0 change from 00-->13 unexpectedly.
I think the "*next" and "int age"storaged in different place.
but it looks like it overwrite?

#include <stdio.h>
#include <stdlib.h>

struct stu{
struct stu *next;
int num;
char name[12];
int age;
};

int
main(void)
{
struct stu *curr,*head;

head = malloc(sizeof *head);
if (head != NULL) {
curr = head;
puts("enter number");
scanf("%d", &(head -> num));
curr -> next = NULL;
while(curr -> num != 0){
puts("enter name and age");
scanf("%s%d", curr -> name, &(curr -> age));
curr = malloc(sizeof *curr);
if (curr == NULL) {
break;
}
puts("enter number");
scanf("%d", &(curr -> num));
curr -> next = NULL;
}
}
return 0;
}

--
pete
Back to top
pete
Guest





PostPosted: Sat Jun 09, 2012 9:28 am    Post subject: Re: linked list Reply with quote



pete wrote:
Quote:

Zhang Yuan wrote:

----------------------------------------------------------------------------
#include<stdio.h
#include<stdlib.h
struct stu{
int num;
char name[12];
int age[0];
struct stu *next;
};

int main(){

struct stu *curr,*head;

head=(stu *)malloc(sizeof(stu));
curr=head;
scanf("%d",&(head->num));
curr->next=NULL;

while(curr->num!=0){
scanf("%s%d",curr->name,curr->age);
curr=(stu *)malloc(sizeof(struct stu));
scanf("%d",&(curr->num));
curr->next=NULL;
}
return 0;
}

--------------------------------------------------------------------------
it doesn't work.When I debug:

input :
101 zhang 19

the memory information:4 pictures
http://flic.kr/p/ccMQey

why 0x6f17e0 change from 00-->13 unexpectedly.
I think the "*next" and "int age"storaged in different place.
but it looks like it overwrite?

#include <stdio.h
#include <stdlib.h

struct stu{
struct stu *next;
int num;
char name[12];
int age;
};

int
main(void)
{
struct stu *curr,*head;

head = malloc(sizeof *head);
if (head != NULL) {
curr = head;
puts("enter number");
scanf("%d", &(head -> num));
curr -> next = NULL;
while(curr -> num != 0){
puts("enter name and age");
scanf("%s%d", curr -> name, &(curr -> age));
curr = malloc(sizeof *curr);

That doesn't make any sense!

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

struct stu{
struct stu *next;
int num;
char name[12];
int age;
};

void stu_free(struct stu *head);
void stu_print(struct stu *head);

int
main(void)
{
struct stu *curr,*head;

puts("/* BEGIN new.c */ output\n");
head = malloc(sizeof *head);
if (head != NULL) {
curr = head;
curr -> next = NULL;
puts("enter number");
if (1 > scanf("%d", &(head -> num))) {
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
while(curr -> num != 0) {
puts("enter name and age");
scanf("%s%d", curr -> name, &(curr -> age));
if (1 > scanf("%d", &(head -> num))) {
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
curr -> next= malloc(sizeof *(curr -> next));
if (curr -> next == NULL) {
break;
}
curr = curr -> next;
curr -> next = NULL;
puts("enter number");
scanf("%d", &(curr -> num));
}
*(curr -> name) = '\0';
curr -> age = 0;
}
stu_print(head);
stu_free(head);
puts("\n/* END new.c */ output");
return 0;
}


void
stu_free(struct stu *head)
{
struct stu *next;

while (head != NULL) {
next = head -> next;
free(head);
head = next;
}
}

void
stu_print(struct stu *head)
{
while (head != NULL) {
printf("\nnumber == %d\n", head -> num);
printf("name is %s\n", head -> name);
printf("age is %d\n", head -> age);
head = head -> next;
}
}


/* END new.c */

--
pete
Back to top
Zhang Yuan
Guest





PostPosted: Sat Jun 09, 2012 2:57 pm    Post subject: Re: linked list Reply with quote



On Saturday, June 9, 2012 7:28:16 PM UTC+8, pete wrote:
Quote:
pete wrote:

Zhang Yuan wrote:

----------------------------------------------------------------------------
#include<stdio.h
#include<stdlib.h
struct stu{
int num;
char name[12];
int age[0];
struct stu *next;
};

int main(){

struct stu *curr,*head;

head=(stu *)malloc(sizeof(stu));
curr=head;
scanf("%d",&(head->num));
curr->next=NULL;

while(curr->num!=0){
scanf("%s%d",curr->name,curr->age);
curr=(stu *)malloc(sizeof(struct stu));
scanf("%d",&(curr->num));
curr->next=NULL;
}
return 0;
}

--------------------------------------------------------------------------
it doesn't work.When I debug:

input :
101 zhang 19

the memory information:4 pictures
http://flic.kr/p/ccMQey

why 0x6f17e0 change from 00-->13 unexpectedly.
I think the "*next" and "int age"storaged in different place.
but it looks like it overwrite?

#include <stdio.h
#include <stdlib.h

struct stu{
struct stu *next;
int num;
char name[12];
int age;
};

int
main(void)
{
struct stu *curr,*head;

head = malloc(sizeof *head);
if (head != NULL) {
curr = head;
puts("enter number");
scanf("%d", &(head -> num));
curr -> next = NULL;
while(curr -> num != 0){
puts("enter name and age");
scanf("%s%d", curr -> name, &(curr -> age));
curr = malloc(sizeof *curr);

That doesn't make any sense!

/* BEGIN new.c */

#include <stdio.h
#include <stdlib.h

struct stu{
struct stu *next;
int num;
char name[12];
int age;
};

void stu_free(struct stu *head);
void stu_print(struct stu *head);

int
main(void)
{
struct stu *curr,*head;

puts("/* BEGIN new.c */ output\n");
head = malloc(sizeof *head);
if (head != NULL) {
curr = head;
curr -> next = NULL;
puts("enter number");
if (1 > scanf("%d", &(head -> num))) {
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
while(curr -> num != 0) {
puts("enter name and age");
scanf("%s%d", curr -> name, &(curr -> age));
if (1 > scanf("%d", &(head -> num))) {
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
curr -> next= malloc(sizeof *(curr -> next));
if (curr -> next == NULL) {
break;
}
curr = curr -> next;
curr -> next = NULL;
puts("enter number");
scanf("%d", &(curr -> num));
}
*(curr -> name) = '\0';
curr -> age = 0;
}
stu_print(head);
stu_free(head);
puts("\n/* END new.c */ output");
return 0;
}


void
stu_free(struct stu *head)
{
struct stu *next;

while (head != NULL) {
next = head -> next;
free(head);
head = next;
}

Why not just free head?


Quote:
}

void
stu_print(struct stu *head)
{
while (head != NULL) {
printf("\nnumber == %d\n", head -> num);
printf("name is %s\n", head -> name);
printf("age is %d\n", head -> age);
head = head -> next;
}
}


/* END new.c */

--
pete
Back to top
Ben Bacarisse
Guest





PostPosted: Sat Jun 09, 2012 4:16 pm    Post subject: Re: linked list Reply with quote

Zhang Yuan <zhabgyuan1993 (AT) gmail (DOT) com> writes:

Quote:
On Saturday, June 9, 2012 7:28:16 PM UTC+8, pete wrote:
snip
void
stu_free(struct stu *head)
{
struct stu *next;

while (head != NULL) {
next = head -> next;
free(head);
head = next;
}

Why not just free head?

Do you understand what pete's code is doing? Are you saying that there
is a simpler why to write code that does the same thing (if,write it out
so it's clear to everyone exactly what you are suggesting), or are you
saying that pete's function does not need to do everything that it does
and thus can be sorter or simpler?

Quote:
}

You might want to start sniping your replies so that they contain only
the parts needed to understand your comments. It will make them easier
to read and simpler to reply to.

--
Ben.
Back to top
Keith Thompson
Guest





PostPosted: Sat Jun 09, 2012 4:17 pm    Post subject: Re: linked list Reply with quote

Ike Naar <ike (AT) sverige (DOT) freeshell.org> writes:
Quote:
On 2012-06-09, Zhang Yuan <zhabgyuan1993 (AT) gmail (DOT) com> wrote:
struct stu{
int num;
char name[12];
int age[0];

There is room for zero ages in the struct ...
[...]


Actually, C doesn't permit zero-sized arrays. Declarating "int age[0]",
is a constraint violation, requiring a diagnostic (N1570 6.7.6.2p1).

--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to top
Keith Thompson
Guest





PostPosted: Sat Jun 09, 2012 4:20 pm    Post subject: Re: linked list Reply with quote

Zhang Yuan <zhabgyuan1993 (AT) gmail (DOT) com> writes:
Quote:
On Saturday, June 9, 2012 1:49:16 PM UTC+8, Ian Collins wrote:
On 06/ 9/12 04:02 PM, Zhang Yuan wrote:
#include<stdio.h
#include<stdlib.h
struct stu{
int num;
char name[12];
int age[0];

Didn't your compiler warn about this?

Yes,just warn.I didn't pay much attention to that

Always pay close attention to warnings.

gcc in particular often produces warnings for constraint violations,
i.e., errors for which a compiler is permitted to reject the program
altogether.

--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to top
pete
Guest





PostPosted: Sat Jun 09, 2012 6:01 pm    Post subject: Re: linked list Reply with quote

Zhang Yuan wrote:
Quote:

On Saturday, June 9, 2012 7:28:16 PM UTC+8, pete wrote:
pete wrote:

void
stu_free(struct stu *head)
{
struct stu *next;

while (head != NULL) {
next = head -> next;
free(head);
head = next;
}

Why not just free head?

Because just freeing head,
would leave the rest of the nodes in the linked list,
inaccessible yet still allocated.

--
pete
Back to top
pete
Guest





PostPosted: Sat Jun 09, 2012 6:50 pm    Post subject: Re: linked list Reply with quote

Zhang Yuan wrote:
Quote:

On Saturday, June 9, 2012 7:28:16 PM UTC+8, pete wrote:
pete wrote:

while(curr -> num != 0){
puts("enter name and age");
scanf("%s%d", curr -> name, &(curr -> age));
curr = malloc(sizeof *curr);

That doesn't make any sense!

scanf("%s%d", curr -> name, &(curr -> age));
if (1 > scanf("%d", &(head -> num))) {

And then I screwed it up again!

/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

struct stu {
struct stu *next;
int num;
int age;
char name[12];
};

void stu_free(struct stu *head);
void stu_print(struct stu *head);

int
main(void)
{
struct stu *curr, *head;

puts("/* BEGIN new.c */ output\n");
head = malloc(sizeof *head);
if (head != NULL) {
curr = head;
curr -> next = NULL;
puts("enter number");
if (1 > scanf("%d", &(head -> num))) {
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
while(curr -> num != 0) {
puts("enter name and age");
if (1 > scanf("%s%d", curr -> name, &(curr -> age))) {
stu_free(head);
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
curr -> next= malloc(sizeof *(curr -> next));
if (curr -> next == NULL) {
break;
}
curr = curr -> next;
curr -> next = NULL;
puts("enter number");
scanf("%d", &(curr -> num));
}
*(curr -> name) = '\0';
curr -> age = 0;
}
stu_print(head);
stu_free(head);
puts("\n/* END new.c */ output");
return 0;
}


void
stu_free(struct stu *head)
{
struct stu *next;

while (head != NULL) {
next = head -> next;
free(head);
head = next;
}
}

void
stu_print(struct stu *head)
{
while (head != NULL) {
printf("\nnumber is %d\n", head -> num);
printf("name is %s\n", head -> name);
printf("age is %d\n", head -> age);
head = head -> next;
}
}


/* END new.c */



--
pete
Back to top
Ike Naar
Guest





PostPosted: Sat Jun 09, 2012 10:02 pm    Post subject: Re: linked list Reply with quote

On 2012-06-09, Keith Thompson <kst-u (AT) mib (DOT) org> wrote:
Quote:
Ike Naar <ike (AT) sverige (DOT) freeshell.org> writes:
On 2012-06-09, Zhang Yuan <zhabgyuan1993 (AT) gmail (DOT) com> wrote:
struct stu{
int num;
char name[12];
int age[0];

There is room for zero ages in the struct ...
[...]

Actually, C doesn't permit zero-sized arrays. Declarating "int age[0]",
is a constraint violation, requiring a diagnostic (N1570 6.7.6.2p1).

True. Some compilers allow it as an extension
(e.g. gcc in default mode accepts it without a diagnostic).
With -ansi -pedantic gcc does give a diagnostic, but still generates
an executable.
Back to top
Zhang Yuan
Guest





PostPosted: Sun Jun 10, 2012 1:21 am    Post subject: Re: linked list Reply with quote

On Sunday, June 10, 2012 4:50:03 AM UTC+8, pete wrote:
Quote:
Zhang Yuan wrote:

On Saturday, June 9, 2012 7:28:16 PM UTC+8, pete wrote:
pete wrote:

while(curr -> num != 0){
puts("enter name and age");
scanf("%s%d", curr -> name, &(curr -> age));
curr = malloc(sizeof *curr);

That doesn't make any sense!

scanf("%s%d", curr -> name, &(curr -> age));
if (1 > scanf("%d", &(head -> num))) {

And then I screwed it up again!

/* BEGIN new.c */

#include <stdio.h
#include <stdlib.h

struct stu {
struct stu *next;
int num;
int age;
char name[12];
};

void stu_free(struct stu *head);
void stu_print(struct stu *head);

int
main(void)
{
struct stu *curr, *head;

puts("/* BEGIN new.c */ output\n");
head = malloc(sizeof *head);
if (head != NULL) {
curr = head;
curr -> next = NULL;
puts("enter number");
if (1 > scanf("%d", &(head -> num))) {
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
while(curr -> num != 0) {
puts("enter name and age");
if (1 > scanf("%s%d", curr -> name, &(curr -> age))) {
stu_free(head);
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
curr -> next= malloc(sizeof *(curr -> next));
if (curr -> next == NULL) {
break;
}
curr = curr -> next;
curr -> next = NULL;
puts("enter number");
scanf("%d", &(curr -> num));
}
*(curr -> name) = '\0';
curr -> age = 0;
}
stu_print(head);
stu_free(head);
puts("\n/* END new.c */ output");
return 0;
}


void
stu_free(struct stu *head)
{
struct stu *next;

while (head != NULL) {
next = head -> next;
free(head);
head = next;
}
}

void
stu_print(struct stu *head)
{
while (head != NULL) {
printf("\nnumber is %d\n", head -> num);
printf("name is %s\n", head -> name);
printf("age is %d\n", head -> age);
head = head -> next;
}
}


/* END new.c */



--
pete

I learn a lot!
Your code is very comprehensive.
I found the difference between natives and foreigners.
I post the same question on comp.lang.c and home forum.
This is explicit and penetrate deeply.
Thank you!
I want to know if there exist some standards about linked list?
Back to top
pete
Guest





PostPosted: Sun Jun 10, 2012 2:11 am    Post subject: Re: linked list Reply with quote

Zhang Yuan wrote:
Quote:

On Sunday, June 10, 2012 4:50:03 AM UTC+8, pete wrote:

/* BEGIN new.c */

I learn a lot!
Your code is very comprehensive.
I found the difference between natives and foreigners.
I post the same question on comp.lang.c and home forum.
This is explicit and penetrate deeply.
Thank you!

You're welcome.

Quote:
I want to know if there exist some standards about linked list?

I don't know about that.

I learned what I know about linked lists
mostly from this newsgroup
and also from working with linked lists.

I have some examples of linked list programs here:
http://www.mindspring.com/~pfilandr/C/lists_and_files/

These are library files:
file_lib.c
file_lib.h
list_io.c
list_io.h
list_lib.c
list_lib.h
list_type.h

These are files which use the library files:
file_collate.c
file_mul.c
file_parse.c
file_sort.c

Those programs mostly serve me as examples
of how to work with lists and files,
in case I forget.

--
pete
Back to top
Zhang Yuan
Guest





PostPosted: Sun Jun 10, 2012 5:20 am    Post subject: Re: linked list Reply with quote

On Sunday, June 10, 2012 12:11:58 PM UTC+8, pete wrote:
Quote:
Zhang Yuan wrote:

On Sunday, June 10, 2012 4:50:03 AM UTC+8, pete wrote:

/* BEGIN new.c */

I learn a lot!
Your code is very comprehensive.
I found the difference between natives and foreigners.
I post the same question on comp.lang.c and home forum.
This is explicit and penetrate deeply.
Thank you!

You're welcome.

I want to know if there exist some standards about linked list?

I don't know about that.

I learned what I know about linked lists
mostly from this newsgroup
and also from working with linked lists.

I have some examples of linked list programs here:
http://www.mindspring.com/~pfilandr/C/lists_and_files/

These are library files:
file_lib.c
file_lib.h
list_io.c
list_io.h
list_lib.c
list_lib.h
list_type.h

These are files which use the library files:
file_collate.c
file_mul.c
file_parse.c
file_sort.c

Those programs mostly serve me as examples
of how to work with lists and files,
in case I forget.

--
pete

thank you.
I will try my best to learn.

Ps:I just look up your photo in New York,1998.Big fish!
Back to top
pete
Guest





PostPosted: Sun Jun 10, 2012 10:31 am    Post subject: Re: linked list Reply with quote

Zhang Yuan wrote:
Quote:

On Sunday, June 10, 2012 12:11:58 PM UTC+8, pete wrote:

These are files which use the library files:
file_collate.c
file_mul.c
file_parse.c
file_sort.c

Those programs mostly serve me as examples
of how to work with lists and files,
in case I forget.
thank you.
I will try my best to learn.

You're welcome.

I think file_sort.c is the simplest.
Then file_mul.c and
then file_parse.c.
file_collate.c is a little complicated.

Quote:
Ps:I just look up your photo in New York,1998.Big fish!

I was wading in South Sandy Creek for that one.
I had her on line for 15 minutes,
during which she jumped completely out of the water twice.

--
pete
Back to top
Guest






PostPosted: Mon Jun 11, 2012 1:10 pm    Post subject: Re: linked list Reply with quote

On Sunday, June 10, 2012 2:21:23 AM UTC+1, Zhang Yuan wrote:

<snip>

Quote:
I learn a lot!
Your code is very comprehensive.
I found the difference between natives and foreigners.
I post the same question on comp.lang.c and home forum.
This is explicit and penetrate deeply.

the average age and years experience on comp.lang.c (clc) is probably
higher than your home forum. Or put it another way- clc has an ageing demographic!

Quote:
Thank you!
I want to know if there exist some standards about linked list?

any book on algorithms. Sedgewick "Algorithms" is quite readable,
Knuth "The Art of Computer programming" is rather thorough.
The wikipedia entry for "Linked List" looks ok (I haven't read
it carefully).
Back to top
Zhang Yuan
Guest





PostPosted: Mon Jun 11, 2012 4:16 pm    Post subject: Re: linked list Reply with quote

On Monday, June 11, 2012 9:10:05 PM UTC+8, nick_keigh...@hotmail.com wrote:
Quote:
On Sunday, June 10, 2012 2:21:23 AM UTC+1, Zhang Yuan wrote:

snip

I learn a lot!
Your code is very comprehensive.
I found the difference between natives and foreigners.
I post the same question on comp.lang.c and home forum.
This is explicit and penetrate deeply.

the average age and years experience on comp.lang.c (clc) is probably
higher than your home forum. Or put it another way- clc has an ageing demographic!

Thank you!
I want to know if there exist some standards about linked list?

any book on algorithms. Sedgewick "Algorithms" is quite readable,
Knuth "The Art of Computer programming" is rather thorough.
The wikipedia entry for "Linked List" looks ok (I haven't read
it carefully).

Thanks,I haven't studied algorithms.
It's time to start.


I want to participate in some open source project.
I found some in github,but quite big for me.
Is there any proper programs i can join in?
Thanks.
Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
 


Powered by phpBB © 2001, 2006 phpBB Group