C Program to Add Corresponding Positioned Elements of 2 Linked Lists

«
»
This C Program adds the corresponding positioned elements of 2 linked lists and display.

Here is source code of the C Program to add corresponding positioned elements of 2 linked lists. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Add Corresponding Positioned Elements of 2 Linked Lists 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7.  
  8. struct node
  9. {
  10.     int num;
  11.     struct node *next;
  12. };
  13.  
  14. int feednumber(struct node **);
  15. struct node *addlist(struct node *, struct node *, int, int);
  16. void release(struct node **);
  17. void display(struct node *);
  18.  
  19. int main()
  20. {
  21.     struct node *p = NULL;
  22.     struct node *q = NULL;
  23.     struct node *res = NULL;
  24.     int pcount = 0, qcount = 0;
  25.  
  26.     printf("Enter first number\n");
  27.     pcount = feednumber(&p);
  28.     printf("Enter second number\n");
  29.     qcount = feednumber(&q);
  30.     printf("Displaying list1: ");
  31.     display(p);
  32.     printf("Displaying list2: ");
  33.     display(q);
  34.     res = addlist(p, q, pcount, qcount);
  35.     printf("Displaying the resulting list: ");
  36.     display(res);
  37.     release(&p);
  38.     release(&q);
  39.     release(&res);
  40.  
  41.     return 0;
  42. }
  43.  
  44. /*Function to create nodes of numbers*/
  45. int feednumber(struct node **head)
  46. {
  47.     char ch, dig;
  48.     int count = 0;
  49.     struct node *temp, *rear = NULL;
  50.  
  51.     ch = getchar();
  52.     while (ch != '\n')
  53.     {
  54.         dig = atoi(&ch);
  55.         temp = (struct node *)malloc(sizeof(struct node));
  56.         temp->num = dig;
  57.         temp->next = NULL;
  58.         count++;
  59.         if ((*head) == NULL)
  60.         {
  61.             *head = temp;
  62.             rear = temp;
  63.         }
  64.         else
  65.         {
  66.             rear->next = temp;
  67.             rear = rear->next;
  68.         }
  69.         ch = getchar();
  70.     }
  71.  
  72.     return count;
  73. }
  74.  
  75. /*Function to display the list of numbers*/
  76. void display (struct node *head)
  77. {
  78.     while (head != NULL)
  79.     {
  80.         printf("%d", head->num);
  81.         head = head->next;
  82.     }
  83.     printf("\n");
  84. }
  85.  
  86. /*Function to free the allocated list of numbers*/
  87. void release (struct node **head)
  88. {
  89.     struct node *temp = *head;
  90.  
  91.     while ((*head) != NULL)
  92.     {
  93.         (*head) = (*head)->next;
  94.         free(temp);
  95.         temp = *head;
  96.     }
  97. }
  98.  
  99. /*Function to add the list of numbers and store them in 3rd list*/
  100. struct node *addlist(struct node *p, struct node *q, int pcount, int qcount)
  101. {
  102.     struct node *ptemp, *qtemp, *result = NULL, *temp;
  103.     int i, carry = 0;
  104.  
  105.     while (pcount != 0 && qcount != 0)
  106.     {
  107.         ptemp = p;
  108.         qtemp = q;
  109.         for (i = 0; i < pcount - 1; i++)
  110.         {
  111.             ptemp = ptemp->next;
  112.         }
  113.         for (i = 0; i < qcount - 1; i++)
  114.         {
  115.             qtemp = qtemp->next;
  116.         }
  117.         temp = (struct node *) malloc (sizeof(struct node));
  118.         temp->num = ptemp->num + qtemp->num + carry;
  119.         carry = temp->num / 10;
  120.         temp->num = temp->num % 10;
  121.         temp->next = result;
  122.         result = temp;
  123.         pcount--;
  124.         qcount--;
  125.     }
  126.     /*both or one of the 2 lists have been read completely by now*/
  127.     while (pcount != 0)
  128.     {
  129.         ptemp = p;
  130.         for (i = 0; i < pcount - 1; i++)
  131.         {
  132.             ptemp = ptemp->next;
  133.         }
  134.         temp = (struct node *) malloc (sizeof(struct node));
  135.         temp->num = ptemp->num + carry;
  136.         carry = temp->num / 10;
  137.         temp->num = temp->num % 10;
  138.         temp->next = result;
  139.         result = temp;
  140.         pcount--;
  141.     }
  142.     while (qcount != 0)
  143.     {
  144.         qtemp = q;
  145.         for (i = 0; i < qcount - 1; i++)
  146.         {
  147.             qtemp = qtemp->next;
  148.         }
  149.         temp = (struct node *) malloc (sizeof(struct node));
  150.         temp->num = qtemp->num + carry;
  151.         carry = temp->num / 10;
  152.         temp->num = temp->num % 10;
  153.         temp->next = result;
  154.         result = temp;
  155.         qcount--;
  156.     }
  157.  
  158.     return result;
  159. }

advertisement
$ cc add2lists.c
$ ./a.out
Enter first number
12345
Enter second number
5678903
Displaying list1: 12345
Displaying list2: 5678903
Displaying the resulting list: 5691248

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.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & 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, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn