By syed Dayim shah

This commit is contained in:
Syed Dayim Shah 2023-03-18 16:49:59 +05:30 committed by GitHub
parent ec7ec8902b
commit 7895e64b36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 167 additions and 0 deletions

124
LinkedList.h Normal file
View File

@ -0,0 +1,124 @@
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
typedef struct LinkedList {
Node *head;
int size;
} LinkedList;
LinkedList *createLinkedList() {
LinkedList *list = (LinkedList *) malloc(sizeof(LinkedList));
list->head = NULL;
list->size = 0;
return list;
}
void addFirst(LinkedList *list, int data) {
Node *node = (Node *) malloc(sizeof(Node));
node->data = data;
node->next = list->head;
list->head = node;
list->size++;
}
void deleteFirst(LinkedList *list) {
if (list->head != NULL) {
Node *temp = list->head;
list->head = list->head->next;
free(temp);
list->size--;
}
}
void addRandom(LinkedList *list, int data) {
int index = rand() % (list->size + 1);
if (index == 0) {
addFirst(list, data);
} else {
Node *prev = NULL;
Node *curr = list->head;
for (int i = 0; i < index; i++) {
prev = curr;
curr = curr->next;
}
Node *node = (Node *) malloc(sizeof(Node));
node->data = data;
node->next = curr;
prev->next = node;
list->size++;
}
}
void deleteRandom(LinkedList *list, int data) {
if (list->size == 0) {
return;
}
if (list->head->data == data) {
deleteFirst(list);
} else {
Node *prev = NULL;
Node *curr = list->head;
while (curr != NULL && curr->data != data) {
prev = curr;
curr = curr->next;
}
if (curr != NULL) {
prev->next = curr->next;
free(curr);
list->size--;
}
}
}
void addEnd(LinkedList *list, int data) {
Node *node = (Node *) malloc(sizeof(Node));
node->data = data;
node->next = NULL;
if (list->head == NULL) {
list->head = node;
} else {
Node *temp = list->head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = node;
}
list->size++;
}
void deleteEnd(LinkedList *list) {
if (list->size == 0) {
return;
}
if (list->head->next == NULL) {
free(list->head);
list->head = NULL;
} else {
Node *prev = NULL;
Node *curr = list->head;
while (curr->next != NULL) {
prev = curr;
curr = curr->next;
}
free(curr);
prev->next = NULL;
}
list->size--;
}
int peek(LinkedList *list) {
if (list->head != NULL) {
return list->head->data;
} else {
return -1;
}
}
#endif

43
main.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
#include "LinkedList.h"
int main() {
LinkedList *list = createLinkedList();
addFirst(list, 10);
addFirst(list, 20);
addFirst(list, 30);
printf("List after adding 3 elements at first:\n");
peek(list);
deleteFirst(list);
printf("List after deleting first element:\n");
peek(list);
addRandom(list, 40);
printf("List after adding element at random:\n");
peek(list);
deleteRandom(list, 20);
printf("List after deleting element at random:\n");
peek(list);
addEnd(list, 50);
printf("List after adding element at end:\n");
peek(list);
deleteEnd(list);
printf("List after deleting element at end:\n");
peek(list);
printf("Peek element of the list: %d\n", peek(list));
return 0;
}