forked from ghostmkg/programming-language
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Doubly_Linked_List under C and cleaned & organized the repo
- Loading branch information
Avinash Kushwaha
committed
Oct 11, 2024
1 parent
d8f4005
commit 716070e
Showing
108 changed files
with
141 additions
and
746 deletions.
There are no files selected for viewing
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,141 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct Node { | ||
int data; | ||
struct Node* next; | ||
struct Node* prev; | ||
} Node; | ||
|
||
Node* head = NULL; | ||
|
||
// Function to create a new node | ||
Node* createNode(int data) { | ||
Node* newNode = (Node*)malloc(sizeof(Node)); | ||
newNode->data = data; | ||
newNode->next = NULL; | ||
newNode->prev = NULL; | ||
return newNode; | ||
} | ||
|
||
// Function to add a node at the beginning | ||
void addNodeAtBeginning(int data) { | ||
Node* newNode = createNode(data); | ||
newNode->next = head; | ||
if (head != NULL) { | ||
head->prev = newNode; | ||
} | ||
head = newNode; | ||
} | ||
|
||
// Function to add a node at the end | ||
void addNodeAtEnd(int data) { | ||
Node* newNode = createNode(data); | ||
if (head == NULL) { | ||
head = newNode; | ||
return; | ||
} | ||
Node* temp = head; | ||
while (temp->next != NULL) { | ||
temp = temp->next; | ||
} | ||
temp->next = newNode; | ||
newNode->prev = temp; | ||
} | ||
|
||
// Function to add a node at a specific position | ||
void addNodeAtPosition(int data, int position) { | ||
if (position == 0) { | ||
addNodeAtBeginning(data); | ||
return; | ||
} | ||
|
||
Node* newNode = createNode(data); | ||
Node* temp = head; | ||
for (int i = 0; temp != NULL && i < position - 1; i++) { | ||
temp = temp->next; | ||
} | ||
|
||
if (temp == NULL) { | ||
printf("Position out of bounds.\n"); | ||
free(newNode); | ||
return; | ||
} | ||
|
||
newNode->next = temp->next; | ||
newNode->prev = temp; | ||
if (temp->next != NULL) { | ||
temp->next->prev = newNode; | ||
} | ||
temp->next = newNode; | ||
} | ||
|
||
// Function to display the list | ||
void displayList() { | ||
Node* temp = head; | ||
if (temp == NULL) { | ||
printf("List is empty.\n"); | ||
return; | ||
} | ||
while (temp != NULL) { | ||
printf("%d ", temp->data); | ||
temp = temp->next; | ||
} | ||
printf("\n"); | ||
} | ||
|
||
// Main function for the CLI | ||
int main() { | ||
int choice, data, position; | ||
|
||
while (1) { | ||
printf("1. Add Node at Beginning\n"); | ||
printf("2. Add Node at End\n"); | ||
printf("3. Add Node at Position\n"); | ||
printf("4. Display List\n"); | ||
printf("5. Exit\n"); | ||
printf("Enter your choice: "); | ||
scanf("%d", &choice); | ||
|
||
switch (choice) { | ||
case 1: | ||
printf("Enter data for the new node: "); | ||
scanf("%d", &data); | ||
addNodeAtBeginning(data); | ||
break; | ||
|
||
case 2: | ||
printf("Enter data for the new node: "); | ||
scanf("%d", &data); | ||
addNodeAtEnd(data); | ||
break; | ||
|
||
case 3: | ||
printf("Enter data for the new node: "); | ||
scanf("%d", &data); | ||
printf("Enter position to add the node: "); | ||
scanf("%d", &position); | ||
addNodeAtPosition(data, position); | ||
break; | ||
|
||
case 4: | ||
displayList(); | ||
break; | ||
|
||
case 5: | ||
printf("Exiting program.\n"); | ||
// Free the list before exiting (optional) | ||
while (head != NULL) { | ||
Node* temp = head; | ||
head = head->next; | ||
free(temp); | ||
} | ||
exit(0); | ||
|
||
default: | ||
printf("Invalid choice. Please try again.\n"); | ||
} | ||
} | ||
|
||
return 0; | ||
} |
File renamed without changes.
File renamed without changes.
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.