logo
  • Home
  • Test & Rank
  • About
  • Training
  • Programming
  • CS
  • IT
  • IS
  • ECE
  • EEE
  • EE
  • Civil
  • Mechanical
  • Chemical
  • Metallurgy
  • Instrumentation
  • Aeronautical
  • Aerospace
  • Biotechnology
  • Mining
  • Marine
  • Agriculture
  • MCA
  • BCA
  • Internship
  • Jobs
  • Contact

Questions & Answers

C Interview Questions
C++ Questions
Linux MCQs
C# Quiz
Java MCQs
JavaScript MCQs
SAN Questions
PHP Questions
Python Quiz

Computer Science Questions

Operating System Quiz
Computer Architecture MCQs
Software Architecture MCQs
Software Engineering MCQs
Artificial Intelligence MCQs
LISP Programming MCQs
Database Management MCQs
Computer Network MCQs
Microprocessor MCQs

C Programming Examples

Simple C Programs
C - Arrays
C - Matrix
C - Strings
C - Bitwise Operations
C - Linked Lists
C - Stacks & Queues
C - Searching & Sorting
C - Trees
C - Strings
C - File Handling
C - Mathematical Functions
C - Puzzles & Games
C Programs - Recursion
C Programs - No Recursion

Java Algorithms

Java - Numerical Problems
Java - Combinatorial Problems
Java - Graph Problems
Java - Hard Graph Problems
Java - Computation Geometry
Java - Sets & Strings
Java - Data-Structures
Java - Collection API Problems

C++ Algorithms

C++ - Numerical Problems
C++ - Combinatorial Problems
C++ - Graph Problems
C++ - Hard Graph Problems
C++ - Computation Geometry
C++ - Sets & Strings
C++ - Data-Structures
C++ - STL Library

C Algorithms

C - Numerical Problems
C - Combinatorial Problems
C - Graph Problems
C - Hard Graph Problems
C - Computation Geometry
C - Sets & Strings
C - Data-Structures

« Prev Page
Next Page »

C Program to Modify the Linked List such that All Even Numbers appear before all the Odd Numbers in the Modified Linked List

Posted on August 30, 2013 by Manish
This C Program modifies the linked list such that all even numbers appear before all the odd number in the modified linked list.

Here is a source code of the C program that modifies the linked list such that all even numbers appear before all the odd number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Modify the Linked List such that All Even Numbers
  3.  * appear before all the Odd Numbers in the Modified Linked List 
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. struct node
  9. {
  10.     int num;
  11.     struct node *next;
  12. };
  13.  
  14. void create(struct node **);
  15. void generate_evenodd(struct node *, struct node**);
  16. void release(struct node **);
  17. void display(struct node *);
  18.  
  19. int main()
  20. {
  21.     struct node *p = NULL, *q = NULL;
  22.     int key, result;
  23.  
  24.     printf("Enter data into the list\n");
  25.     create(&p);
  26.     printf("Displaying the nodes in the list:\n");
  27.     display(p);
  28.     generate_evenodd(p, &q);
  29.     printf("Displaying the list with even and then odd:\n");
  30.     display(q);
  31.     release(&p);
  32.  
  33.     return 0;
  34. }
  35.  
  36. void generate_evenodd(struct node *list, struct node **head)
  37. {
  38.     struct node *even = NULL, *odd = NULL, *temp;
  39.     struct node *reven, *rodd;
  40.     while (list != NULL)
  41.     {
  42.         temp = (struct node *)malloc(sizeof(struct node));
  43.         temp->num = list->num;
  44.         temp->next = NULL;
  45.         if (list->num % 2 == 0)
  46.         {
  47.             if (even == NULL)
  48.             {
  49.                 even = temp;
  50.             }
  51.             else
  52.             {
  53.                 reven->next = temp;
  54.             }
  55.             reven = temp;
  56.         }
  57.         else
  58.         {
  59.             if (odd == NULL)
  60.             {
  61.                 odd = temp;
  62.             }
  63.             else
  64.             {
  65.                 rodd->next = temp;
  66.             }
  67.             rodd = temp;
  68.         }
  69.         list = list->next;
  70.     }
  71.     reven->next = odd;
  72.     *head = even;
  73. }
  74.  
  75. void create(struct node **head)
  76. {
  77.     int c, ch;
  78.     struct node *temp, *rear;
  79.  
  80.     do
  81.     {
  82.         printf("Enter number: ");
  83.         scanf("%d", &c);
  84.         temp = (struct node *)malloc(sizeof(struct node));
  85.         temp->num = c;
  86.         temp->next = NULL;
  87.         if (*head == NULL)
  88.         {
  89.             *head = temp;
  90.         }
  91.         else
  92.         {
  93.             rear->next = temp;
  94.         }
  95.         rear = temp;
  96.         printf("Do you wish to continue [1/0]: ");
  97.         scanf("%d", &ch);
  98.     } while (ch != 0);
  99.     printf("\n");
  100. }
  101.  
  102. void display(struct node *p)
  103. {
  104.     while (p != NULL)
  105.     {
  106.         printf("%d\t", p->num);
  107.         p = p->next;
  108.     }
  109.     printf("\n");
  110. }
  111.  
  112. void release(struct node **head)
  113. {
  114.     struct node *temp = *head;
  115.     *head = (*head)->next;
  116.     while ((*head) != NULL)
  117.     {
  118.         free(temp);
  119.         temp = *head;
  120.         (*head) = (*head)->next;
  121.     }
  122. }

