i am to write a program with a linked list of 10 int #\'s. thenhave the program
ID: 3607873 • Letter: I
Question
i am to write a program with a linked list of 10 int #'s. thenhave the program display the #'s in the list. seems simple, right?well i came up with one way to do it... but it doesn't seem quiteright. plus, i would like to write out a simpler version. any helpwould be great.#include <stdio.h>
struct list
{ int value;
struct list *next;
};
main()
{ struct list n1, n2, n3, n4, n5, n6, n7, n8, n9, n10;
int i;
n1.value = 10;
n2.value = 20;
n3.value = 30;
n4.value = 40;
n5.value = 50;
n6.value = 60;
n7.value = 70;
n8.value = 80;
n9.value = 90;
n10.value = 100;
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = &n7;
n7.next = &n8;
n8.next = &n9;
n9.next = &n10;
i = n1.next->value;
printf("%d ", n2.next->value); system(“pause”);
}i am to write a program with a linked list of 10 int #'s. thenhave the program display the #'s in the list. seems simple, right?well i came up with one way to do it... but it doesn't seem quiteright. plus, i would like to write out a simpler version. any helpwould be great.
#include <stdio.h>
struct list
{ int value;
struct list *next;
};
main()
{ struct list n1, n2, n3, n4, n5, n6, n7, n8, n9, n10;
int i;
n1.value = 10;
n2.value = 20;
n3.value = 30;
n4.value = 40;
n5.value = 50;
n6.value = 60;
n7.value = 70;
n8.value = 80;
n9.value = 90;
n10.value = 100;
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = &n7;
n7.next = &n8;
n8.next = &n9;
n9.next = &n10;
i = n1.next->value;
printf("%d ", n2.next->value); system(“pause”);
}
Explanation / Answer
struct list
{
int value;
struct list*next;
};
main()
{
struct listn1, n2, n3, n4, n5, n6, n7, n8, n9, n10,*ptr;
int i;
n1.value = 10;
n2.value = 20;
n3.value = 30;
n4.value = 40;
n5.value = 50;
n6.value = 60;
n7.value = 70;
n8.value = 80;
n9.value = 90;
n10.value = 100;
n1.next = &n2;
n2.next = &n3;
n3.next = &n4;
n4.next = &n5;
n5.next = &n6;
n6.next = &n7;
n7.next = &n8;
n8.next = &n9;
n9.next = &n10;
n10.next =NULL; /* Make the last next equal to NULL, means end of list*/
ptr= &n1;
while(ptr!=NULL) /* Now scan, until the end of listcome */
{
i =ptr->value; /* Get the value of current node*/
printf("%d ",i);
}
system("pause");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.