Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views2 pages

SLL C Program

Uploaded by

Sid1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

SLL C Program

Uploaded by

Sid1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <stdio.

h>
#include <conio.h> // for getch()
#include <stdlib.h> // for malloc()

struct node {
int data;
struct node *next;
};

struct node* create();


void display(struct node*);
struct node* merge(struct node*, struct node*);

struct node* create() {


int n, i;
struct node *s, *f;

printf("Enter how many nodes: ");


scanf("%d", &n);

f = (struct node*)malloc(sizeof(struct node)); // Allocate memory for the


first node
printf("Enter data: ");
scanf("%d", &f->data);

s = f; // Initialize s to point to the first node

for (i = 1; i < n; i++) {


s->next = (struct node*)malloc(sizeof(struct node)); // Allocate memory
for the next node
s = s->next; // Move the pointer to the newly created node
printf("Enter data: ");
scanf("%d", &s->data);
}
s->next = NULL; // Mark the end of the linked list

return f; // Return the head of the list


}

void display(struct node *f) {


struct node *s;

for (s = f; s != NULL; s = s->next) {


printf("| %d |-> ", s->data);
}
printf("NULL\n"); // End of list
}

struct node* merge(struct node *f1, struct node *f2) {


struct node *s;

for (s = f1; s->next != NULL; s = s->next) {


// Traverse to the end of the first list
}
s->next = f2; // Connect the end of the first list to the second list

return f1; // Return the head of the merged list


}
int main() {
struct node *f1, *f2, *f3;

clrscr(); // Clear screen (specific to Turbo C)

printf("Create 1st linked list:\n");


f1 = create();

printf("\nCreate 2nd linked list:\n");


f2 = create();

printf("\n1st linked list: ");


display(f1);

printf("\n2nd linked list: ");


display(f2);

f3 = merge(f1, f2);

printf("\nAfter merging, the linked list is: ");


display(f3);

getch(); // Wait for key press before exiting (specific to Turbo C)


return 0;
}

You might also like