-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added singly linked list operations with insert, delete, count, and display functions. * Added singly circular linked list operations with insert, delete, count, and display functions. * Added doubly linear linked list operations with insert, delete, count, and display functions. * Added doubly Circular linked list operations with insert, delete, count, and display functions. * contains Linear and binary search algorithms * Program to sort elements of linkedlist using bubble sort * Program to sort elements of linkedlist using selection sort * Program to sort elements of linkedlist using Insertion sort * Program to sort elements of linkedlist using Insertion sort * Program to reverse linked list * Program to check wether linked list contains loop
- Loading branch information
1 parent
d95d4f8
commit 798a7f1
Showing
10 changed files
with
1,835 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
#pragma pack(1) | ||
|
||
struct node | ||
{ | ||
int iData; | ||
struct node *next; | ||
}; | ||
|
||
typedef struct node NODE; | ||
typedef struct node * PNODE; | ||
typedef struct node ** PPNODE; | ||
|
||
void Display(PNODE First) | ||
{ | ||
printf("Elements in Linked list are:\n"); | ||
|
||
while(First != NULL) | ||
{ | ||
printf("| %d | -> ", First->iData); | ||
First = First->next; | ||
} | ||
printf("NULL\n"); | ||
} | ||
|
||
void InsertLast(PPNODE First, int iNo) | ||
{ | ||
PNODE Temp = *First; | ||
PNODE newn = (PNODE)malloc(sizeof(NODE)); | ||
newn->iData = iNo; | ||
newn->next = NULL; | ||
|
||
if(*First == NULL) | ||
{ | ||
*First = newn; | ||
} | ||
else | ||
{ | ||
while(Temp->next != NULL) | ||
{ | ||
Temp = Temp->next; | ||
} | ||
Temp->next = newn; | ||
} | ||
} | ||
|
||
// Function to count the number of nodes in the linked list | ||
int CountNodes(PNODE First) | ||
{ | ||
int iCnt = 0; | ||
while (First != NULL) | ||
{ | ||
iCnt++; | ||
First = First->next; | ||
} | ||
return iCnt; | ||
} | ||
|
||
// Bubble sort function to sort the linked list | ||
void BubbleSort(PPNODE First) | ||
{ | ||
int i, j, iCnt; | ||
PNODE Temp = NULL; | ||
PNODE Current = NULL; | ||
int tempData; | ||
|
||
iCnt = CountNodes(*First); | ||
|
||
// Perform the Bubble Sort | ||
for(i = 0; i < iCnt - 1; i++) | ||
{ | ||
Current = *First; | ||
for(j = 0; j < iCnt - i - 1; j++) | ||
{ | ||
if(Current->iData > Current->next->iData) // Swap if elements are in wrong order | ||
{ | ||
// Swap the data | ||
tempData = Current->iData; | ||
Current->iData = Current->next->iData; | ||
Current->next->iData = tempData; | ||
} | ||
Current = Current->next; | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
PNODE Head = NULL; | ||
int iRet = 0; | ||
|
||
// Inserting elements in sorted order | ||
InsertLast(&Head, 121); | ||
InsertLast(&Head, 131); | ||
InsertLast(&Head, 181); | ||
InsertLast(&Head, 116); | ||
InsertLast(&Head, 141); | ||
InsertLast(&Head, 171); | ||
InsertLast(&Head, 151); | ||
InsertLast(&Head, 161); | ||
InsertLast(&Head, 111); | ||
|
||
printf("Before Sorting:\n"); | ||
Display(Head); | ||
|
||
BubbleSort(&Head); | ||
|
||
printf("\nAfter Sorting:\n"); | ||
Display(Head); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
|
||
#pragma pack(1) | ||
|
||
struct node | ||
{ | ||
int iData; | ||
struct node *next; | ||
}; | ||
|
||
typedef struct node NODE; | ||
typedef struct node * PNODE; | ||
typedef struct node ** PPNODE; | ||
|
||
void Display(PNODE First) | ||
{ | ||
printf("Elements in Linked list are:\n"); | ||
|
||
while(First != NULL) | ||
{ | ||
printf("| %d | -> ", First->iData); | ||
First = First->next; | ||
} | ||
printf("NULL\n"); | ||
} | ||
|
||
void InsertLast(PPNODE First, int iNo) | ||
{ | ||
PNODE Temp = *First; | ||
PNODE newn = (PNODE)malloc(sizeof(NODE)); | ||
newn->iData = iNo; | ||
newn->next = NULL; | ||
|
||
if(*First == NULL) | ||
{ | ||
*First = newn; | ||
} | ||
else | ||
{ | ||
while(Temp->next != NULL) | ||
{ | ||
Temp = Temp->next; | ||
} | ||
Temp->next = newn; | ||
} | ||
} | ||
bool LoopCheck(PNODE First) | ||
{ | ||
PNODE Student = First; | ||
PNODE Teacher = First; | ||
bool flag = false; | ||
|
||
while((Teacher != NULL) && (Teacher->next != NULL)) | ||
{ | ||
Student = Student -> next; | ||
Teacher = Teacher->next->next; | ||
|
||
if(Student == Teacher) | ||
{ | ||
flag = true; | ||
break; | ||
} | ||
} | ||
|
||
return flag; | ||
} | ||
|
||
int main() | ||
{ | ||
PNODE Head = NULL; | ||
bool bRet = 0; | ||
|
||
// Inserting elements in sorted order | ||
InsertLast(&Head, 121); | ||
InsertLast(&Head, 131); | ||
InsertLast(&Head, 181); | ||
InsertLast(&Head, 116); | ||
InsertLast(&Head, 141); | ||
InsertLast(&Head, 171); | ||
InsertLast(&Head, 151); | ||
InsertLast(&Head, 161); | ||
InsertLast(&Head, 111); | ||
|
||
Display(Head); | ||
|
||
bRet = LoopCheck(Head); | ||
|
||
if(bRet == true) | ||
{ | ||
printf("There is loop\n"); | ||
} | ||
else | ||
{ | ||
printf("There is no loop\n"); | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.