Skip to content

Commit

Permalink
list update
Browse files Browse the repository at this point in the history
  • Loading branch information
sujan-poudel-03 committed Jun 4, 2023
1 parent 5d747f7 commit 76827c5
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 0 deletions.
28 changes: 28 additions & 0 deletions C/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\MinGW\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
85 changes: 85 additions & 0 deletions C/list/array_implementation_of_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <stdio.h>

#define MAX_SIZE 100

struct List {
int arr[MAX_SIZE];
int size;
};

void initList(struct List* list) {
list->size = 0;
}

void insert(struct List* list, int data, int position) {
if (position < 0 || position > list->size) {
printf("Invalid position!\n");
return;
}

if (list->size == MAX_SIZE) {
printf("List is full!\n");
return;
}

// Shift elements to the right to make space for the new element
for (int i = list->size; i > position; i--) {
list->arr[i] = list->arr[i - 1];
}

list->arr[position] = data;
list->size++;
}

void removeElement(struct List* list, int position) {
if (position < 0 || position >= list->size) {
printf("Invalid position!\n");
return;
}

// Shift elements to the left to remove the element at the given position
for (int i = position; i < list->size - 1; i++) {
list->arr[i] = list->arr[i + 1];
}

list->size--;
}

void update(struct List* list, int data, int position) {
if (position < 0 || position >= list->size) {
printf("Invalid position!\n");
return;
}

list->arr[position] = data;
}

void display(struct List* list) {
printf("List: ");
for (int i = 0; i < list->size; i++) {
printf("%d ", list->arr[i]);
}
printf("\n");
}

int main() {
struct List myList;

initList(&myList);

insert(&myList, 5, 0); // Insert 5 at position 0
insert(&myList, 10, 1); // Insert 10 at position 1
insert(&myList, 15, 1); // Insert 15 at position 1

display(&myList); // Output: List: 5 15 10

update(&myList, 20, 1); // Update element at position 1 to 20

display(&myList); // Output: List: 5 20 10

removeElement(&myList, 0); // Remove element at position 0

display(&myList); // Output: List: 20 10

return 0;
}
85 changes: 85 additions & 0 deletions C/list/queue_as_a_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

struct Queue {
struct Node* front;
struct Node* rear;
};

struct Queue* createQueue() {
struct Queue* queue = (struct Queue*)malloc(sizeof(struct Queue));
queue->front = NULL;
queue->rear = NULL;
return queue;
}

void enqueue(struct Queue* queue, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;

if (queue->rear == NULL) {
queue->front = newNode;
queue->rear = newNode;
} else {
queue->rear->next = newNode;
queue->rear = newNode;
}

printf("%d enqueued to the queue.\n", data);
}

void dequeue(struct Queue* queue) {
if (queue->front == NULL) {
printf("Queue is empty. Cannot dequeue.\n");
return;
}

struct Node* temp = queue->front;
queue->front = queue->front->next;

if (queue->front == NULL) {
queue->rear = NULL;
}

printf("%d dequeued from the queue.\n", temp->data);
free(temp);
}

void display(struct Queue* queue) {
if (queue->front == NULL) {
printf("Queue is empty.\n");
return;
}

struct Node* current = queue->front;
printf("Queue: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main() {
struct Queue* queue = createQueue();

enqueue(queue, 5); // Enqueue 5
enqueue(queue, 10); // Enqueue 10
enqueue(queue, 15); // Enqueue 15

display(queue); // Output: Queue: 5 10 15

dequeue(queue); // Dequeue
dequeue(queue); // Dequeue

display(queue); // Output: Queue: 15

return 0;

}
16 changes: 16 additions & 0 deletions Python/array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class array_implemenation:

def __init__(self, size):
self.size = size
self.arr = [None] * size

arr = [1, 2, 3, 4, 5]

def display():
for i in range(len(arr)):
print(arr[i], end=" ")
print()



display()

0 comments on commit 76827c5

Please sign in to comment.