advertisement
$ gcc evenodd.c 
$ ./a.out
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 1
Enter number: 5
Do you wish to continue [1/0]: 1
Enter number: 6
Do you wish to continue [1/0]: 0
 
Displaying the nodes in the list:
1	2	3	4	5	6	
Displaying the list with even and then odd:
2	4	6	1	3	5

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.
« Prev Page - C Program to Input a String with atleast one Number, Print the Square of all the Numbers in a String
» Next Page - C Program to Count the Number of Repeated Occurrences of a particular Word in a String

« C Program to Input a String with atleast one Number, Print the Square of all the Numbers in a String
C Program to Check whether a given Character is present in a String, Find Frequency & Position of Occurrence »

Deep Dive @ Sanfoundry:

  1. Simple C Programs
  2. C# Programming Examples on LINQ
  3. C# Programming Examples on Data Structures
  4. Java Programming Examples on Data-Structures
  5. C Programming Examples
  6. C Programming Examples on Trees
  7. C++ Programming Examples on Data-Structures
  8. C Programming Examples using Recursion
  9. C Programming Examples without using Recursion
  10. C Programming Examples on Linked List
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below:
LinkedIn | Facebook | Twitter | Google+

Best Careers

Developer Tracks
SAN Developer
Linux Kernel Developer
Linux Driver Developer
Linux Network Developer

Live Training Photos
Mentoring
Software Productivity
GDB Assignment
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree, Cognizant and Startups.

Best Trainings

SAN I - Technology
SAN II - Admin
Linux Fundamentals
Advanced C Training
Linux-C Debugging
System Programming
Network Programming
Linux Threads
Kernel Programming
Kernel Debugging
Linux Device Drivers

Best Reference Books

Computer Science Books
Algorithm & Programming Books
Electronics Engineering Books
Electrical Engineering Books
Chemical Engineering Books
Civil Engineering Books
Mechanical Engineering Books
Industrial Engineering Books
Instrumentation Engg Books
Metallurgical Engineering Books
All Stream Best Books

Questions and Answers

1000 C Questions & Answers
1000 C++ Questions & Answers
1000 C# Questions & Answers
1000 Java Questions & Answers
1000 Linux Questions & Answers
1000 Python Questions
1000 PHP Questions & Answers
1000 Hadoop Questions
Cloud Computing Questions
Computer Science Questions
All Stream Questions & Answers

India Internships

Computer Science Internships
Instrumentation Internships
Electronics Internships
Electrical Internships
Mechanical Internships
Industrial Internships
Systems Internships
Chemical Internships
Civil Internships
IT Internships
All Stream Internships

About Sanfoundry

About Us
Copyright
Terms
Privacy Policy
Jobs
Bangalore Training
Online Training
Developers Track
Mentoring Sessions
Contact Us
Sitemap
© 2011 Sanfoundry. All Rights Reserved.