Algorithm
INPUT: HEADER AND HEADER1 ARE POINTER TO THE HEADER NODE OF THE LINKEDLISTS
OUTPUT: CONCATENATED LINKEDLIST
DATA STRUCTURE: SINGLY LINKEDLIST
STEP 1: START
STEP 2: PTR=HEADER->LINK
STEP 3: WHILE(PTR->LINK!=NULL)
STEP 4: PTR=PTR->LINK
STEP 5: ENDWHILE
STEP 6: PTR->LINK=HEADER1->LINK
STEP 7: END
Code
#include <stdio.h>
#include <stdlib.h>
int getnode(int);
void display();
int concat();
int x, pos, n;
struct node
{
int data;
struct node *link;
};
struct node *prev, *head, *news, *p, *ptr;
struct node *q1;
struct node *header, *header1;
struct node *q, *r, *s;
int main()
{
printf("ENTER THE LIMIT OF LINKERLIST 1");
scanf("%d", &n);
getnode(n);
header1=header;
printf("ENTER THE LIMIT OF LINKEDLIST 2 ");
scanf("%d", &n);
getnode(n);
concat();
printf("THE CONCATENATED LIST IS:");
display(header1);
return 0;
}
void display(struct node *head1)
{
struct node *ptr;
ptr = head1->link;
// ptr=header->link;
while (ptr != NULL)
{
printf("%d ", ptr->data);
ptr = ptr->link;
}
}
concat()
{
q=header1->link;
while(q!=NULL)
{
q1=q;
// printf(" %d",q->data);
q=q->link;
}
q1->link=header->link;
// q->link=header1;
}
int getnode(int n)
{
header = malloc(sizeof(struct node));
head = NULL;
for (int i = 0; i < n; i++)
{
p = malloc(sizeof(struct node));
q1 = malloc(sizeof(struct node));
q = NULL;
scanf("%d", &p->data);
p->link = NULL;
if (head == NULL)
head = p;
else
prev->link = p;
prev = p;
}
header->link = head;
display(header);
return 0;
}
Output
user@computer$ : ENTER THE LIMIT OF LINKERLIST 15
1
2
3
4
5
1 2 3 4 5 ENTER THE LIMIT OF LINKEDLIST 2 5
6
7
8
3
5
6 7 8 3 5 THE CONCATENATED LIST IS:1 2 3 4 5 6 7 8 3 5