SlideShare a Scribd company logo
// C code on linked list
#include
#include
// structure for linked list
struct node
{
int value;
struct node *next;
};
// insert new value to linked list
void insert(struct node** head, int data)
{
// create node and insert value
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->value = data;
// update head
newNode->next = (*head);
(*head) = newNode;
}
// print linked list
void print(struct node *node)
{
printf("[ ");
while (node != NULL)
{
printf("%d", node->value);
node = node->next;
}
printf("] ");
}
int main()
{
// empty list
struct node* head = NULL;
int array[5], i;
printf("Enter 5 values to add to list: ");
for (i = 0; i < 5; ++i)
{
scanf("%d",&array[i]);
}
// add elements to linked list
for (i = 0; i < 5; ++i)
{
insert(&head, array[i]);
}
printf(" Linked list: ");
print(head);
printf("  ");
int number;
printf("Add one more value to list: ");
scanf("%d",&number);
insert(&head, number);
printf(" Final Linked list: ");
print(head);
printf(" ");
return 0;
}
/*
output:
Enter 5 values to add to list: 5 4 7 9 1
Linked list: [ 1 9 7 4 5 ]
Add one more value to list: 6
Final Linked list: [ 6 1 9 7 4 5 ]
*/
Solution
// C code on linked list
#include
#include
// structure for linked list
struct node
{
int value;
struct node *next;
};
// insert new value to linked list
void insert(struct node** head, int data)
{
// create node and insert value
struct node* newNode = (struct node*) malloc(sizeof(struct node));
newNode->value = data;
// update head
newNode->next = (*head);
(*head) = newNode;
}
// print linked list
void print(struct node *node)
{
printf("[ ");
while (node != NULL)
{
printf("%d", node->value);
node = node->next;
}
printf("] ");
}
int main()
{
// empty list
struct node* head = NULL;
int array[5], i;
printf("Enter 5 values to add to list: ");
for (i = 0; i < 5; ++i)
{
scanf("%d",&array[i]);
}
// add elements to linked list
for (i = 0; i < 5; ++i)
{
insert(&head, array[i]);
}
printf(" Linked list: ");
print(head);
printf("  ");
int number;
printf("Add one more value to list: ");
scanf("%d",&number);
insert(&head, number);
printf(" Final Linked list: ");
print(head);
printf(" ");
return 0;
}
/*
output:
Enter 5 values to add to list: 5 4 7 9 1
Linked list: [ 1 9 7 4 5 ]
Add one more value to list: 6
Final Linked list: [ 6 1 9 7 4 5 ]
*/

More Related Content

DOCX
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
PDF
Linked list
PDF
Program to insert in a sorted list #includestdio.h#include.pdf
DOC
C program to insert a node in doubly linked list
PPTX
Singly linked list.pptx
PPTX
Ll.pptx
PPTX
linked list.pptx
PPTX
linkedlistforslideshare-210123143943.pptx
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
Linked list
Program to insert in a sorted list #includestdio.h#include.pdf
C program to insert a node in doubly linked list
Singly linked list.pptx
Ll.pptx
linked list.pptx
linkedlistforslideshare-210123143943.pptx

Similar to C code on linked list #include stdio.h #include stdlib.h.pdf (20)

PPTX
Implemention of Linked list concept in Data Structures
PPTX
Linked list
PPTX
Unit II Data Structure 2hr topic - List - Operations.pptx
PPTX
Circular linked list.pptx
PPTX
LINKED LIST.pptx
DOCX
What is Linked List in C.docx
PDF
How to do insertion sort on a singly linked list with no header usin.pdf
DOCX
DS UNIT4_OTHER LIST STRUCTURES.docx
PPTX
Unit 5 linked list
PPTX
Data Structures Linked List, Double Linked List
PDF
TutorialII_Updated____niceupdateprogram.pdf
PDF
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
PPT
Linkedlist
PDF
^^^Q2. Discuss about Header Node    And also write a program fo.pdf
PPTX
Linked list part-2
PPTX
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
PDF
Singly Linked List
PPTX
Linked Lists, Single Linked list and its operations
DOCX
Shortened Linked List in C programming easy to learn for exam
PPTX
link listawetewrtwertwertewrtwertwer.pptx
Implemention of Linked list concept in Data Structures
Linked list
Unit II Data Structure 2hr topic - List - Operations.pptx
Circular linked list.pptx
LINKED LIST.pptx
What is Linked List in C.docx
How to do insertion sort on a singly linked list with no header usin.pdf
DS UNIT4_OTHER LIST STRUCTURES.docx
Unit 5 linked list
Data Structures Linked List, Double Linked List
TutorialII_Updated____niceupdateprogram.pdf
Linked List, Types of Linked LIst, Various Operations, Applications of Linked...
Linkedlist
^^^Q2. Discuss about Header Node    And also write a program fo.pdf
Linked list part-2
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
Singly Linked List
Linked Lists, Single Linked list and its operations
Shortened Linked List in C programming easy to learn for exam
link listawetewrtwertwertewrtwertwer.pptx
Ad

More from deepua8 (20)

PDF
Balancing ANY chemical equation is done exactly t.pdf
PDF
36.Kovacs reagent is used in Indole test. Kovacs reagent is 4 (p)-.pdf
PDF
10.Real number1.Baseb. Consists of a set and rule for combining2.Bin.pdf
PDF
1. Yeasts grow by budding. The cell buds and separates into 2 cells..pdf
PDF
1. 252.125Solution1. 252.125.pdf
PDF
(B) 0.815Solution(B) 0.815.pdf
PDF
Suppose AFnSolution Suppose AFn.pdf
PDF
Definition of Log-Normal DistributionA statistical distr.pdf
PDF
Step1 NaOH (aq) ----- Na(+)(aq) + Cl(-)(aq) Ste.pdf
PDF
The compounds with low oxidation states (O.S.) be.pdf
PDF
A) ionic compounds generally formed between the c.pdf
PDF
A ethers ethers contain R-O-R linkage not carbony.pdf
PDF
The fact that a molecule vibrates does not in its.pdf
PDF
The compounds of interest are Na2S and H2SO4. Th.pdf
PDF
sucrose has a formula of C12H22O11while ammonia h.pdf
PDF
reduction strength Fe Pb As .pdf
PDF
sp3d since it has 1s 3p and 1d orbitals available.pdf
PDF
No standard potential data given. .pdf
PDF
x2 + 4xSolutionx2 + 4x.pdf
PDF
moles of Al = 1227 Since chlorine is in excess ,.pdf
Balancing ANY chemical equation is done exactly t.pdf
36.Kovacs reagent is used in Indole test. Kovacs reagent is 4 (p)-.pdf
10.Real number1.Baseb. Consists of a set and rule for combining2.Bin.pdf
1. Yeasts grow by budding. The cell buds and separates into 2 cells..pdf
1. 252.125Solution1. 252.125.pdf
(B) 0.815Solution(B) 0.815.pdf
Suppose AFnSolution Suppose AFn.pdf
Definition of Log-Normal DistributionA statistical distr.pdf
Step1 NaOH (aq) ----- Na(+)(aq) + Cl(-)(aq) Ste.pdf
The compounds with low oxidation states (O.S.) be.pdf
A) ionic compounds generally formed between the c.pdf
A ethers ethers contain R-O-R linkage not carbony.pdf
The fact that a molecule vibrates does not in its.pdf
The compounds of interest are Na2S and H2SO4. Th.pdf
sucrose has a formula of C12H22O11while ammonia h.pdf
reduction strength Fe Pb As .pdf
sp3d since it has 1s 3p and 1d orbitals available.pdf
No standard potential data given. .pdf
x2 + 4xSolutionx2 + 4x.pdf
moles of Al = 1227 Since chlorine is in excess ,.pdf
Ad

Recently uploaded (20)

PDF
Pre independence Education in Inndia.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Lesson notes of climatology university.
PDF
Basic Mud Logging Guide for educational purpose
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pre independence Education in Inndia.pdf
O7-L3 Supply Chain Operations - ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
01-Introduction-to-Information-Management.pdf
Microbial diseases, their pathogenesis and prophylaxis
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial disease of the cardiovascular and lymphatic systems
PPH.pptx obstetrics and gynecology in nursing
STATICS OF THE RIGID BODIES Hibbelers.pdf
Renaissance Architecture: A Journey from Faith to Humanism
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Lesson notes of climatology university.
Basic Mud Logging Guide for educational purpose
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

C code on linked list #include stdio.h #include stdlib.h.pdf

  • 1. // C code on linked list #include #include // structure for linked list struct node { int value; struct node *next; }; // insert new value to linked list void insert(struct node** head, int data) { // create node and insert value struct node* newNode = (struct node*) malloc(sizeof(struct node)); newNode->value = data; // update head newNode->next = (*head); (*head) = newNode; } // print linked list void print(struct node *node) { printf("[ "); while (node != NULL) { printf("%d", node->value); node = node->next; } printf("] "); } int main() { // empty list struct node* head = NULL;
  • 2. int array[5], i; printf("Enter 5 values to add to list: "); for (i = 0; i < 5; ++i) { scanf("%d",&array[i]); } // add elements to linked list for (i = 0; i < 5; ++i) { insert(&head, array[i]); } printf(" Linked list: "); print(head); printf(" "); int number; printf("Add one more value to list: "); scanf("%d",&number); insert(&head, number); printf(" Final Linked list: "); print(head); printf(" "); return 0; } /* output: Enter 5 values to add to list: 5 4 7 9 1 Linked list: [ 1 9 7 4 5 ] Add one more value to list: 6 Final Linked list: [ 6 1 9 7 4 5 ] */ Solution // C code on linked list #include #include
  • 3. // structure for linked list struct node { int value; struct node *next; }; // insert new value to linked list void insert(struct node** head, int data) { // create node and insert value struct node* newNode = (struct node*) malloc(sizeof(struct node)); newNode->value = data; // update head newNode->next = (*head); (*head) = newNode; } // print linked list void print(struct node *node) { printf("[ "); while (node != NULL) { printf("%d", node->value); node = node->next; } printf("] "); } int main() { // empty list struct node* head = NULL; int array[5], i; printf("Enter 5 values to add to list: "); for (i = 0; i < 5; ++i) {
  • 4. scanf("%d",&array[i]); } // add elements to linked list for (i = 0; i < 5; ++i) { insert(&head, array[i]); } printf(" Linked list: "); print(head); printf(" "); int number; printf("Add one more value to list: "); scanf("%d",&number); insert(&head, number); printf(" Final Linked list: "); print(head); printf(" "); return 0; } /* output: Enter 5 values to add to list: 5 4 7 9 1 Linked list: [ 1 9 7 4 5 ] Add one more value to list: 6 Final Linked list: [ 6 1 9 7 4 5 ] */