chore(C): add reverse and merge linked lists (#340)

pull/354/head
Rahul V 2021-06-10 18:42:08 +05:30 committed by GitHub
parent 6ff3a4b887
commit 462400bb01
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 322 additions and 0 deletions

View File

@ -11,6 +11,8 @@
- [Insert and Delete at Beginning](linked-lists/Insert-and-delete-beginning.c)
- [Josephus Problem](linked-lists/josephus-problem.c)
- [Circular Linked List](linked-lists/Insert-and-del-beginning-circular-ll.c)
- [Merge two Linked Lists](linked-lists/merge-two-linkedlists.c)
- [Reverse a Linked List](linked-lists/reverse-linkedlists.c)
## Queues
- [Double Ended Queue using array](queues/double-ended-queue-using-array.c)

View File

@ -0,0 +1,172 @@
//Merging two Linked-List
#include<stdio.h>
#include<stdlib.h>
/*Structure definition:
data : holds the integer value of the node
next : holds the pointer to the next node
*/
struct node
{
int data;
struct node *next;
};
/*Function to create a linked-list of given size with passed head pointer.
head - pointer to the first node of the linked-list
size - number of nodes in the linked-list
Return value - Returns a pointer, pointing to the head node of the linked-list
*/
struct node* createList(struct node* head,int size)
{
struct node* prev = NULL;
//To store value (user-input)
int a;
for(int i=1;i<=size;i++){
//If head node
if(i==1){
head = (struct node*)malloc(sizeof(struct node)); //Allocating memory
if(head!=NULL){
printf("\nEnter a value: ");
scanf("%d",&a);
head->data = a;
head->next = NULL;
prev = head;
}
//If memory cannot be allocated
else{
printf("\nCannot allocate memory!");
return head;
}
}
else{
struct node* current = NULL;
current = (struct node*)malloc(sizeof(struct node));
if(current!=NULL){
printf("\nEnter a value: ");
scanf("%d",&a);
current->data = a;
current->next = NULL;
prev->next = current;
prev = current;
}
//If memory cannot be allocated
else{
printf("\nCannot allocate memory!");
return head;
}
}
}
printf("\n");
return head;
}
//Function to display a linked-list from the passed head pointer
void display(struct node* head)
{
struct node *temp = head;
while(temp!=NULL)
{
printf("%d -> ",temp->data);
temp=temp->next;
}
printf("\n\n");
}
/*Function to merge linked-lists of given order.
a_list - head pointer of List A
b_list - head pointer of List B
first - number specifying the order of merging
if first==1:
Order of merging will be List A -> List B
if first==2:
Order of merging will be List B -> List A
Return value - Returns a pointer, pointing to the head node of the merged linked-list
*/
struct node* merge(struct node* a_list,struct node* b_list,int first)
{
struct node* temp1;
struct node* temp2;
if(first==1){
temp1 = a_list;
temp2 = b_list;
}
else{
temp1 = b_list;
temp2 = a_list;
}
while(temp1->next!=NULL)
{
temp1=temp1->next;
}
temp1->next=temp2;
return (first==1)?a_list:b_list;
}
//Driver function
int main()
{
//Our two linked-lists
struct node* lista = NULL;
struct node* listb = NULL;
int sizeOfListA = 0;
int sizeOfListB = 0;
//Getting the sizes of the linked-list
while(sizeOfListA<1 || sizeOfListB<1){
printf("\nEnter the size of List A: ");
scanf("%d",&sizeOfListA);
printf("Enter the size of List B: ");
scanf("%d",&sizeOfListB);
if(sizeOfListA<1 || sizeOfListB<1) printf("\n\tSize cannot be less than 1. Enter a valid size!\n\n");
}
//Displaying List A
printf("\nFor List A: ");
lista = createList(lista,sizeOfListA);
printf("\nList A: \n");
display(lista);
//Displaying List B
printf("\nFor List B: ");
listb = createList(listb,sizeOfListB);
printf("\nList B: \n");
display(listb);
//Checking whether both the linked-lists are not NULL
if(lista!=NULL && listb!=NULL){
//Getting the order of merging
int option;
while(option!=1 && option!=2){
printf("\nOptions for Merging:\n\n\t1. List A -> List B (Enter 1)\n\t2. List B -> List A (Enter 2)\n");
printf("\nEnter the option: ");
scanf("%d",&option);
if(option!=1 && option!=2) printf("\n\tInvalid option!\n");
}
//Merging the two linked-lists
struct node* output = merge(lista,listb,option);
printf("\nAfter Merging:\n\n");
display(output);
}
printf("\n");
//De-allocating the memory
free(lista);
free(listb);
return 0;
}

View File

@ -0,0 +1,148 @@
//Reversing a linked-list
#include<stdio.h>
#include<stdlib.h>
/*Structure definition
data : holds the integer value of node
next : holds the pointer to the next node
*/
struct node
{
int data;
struct node *next;
};
/*Function to create a linked-list of given size with passed head pointer.
head - pointer to the first node of the linked-list
size - number of nodes in the linked-list
Return value - Returns a pointer, pointing to the head node of the linked-list
*/
struct node* createList(struct node* head,int size)
{
struct node* prev = NULL;
//To store value (user-input)
int a;
for(int i=1;i<=size;i++){
//If head node
if(i==1){
head = (struct node*)malloc(sizeof(struct node)); //Allocating memory
if(head!=NULL){
printf("\nEnter a value: ");
scanf("%d",&a);
head->data = a;
head->next = NULL;
prev = head;
}
//If memory cannot be allocated
else{
printf("\nCannot allocate memory!");
return head;
}
}
else{
struct node* current = NULL;
current = (struct node*)malloc(sizeof(struct node));
if(current!=NULL){
printf("\nEnter a value: ");
scanf("%d",&a);
current->data = a;
current->next = NULL;
prev->next = current;
prev = current;
}
//If memory cannot be allocated
else{
printf("\nCannot allocate memory!");
return head;
}
}
}
printf("\n");
return head;
}
/*Function to reverse a linked-list.
head - pointer, pointing to the head of the linked-list that we need to reverse
Return value - pointer, pointing to the head of the reversed linked-list
*/
struct node* reverseList(struct node* head){
/*We will need three pointers
previous : to keep track of the node previous to present node
present : to keep track of the present node
after : to keep track of the next node
Initially we will be in the present in the head (first) node
*/
struct node* previous = NULL;
struct node* present = head;
struct node* after = NULL;
while (present != NULL) {
//Making 'after' node, point to the next node of 'present' node
after = present->next;
// Making 'present' node, point to the previous node (REVERSING)
present->next = previous;
//Going one step further in the linked-list
previous = present;
present = after;
}
/*After the loop termination, the 'head' will point to the last node of the linked-list.
All other nodes will also be reversed.
*/
head = previous;
return head;
}
//Function to display a linked-list from the passed head pointer
void display(struct node* head)
{
struct node *temp = head;
while(temp!=NULL)
{
printf("%d -> ",temp->data);
temp=temp->next;
}
printf("\n\n");
}
//Driver function
int main()
{
//Our linked-list
struct node* list = NULL;
int n = 0;
//Getting the size of the linked-list
while(n<1){
printf("\nEnter the number of nodes: ");
scanf("%d",&n);
if(n<1) printf("\n\tSize cannot be less than 1. Enter a valid input!\n\n");
}
printf("\n");
//Creating the linked-list of given size (n)
list = createList(list,n);
//Making sure the linked-list is created.
if(list!=NULL){
//Original list
printf("\nOriginal Linked-list: ");
display(list);
//Reversing the linked-list
list = reverseList(list);
printf("\nReversed Linked-List: ");
display(list);
}
printf("\n");
//De-allocating memory
free(list);
return 0;
}