linked list

Z

Zhang Yuan

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 */

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?
 
Z

Zhang Yuan

You're welcome.


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.

thank you.
I will try my best to learn.

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

Guest

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

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).
 
Z

Zhang Yuan

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



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!


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.
 
K

Keith Thompson

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


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!


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).

Knuth is *extremely* thorough, but it's not a tutorial.
 
8

88888 Dihedral

Kaz Kylhekuæ–¼ 2012å¹´6月9日星期六UTC+8下åˆ12時59分57秒寫é“:
----------------------------------------------------------------------------
#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));

If you're really working in C and not C++, you want sizeof (struct stu)
here (just like in your other malloc call below).

(In C++, a struct stu { ... } declaration will introduce stu as a type name,
but not in C.

In C, if you have some defined object called "stu" somewhere, or a type "stu",
this sizeof will measure the size of that object or type, and not "structstu".

Example:

int stu;

head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */
curr=head;
scanf("%d",&(head->num));

scanf without checking the return value is a very bad way to obtain input..
What if the next input character is not a digit? What if EOF is signaled?

scanf with error checking is still very bad for interactive input.
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;
}

This leaks memory. You have assigned a new object to curr
and populated some of its fields, num and next.
but this object is not entered into the linked list.

If the input ends with EOF or is erroneous, you potentially
have an infinite loop. Worse, an infinite loop which
allocates memory.

None of that readily explains the overwrite of 19.
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.

At what point in the execution of the program?

If you single step the program, which line?



Kaz Kylhekuæ–¼ 2012å¹´6月9日星期六UTC+8下åˆ12時59分57秒寫é“:
----------------------------------------------------------------------------
#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));

If you're really working in C and not C++, you want sizeof (struct stu)
here (just like in your other malloc call below).

(In C++, a struct stu { ... } declaration will introduce stu as a type name,
but not in C.

In C, if you have some defined object called "stu" somewhere, or a type "stu",
this sizeof will measure the size of that object or type, and not "structstu".

Example:

int stu;

head = (stu *) malloc(sizeof(stu)); /* actually sizeof int */
head = (stu *) malloc(sizeof(struct stu)); /* size of the structure */
curr=head;
scanf("%d",&(head->num));

scanf without checking the return value is a very bad way to obtain input..
What if the next input character is not a digit? What if EOF is signaled?

scanf with error checking is still very bad for interactive input.
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;
}

This leaks memory. You have assigned a new object to curr
and populated some of its fields, num and next.
but this object is not entered into the linked list.

If the input ends with EOF or is erroneous, you potentially
have an infinite loop. Worse, an infinite loop which
allocates memory.

None of that readily explains the overwrite of 19.
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.

At what point in the execution of the program?

If you single step the program, which line?

Well, one can implement an instance of a container class
that can be iterable in C.

What's next?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,205
Latest member
ElwoodDurh

Latest Threads

Top