SlideShare a Scribd company logo
Write a program in C that does the following:
a) Builds a simple linked list consisting of 10 nodes each of which contains a random integer
between 50 and 100 inclusive. The list is to be built in main() and must be a local variable in
main().
b) Includes a user-defined function named printout(arg) that takes in the head pointer of the
already-built linked list as an argument and prints out the contents of the linked list to the screen,
starting from the head of the list to the tail. See the output below.
Node #0 contains 53
Node #1 contains 78
Node #2 contains 95
. . .
Node #20 contains 100
c) Includes a user-defined function called sum() that adds up the contents of the data members of
each node in the list, and prints out the total as follows:
The sum total of all nodes in this list is
d) Includes a user-defined function called reverse() that takes in the head pointer of the already-
built linked list as an argument and will physically reverse the order of the nodes in the list (i.e.,
the new head will be the old tail and the new tail will be the old head of the list). Use the
function printout() to output its contents now in reverse order. This does NOT mean that it will
be read backwards, but rather, the reversed list will be read from head to tail!
** PLEASE MAKE SURE THIS PROGRAM WORKS IN C LANGUAGE !!
Solution
#include
#include
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Function to reverse the linked list */
static void reverse(struct node** head_ref)
{
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head_ref = prev;
}
/* Function to push a node */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Function to print linked list */
void printList(struct node *head)
{
struct node *temp = head;
int i=0;
while(temp != NULL)
{
printf("Node #%d contains %d ",i, temp->data);
temp = temp->next;
i++;
}
}
void sum(struct node *head)
{
int sum=0;
struct node *temp = head;
while(temp != NULL)
{
//printf("%d ", temp->data);
sum+=temp->data;
temp = temp->next;
}
printf(" The sum total of all nodes in this list is %d ",sum);
}
/* Driver program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
int i,t;
for(i=0;i<10;i++)
{
// (rand() % 51) generates random number between 0 and 50 adding 50 generates random
number beween 50 and 100 both inclusive
t=(rand() % 51)+50;
push(&head, t);
}
printf("Given linked list ");
printList(head);
sum(head);
reverse(&head);
printf(" Reversed Linked list  ");
printList(head);
return 0;
}

More Related Content

DOCX
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
PDF
C++Write a method Node Nodereverse() which reverses a list..pdf
DOCX
coding in C- Create a function called reverseList that takes the head.docx
PDF
How to do insertion sort on a singly linked list with no header usin.pdf
PDF
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
PDF
I need to implment a function that can reverse a single linked list..pdf
DOC
C program to insert a node in doubly linked list
PDF
write recursive function that calculates and returns the length of a.pdf
Write a C++ function that delete nodes in a doubly linkedlist- It shou.docx
C++Write a method Node Nodereverse() which reverses a list..pdf
coding in C- Create a function called reverseList that takes the head.docx
How to do insertion sort on a singly linked list with no header usin.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
I need to implment a function that can reverse a single linked list..pdf
C program to insert a node in doubly linked list
write recursive function that calculates and returns the length of a.pdf

Similar to Write a program in C that does the followinga) Builds a simple li.pdf (20)

PDF
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
PDF
Use C++ Write a function to merge two doubly linked lists. The input.pdf
DOCX
Below is a depiction of a doubly-linked list implementation of the bag.docx
PDF
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
DOCX
C++ please put everthing after you answer it- thanks Complete the stub.docx
PDF
LinkedList1LinkedList1LinkedList1111.pdf
PDF
TutorialII_Updated____niceupdateprogram.pdf
PDF
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
PDF
Program to insert in a sorted list #includestdio.h#include.pdf
DOCX
Write java program using linked list to get integer from user and.docx
PPTX
C Exam Help
PPTX
C Homework Help
PDF
This assignment and the next (#5) involve design and development of a.pdf
PDF
Please find the answer to the above problem as follows- Program.pdf
PDF
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
PDF
Lec-4_Linked-List (1).pdf
DOCX
C++ Please write the whole code that is needed for this assignment- wr.docx
PPTX
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
PDF
Lab-2.4 101.pdf
PPT
Linkedlist
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Below is a depiction of a doubly-linked list implementation of the bag.docx
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
C++ please put everthing after you answer it- thanks Complete the stub.docx
LinkedList1LinkedList1LinkedList1111.pdf
TutorialII_Updated____niceupdateprogram.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Program to insert in a sorted list #includestdio.h#include.pdf
Write java program using linked list to get integer from user and.docx
C Exam Help
C Homework Help
This assignment and the next (#5) involve design and development of a.pdf
Please find the answer to the above problem as follows- Program.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Lec-4_Linked-List (1).pdf
C++ Please write the whole code that is needed for this assignment- wr.docx
Data Structures and Agorithm: DS 05 Doubly Linked List.pptx
Lab-2.4 101.pdf
Linkedlist

More from kavithaarp (20)

PDF
Sharmeka recently stopped taking her medication but did not follow h.pdf
PDF
public interface Game Note interface in place of class { .pdf
PDF
Rana Issa is a college student from Egypt, who is majoring in Agricu.pdf
PDF
Public health surveillance includes all of the following EXCEPT Dis.pdf
PDF
Prepare a two to three page report titled Environmental Sustainabi.pdf
PDF
Program Requirements Create a (cmd line terminal) based program.pdf
PDF
PJ conducts an experimental study on the effects of soft music durin.pdf
PDF
Neurons and neuroglia are components of_ connective tissue nervous ti.pdf
PDF
In which component of an HVAC system does a chilled beam save energy.pdf
PDF
In every generation of a dynasty, the number of children born is X i.pdf
PDF
If you deposit money today in an account that pays 4.5 annual inter.pdf
PDF
HINT Eventually you will need to factor. Just before factoring, try.pdf
PDF
how can second messengers amplify a signal initiated by a toxicant o.pdf
PDF
Examine the following table of characters in four different species o.pdf
PDF
Elucidate the various functions and elements of business models. .pdf
PDF
describe the mutualistic relationship between trichonymphs, termite,.pdf
PDF
Could you help me answer this question...If an older adult in your c.pdf
PDF
comporative financial statements of Korbin Co mpany follow KORBIN COM.pdf
PDF
Compare and contrast how the nervous and endocrine systems control b.pdf
PDF
Answer 1 and 2 in detail Recently, Kathy Smith, a project manager fo.pdf
Sharmeka recently stopped taking her medication but did not follow h.pdf
public interface Game Note interface in place of class { .pdf
Rana Issa is a college student from Egypt, who is majoring in Agricu.pdf
Public health surveillance includes all of the following EXCEPT Dis.pdf
Prepare a two to three page report titled Environmental Sustainabi.pdf
Program Requirements Create a (cmd line terminal) based program.pdf
PJ conducts an experimental study on the effects of soft music durin.pdf
Neurons and neuroglia are components of_ connective tissue nervous ti.pdf
In which component of an HVAC system does a chilled beam save energy.pdf
In every generation of a dynasty, the number of children born is X i.pdf
If you deposit money today in an account that pays 4.5 annual inter.pdf
HINT Eventually you will need to factor. Just before factoring, try.pdf
how can second messengers amplify a signal initiated by a toxicant o.pdf
Examine the following table of characters in four different species o.pdf
Elucidate the various functions and elements of business models. .pdf
describe the mutualistic relationship between trichonymphs, termite,.pdf
Could you help me answer this question...If an older adult in your c.pdf
comporative financial statements of Korbin Co mpany follow KORBIN COM.pdf
Compare and contrast how the nervous and endocrine systems control b.pdf
Answer 1 and 2 in detail Recently, Kathy Smith, a project manager fo.pdf

Recently uploaded (20)

PPTX
Institutional Correction lecture only . . .
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
01-Introduction-to-Information-Management.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Lesson notes of climatology university.
PDF
Complications of Minimal Access Surgery at WLH
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
master seminar digital applications in india
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Insiders guide to clinical Medicine.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
GDM (1) (1).pptx small presentation for students
Institutional Correction lecture only . . .
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
01-Introduction-to-Information-Management.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Anesthesia in Laparoscopic Surgery in India
Lesson notes of climatology university.
Complications of Minimal Access Surgery at WLH
STATICS OF THE RIGID BODIES Hibbelers.pdf
VCE English Exam - Section C Student Revision Booklet
Abdominal Access Techniques with Prof. Dr. R K Mishra
O7-L3 Supply Chain Operations - ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
master seminar digital applications in india
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Pharma ospi slides which help in ospi learning
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Insiders guide to clinical Medicine.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GDM (1) (1).pptx small presentation for students

Write a program in C that does the followinga) Builds a simple li.pdf

  • 1. Write a program in C that does the following: a) Builds a simple linked list consisting of 10 nodes each of which contains a random integer between 50 and 100 inclusive. The list is to be built in main() and must be a local variable in main(). b) Includes a user-defined function named printout(arg) that takes in the head pointer of the already-built linked list as an argument and prints out the contents of the linked list to the screen, starting from the head of the list to the tail. See the output below. Node #0 contains 53 Node #1 contains 78 Node #2 contains 95 . . . Node #20 contains 100 c) Includes a user-defined function called sum() that adds up the contents of the data members of each node in the list, and prints out the total as follows: The sum total of all nodes in this list is d) Includes a user-defined function called reverse() that takes in the head pointer of the already- built linked list as an argument and will physically reverse the order of the nodes in the list (i.e., the new head will be the old tail and the new tail will be the old head of the list). Use the function printout() to output its contents now in reverse order. This does NOT mean that it will be read backwards, but rather, the reversed list will be read from head to tail! ** PLEASE MAKE SURE THIS PROGRAM WORKS IN C LANGUAGE !! Solution #include #include /* Link list node */ struct node { int data; struct node* next; }; /* Function to reverse the linked list */ static void reverse(struct node** head_ref) {
  • 2. struct node* prev = NULL; struct node* current = *head_ref; struct node* next; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *head_ref = prev; } /* Function to push a node */ void push(struct node** head_ref, int new_data) { /* allocate node */ struct node* new_node = (struct node*) malloc(sizeof(struct node)); /* put in the data */ new_node->data = new_data; /* link the old list off the new node */ new_node->next = (*head_ref); /* move the head to point to the new node */ (*head_ref) = new_node; } /* Function to print linked list */ void printList(struct node *head) { struct node *temp = head; int i=0; while(temp != NULL) { printf("Node #%d contains %d ",i, temp->data);
  • 3. temp = temp->next; i++; } } void sum(struct node *head) { int sum=0; struct node *temp = head; while(temp != NULL) { //printf("%d ", temp->data); sum+=temp->data; temp = temp->next; } printf(" The sum total of all nodes in this list is %d ",sum); } /* Driver program to test above function*/ int main() { /* Start with the empty list */ struct node* head = NULL; int i,t; for(i=0;i<10;i++) { // (rand() % 51) generates random number between 0 and 50 adding 50 generates random number beween 50 and 100 both inclusive t=(rand() % 51)+50; push(&head, t); } printf("Given linked list "); printList(head); sum(head); reverse(&head);
  • 4. printf(" Reversed Linked list "); printList(head); return 0; }