From 56bd2f35d293c18a19bce154bc17a2379d3b342e Mon Sep 17 00:00:00 2001 From: Tushar Sublaik <99117094+tushar-sublaik@users.noreply.github.com> Date: Sat, 22 Oct 2022 11:18:22 +0530 Subject: [PATCH 1/2] Create linked-list-code.c Linked list code in C Language. --- Programming_Languages/C/linked-list-code.c | 118 +++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 Programming_Languages/C/linked-list-code.c diff --git a/Programming_Languages/C/linked-list-code.c b/Programming_Languages/C/linked-list-code.c new file mode 100644 index 00000000..96c0e987 --- /dev/null +++ b/Programming_Languages/C/linked-list-code.c @@ -0,0 +1,118 @@ +#include +#include + +typedef struct node +{ + int data; + struct node *next; +}node; + +node* newnode(struct node* new,int n) +{ + struct node* temp; + temp=(struct node*)malloc(sizeof(struct node)); + temp->data=n; + temp->next=NULL; + if(new==NULL) + { + new=temp; + } + else + { + while(new->next!=NULL) + { + new=new->next; + } + new->next=temp; + } +} +void printlist(struct node* head) +{ + head = head->next; + while (head != NULL) { + printf("%d ", head->data); + if (head->next) + printf("-> "); + head = head->next; + } + printf("\n"); +} + +// Case 1: Deleting the first element from the linked list +node * deleteFirst(struct node * head){ + struct node * ptr = head; + head = head->next; + free(ptr); + return head; +} + +// Case 2: Deleting the element at a given index from the linked list +node * deleteAtIndex(struct node * head, int index){ + struct node *p = head; + struct node *q = head->next; + for (int i = 0; i < index-1; i++) + { + p = p->next; + q = q->next; + } + + p->next = q->next; + free(q); + return head; +} + +// Case 3: Deleting the last element +node * deleteLast(struct node * head){ + struct node *p = head; + struct node *q = head->next; + while(q->next !=NULL) + { + p = p->next; + q = q->next; + } + + p->next = NULL; + free(q); + return head; +} + +int main() +{ + int a,data,i,j,x; + node* head; + printf("\nEner the size of the linked list: "); + scanf("%d",&a); + printf("\nEnter data in the linked list: "); + for(i=1;i<=a;i++) + { + scanf("%d",&data); + newnode(head,data); + } + printf("\nLinked list before deletion :\n"); + printlist(head); + printf("\nChoose out of given below options :\n"); + printf("Press '1' to Deleting the first element from the linked list\n"); + printf("Press '2' to Deleting the element at a given index from the linked list\n"); + printf("Press '3' to Deleting the last element from the linked list\n"); + printf("\nEnter the option: "); + scanf("%d",&x); + switch (x) + { + case 1: head = deleteFirst(head); + printf("\nLinked list after deletion:\n"); + printlist(head); + break; + case 2: printf("Enter the index position: "); + scanf("%d",&j); + head = deleteAtIndex(head, j); + printf("\nLinked list after deletion:\n"); + printlist(head); + break; + case 3: head = deleteLast(head); + printf("\nLinked list after deletion:\n"); + printlist(head); + break; + default: printf("\nWrong Input !!!\n");break; + } + return 0; +} From 4492bd83a8a3cc0d3717e7fb05d9abf6233f6c0e Mon Sep 17 00:00:00 2001 From: Tushar Sublaik <99117094+tushar-sublaik@users.noreply.github.com> Date: Sat, 22 Oct 2022 12:46:31 +0530 Subject: [PATCH 2/2] Create Hacktoberfest-topic.md --- Hacktoberfest-topic.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Hacktoberfest-topic.md diff --git a/Hacktoberfest-topic.md b/Hacktoberfest-topic.md new file mode 100644 index 00000000..abd1291e --- /dev/null +++ b/Hacktoberfest-topic.md @@ -0,0 +1,2 @@ +Label:hactoberfest-accepted +the hacktoberfest topic is added