Skip to content

Commit 716070e

Browse files
author
Avinash Kushwaha
committed
Added Doubly_Linked_List under C and cleaned & organized the repo
1 parent d8f4005 commit 716070e

File tree

108 files changed

+141
-746
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+141
-746
lines changed

ARMSTRONG.exe

-44 KB
Binary file not shown.

2sumcpp renamed to C++/2sum.cpp

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.

count.cpp renamed to C++/count.cpp

File renamed without changes.

fact.cpp renamed to C++/fact.cpp

File renamed without changes.

game renamed to C++/game.cpp

File renamed without changes.
File renamed without changes.

hello.c++ renamed to C++/hello.c++

File renamed without changes.

kmp.cpp renamed to C++/kmp.cpp

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

C/Doubly_Linked_List.c

+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
typedef struct Node {
5+
int data;
6+
struct Node* next;
7+
struct Node* prev;
8+
} Node;
9+
10+
Node* head = NULL;
11+
12+
// Function to create a new node
13+
Node* createNode(int data) {
14+
Node* newNode = (Node*)malloc(sizeof(Node));
15+
newNode->data = data;
16+
newNode->next = NULL;
17+
newNode->prev = NULL;
18+
return newNode;
19+
}
20+
21+
// Function to add a node at the beginning
22+
void addNodeAtBeginning(int data) {
23+
Node* newNode = createNode(data);
24+
newNode->next = head;
25+
if (head != NULL) {
26+
head->prev = newNode;
27+
}
28+
head = newNode;
29+
}
30+
31+
// Function to add a node at the end
32+
void addNodeAtEnd(int data) {
33+
Node* newNode = createNode(data);
34+
if (head == NULL) {
35+
head = newNode;
36+
return;
37+
}
38+
Node* temp = head;
39+
while (temp->next != NULL) {
40+
temp = temp->next;
41+
}
42+
temp->next = newNode;
43+
newNode->prev = temp;
44+
}
45+
46+
// Function to add a node at a specific position
47+
void addNodeAtPosition(int data, int position) {
48+
if (position == 0) {
49+
addNodeAtBeginning(data);
50+
return;
51+
}
52+
53+
Node* newNode = createNode(data);
54+
Node* temp = head;
55+
for (int i = 0; temp != NULL && i < position - 1; i++) {
56+
temp = temp->next;
57+
}
58+
59+
if (temp == NULL) {
60+
printf("Position out of bounds.\n");
61+
free(newNode);
62+
return;
63+
}
64+
65+
newNode->next = temp->next;
66+
newNode->prev = temp;
67+
if (temp->next != NULL) {
68+
temp->next->prev = newNode;
69+
}
70+
temp->next = newNode;
71+
}
72+
73+
// Function to display the list
74+
void displayList() {
75+
Node* temp = head;
76+
if (temp == NULL) {
77+
printf("List is empty.\n");
78+
return;
79+
}
80+
while (temp != NULL) {
81+
printf("%d ", temp->data);
82+
temp = temp->next;
83+
}
84+
printf("\n");
85+
}
86+
87+
// Main function for the CLI
88+
int main() {
89+
int choice, data, position;
90+
91+
while (1) {
92+
printf("1. Add Node at Beginning\n");
93+
printf("2. Add Node at End\n");
94+
printf("3. Add Node at Position\n");
95+
printf("4. Display List\n");
96+
printf("5. Exit\n");
97+
printf("Enter your choice: ");
98+
scanf("%d", &choice);
99+
100+
switch (choice) {
101+
case 1:
102+
printf("Enter data for the new node: ");
103+
scanf("%d", &data);
104+
addNodeAtBeginning(data);
105+
break;
106+
107+
case 2:
108+
printf("Enter data for the new node: ");
109+
scanf("%d", &data);
110+
addNodeAtEnd(data);
111+
break;
112+
113+
case 3:
114+
printf("Enter data for the new node: ");
115+
scanf("%d", &data);
116+
printf("Enter position to add the node: ");
117+
scanf("%d", &position);
118+
addNodeAtPosition(data, position);
119+
break;
120+
121+
case 4:
122+
displayList();
123+
break;
124+
125+
case 5:
126+
printf("Exiting program.\n");
127+
// Free the list before exiting (optional)
128+
while (head != NULL) {
129+
Node* temp = head;
130+
head = head->next;
131+
free(temp);
132+
}
133+
exit(0);
134+
135+
default:
136+
printf("Invalid choice. Please try again.\n");
137+
}
138+
}
139+
140+
return 0;
141+
}
File renamed without changes.
File renamed without changes.

FACTORIAL.exe

-44.1 KB
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.

Majority Element.exe

-211 KB
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.
-293 KB
Binary file not shown.
File renamed without changes.
File renamed without changes.

fact.exe

-55.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)