C Program to Reverse First N Elements of a Linked List

This C Program to reverse only first N elements of a linked list.

Here is a source code of the C Program that reverses only first n elements of a linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

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

$ cc nreverse.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]: 1
Enter number: 7
Do you wish to continue [1/0]: 0
 
Displaying the nodes in the list:
1	2	3	4	5	6	7	
Enter the number N to reverse first N node: 4
Reversing the list...
Displaying the reversed list:
4	3	2	1	5	6	7

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

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

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.