From fce0d6fc72f542602cbd3d7385c96c8bd01d7f90 Mon Sep 17 00:00:00 2001 From: Nireesha7979 <81856769+Nireesha7979@users.noreply.github.com> Date: Wed, 12 Oct 2022 19:19:12 +0530 Subject: [PATCH 1/2] Add files via upload --- Linked List/single.c | 179 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) create mode 100644 Linked List/single.c diff --git a/Linked List/single.c b/Linked List/single.c new file mode 100644 index 0000000..bce8ff2 --- /dev/null +++ b/Linked List/single.c @@ -0,0 +1,179 @@ +#include +#include +#include +struct node{ + int key; + struct node *addr; +}; +struct node *front=NULL; +struct node *rear=NULL; +void display(){ + struct node *temp2; + temp2 = front; + while(temp2!=NULL){ + printf("%d\t",temp2->key); + temp2=temp2->addr; + } +} +void display_r(){ + int i,j,k; + struct node *temp,*temp2; + i=0; + temp2=front; + while(temp2!=NULL){ + i++; + temp2=temp2->addr; + } + j=i; + for(k=j;k>1;k--){ + temp=front; + i=0; + do{ + temp=temp->addr; + i++; + }while(ikey); + } + printf("%d",front->key); +} +void insert_at_begin(int key1){ + struct node *temp=(struct node*)malloc(sizeof(struct node)); + if(front==NULL){ + front=rear=temp; + temp->key=key1; + temp->addr=NULL; + } + else{ + temp->addr=front; + temp->key=key1; + front=temp; + } +} +void insert_at_end(int key1){ + struct node *temp=(struct node*)malloc(sizeof(struct node)); + if(front==NULL || rear==NULL) { + temp->key=key1; + temp->addr=NULL; + front=temp; + rear=temp; + } + else { + rear->addr=temp; + temp->key=key1; + temp->addr=NULL; + rear=temp; + } +} +void insert_at_pos(int key1,int pos){ + struct node *temp=(struct node*)malloc(sizeof(struct node)); + struct node *temp1; + int i; + temp->key=key1; + temp1=front; + if(front==NULL || rear==NULL) { + front=temp; + rear=temp; + } + else { + for(i=0;iaddr; + } + temp->addr=temp1->addr; + temp1->addr=temp; + } +} +void delete_at_begin(){ + if(front==NULL && rear==NULL){ + printf("list is empty"); + } + else if(front==rear){ + free(front); + front=rear=NULL; + } + else{ + struct node*temp; + temp=front->addr; + front=temp; + } +} +void delete_at_end() +{ + struct node*temp; + if(front==NULL){ + printf("list is empty"); + } + else{ + temp=front; + while(temp->addr!=rear){ + temp=temp->addr; + } + printf("deleted element is %d",rear->key); + temp->addr=NULL; + rear=temp; + } +} +void delete_at_pos(int pos){ + struct node *temp,*temp1; + int i; + if(front==NULL){ + printf("list is empty"); + } + else if(front==rear){ + printf("deleted element is\t%d",front->key); + front=NULL; + rear=NULL; + } + else{ + temp1=front; + for(i=1;iaddr; + } + temp=temp1->addr; + temp1->addr=temp->addr; + temp->addr=NULL; + free(temp); + } +} +int main(){ + int n,n1,key1,pos,pos1,n2; + do{ + printf("\nselect an option:1.Insert\t2.delete\t3.display\t4.display reverse\t5.exit"); + scanf("%d",&n); + switch(n){ + case 1: printf("\nenter element"); + scanf("%d",&key1); + printf("\nselect where to insert?:1.begin\t2.end\t3.pos"); + scanf("%d",&n1); + switch(n1){ + case 1: insert_at_begin(key1); + break; + case 2: insert_at_end(key1); + break; + case 3: printf("\nenter position to insert?:"); + scanf("%d",&pos); + insert_at_pos(key1,pos); + break; + } + break; + case 2: printf("\nselect option where to delete?:1.begin\t2.end\t3.pos"); + scanf("%d",&n2); + switch(n2){ + case 1: delete_at_begin(); + break; + case 2: delete_at_end(); + break; + case 3: printf("\nenter position where to delete"); + scanf("%d",&pos1); + delete_at_pos(pos1); + break; + } + break; + case 3: display(); + break; + case 4: display_r(); + break; + case 5: exit(0); + default : printf("\nselect valid option"); + } + }while(1); +} \ No newline at end of file From c78813bab4d4f1c93c702798b0114db3619b1b0c Mon Sep 17 00:00:00 2001 From: Nireesha7979 <81856769+Nireesha7979@users.noreply.github.com> Date: Wed, 12 Oct 2022 19:26:49 +0530 Subject: [PATCH 2/2] All functions that can be performed in linked list 1.Insertion 2.Deletion 3.Display (At different possible positions) --- Linked List/{single.c => singleLinkedList.c} | 21 +++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) rename Linked List/{single.c => singleLinkedList.c} (84%) diff --git a/Linked List/single.c b/Linked List/singleLinkedList.c similarity index 84% rename from Linked List/single.c rename to Linked List/singleLinkedList.c index bce8ff2..cf5afcb 100644 --- a/Linked List/single.c +++ b/Linked List/singleLinkedList.c @@ -1,12 +1,16 @@ #include #include #include +//creating a structure node struct node{ int key; struct node *addr; }; + struct node *front=NULL; struct node *rear=NULL; + +//Function to display the linked list items in order void display(){ struct node *temp2; temp2 = front; @@ -15,6 +19,8 @@ void display(){ temp2=temp2->addr; } } + +//Function to display the linked list items in reverse order void display_r(){ int i,j,k; struct node *temp,*temp2; @@ -36,6 +42,8 @@ void display_r(){ } printf("%d",front->key); } + +//Function to insert element at beginning of linked list void insert_at_begin(int key1){ struct node *temp=(struct node*)malloc(sizeof(struct node)); if(front==NULL){ @@ -49,6 +57,8 @@ void insert_at_begin(int key1){ front=temp; } } + +//Function to insert element at th end of linked list void insert_at_end(int key1){ struct node *temp=(struct node*)malloc(sizeof(struct node)); if(front==NULL || rear==NULL) { @@ -64,6 +74,8 @@ void insert_at_end(int key1){ rear=temp; } } + +//Function to insert the element at some position of the linked list void insert_at_pos(int key1,int pos){ struct node *temp=(struct node*)malloc(sizeof(struct node)); struct node *temp1; @@ -82,6 +94,8 @@ void insert_at_pos(int key1,int pos){ temp1->addr=temp; } } + +//Function to delete element at beginning of linked list void delete_at_begin(){ if(front==NULL && rear==NULL){ printf("list is empty"); @@ -96,6 +110,8 @@ void delete_at_begin(){ front=temp; } } + +//Function to delete element at the end of linked list void delete_at_end() { struct node*temp; @@ -112,6 +128,8 @@ void delete_at_end() rear=temp; } } + +//Function to delete element at some position of linked list void delete_at_pos(int pos){ struct node *temp,*temp1; int i; @@ -144,6 +162,7 @@ int main(){ scanf("%d",&key1); printf("\nselect where to insert?:1.begin\t2.end\t3.pos"); scanf("%d",&n1); + //Switch case to choose inserting (or) deleting (or) displaying element in linked list switch(n1){ case 1: insert_at_begin(key1); break; @@ -176,4 +195,4 @@ int main(){ default : printf("\nselect valid option"); } }while(1); -} \ No newline at end of file +}