feat: improve the Singly Link List implementation (#1092)

* Update singly_link_list_deletion.c

Delete function is updated to delete at any required position (not only first)

* Update singly_link_list_deletion.c

The code is changed as per the suggestions. Please lat me know if there are any changes to be done
Thank you

* Update singly_link_list_deletion.c

The code is changed as per the suggestions. Please let me know if there are any changes to be done
Thank you..

* Update singly_link_list_deletion.c

I added inserting at any input location . Please let me know if changes need to be done

* Update singly_link_list_deletion.c

* Update singly_link_list_deletion.c

I updated self tests for both insert and delete functions properly. Please let me know if any changes need to be done
Thank you

* Update singly_link_list_deletion.c

As per your suggestions I updated the code. Please let me know if any changes need to be done..

* chore: apply suggestions from code review

Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
ms3939 2023-01-06 04:58:08 +05:30 committed by GitHub
parent 1a5b5f522f
commit 3014b7352d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,39 +4,64 @@
when passed with a key of the node.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct node
{
int info;
struct node *link;
};
struct node *start = NULL;
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
struct node *createnode() // function to create node
{
struct node *t;
t = (struct node *)malloc(sizeof(struct node));
return (t);
}
////////////////////////////////////////////////////////
void insert() // function to insert at first location
//////////////////////////////////////////////////////////////////
int insert(int pos, int d)
{
struct node *p;
p = createnode();
printf("\nenter the number to insert");
scanf("%d", &p->info);
p->link = NULL;
struct node *new;
new = createnode();
new->info = d;
if (pos == 1)
{
new->link = NULL;
if (start == NULL)
{
start = p;
start = new;
}
else
{
p->link = start;
start = p;
new->link = start;
start = new;
}
}
///////////////////////////////////////////////////////////
void deletion() // function to delete from first position
}
else
{
struct node *pre = start;
for (int i = 2; i < pos; i++)
{
if (pre == NULL)
{
break;
}
pre = pre->link;
}
if(pre==NULL)
{
printf("Position not found!");
return 0;
}
new->link = pre->link;
pre->link = new;
}
return 0;
}
///////////////////////////////////////////////////////////////////
int deletion(int pos) // function to delete from any position
{
struct node *t;
if (start == NULL)
@ -44,14 +69,34 @@ void deletion() // function to delete from first position
printf("\nlist is empty");
}
else
{
if (pos == 1)
{
struct node *p;
p = start;
start = start->link;
free(p);
}
else
{
struct node *prev = start;
for (int i = 2; i < pos; i++)
{
if (prev == NULL)
{
printf("Position not found!");
return 0;
}
prev = prev->link;
}
struct node *n = prev->link; // n points to required node to be deleted
prev->link = n->link;
free(n);
}
}
return 0;
}
///////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
void viewlist() // function to display values
{
struct node *p;
@ -69,25 +114,52 @@ void viewlist() // function to display values
}
}
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
static void test()
{
insert(1, 39);
assert(start->info == 39);
insert(2, 10);
insert(3, 11);
deletion(1);
assert(start->info != 39);
printf("Self-tests successfully passed!\n");
}
//////////////////////////////////////////////////////////////////
int main()
{
int n;
int n = 0, pos = 0, p = 0, num = 0, c = 0;
printf("\n1.self test mode");
printf("\n2.interactive mode");
printf("\nenter your choice:");
scanf("%d", &c);
if (c == 1)
{
test();
}
else if (c == 2)
{
while (1)
{
printf("\n1.add value at first location");
printf("\n2.delete value from first location");
printf("\n3.view value");
printf("\nenter your choice");
printf("\n1.add value at the given location");
printf("\n2.delete value at the given location");
printf("\n3.view list");
printf("\nenter your choice :");
scanf("%d", &n);
switch (n)
{
case 1:
insert();
printf("enter the position where the element is to be added :");
scanf("%d", &p);
printf("enter the element is to be added :");
scanf("%d", &num);
insert(p, num);
break;
case 2:
deletion();
printf("enter the position where the element is to be deleted :");
scanf("%d", &pos);
deletion(pos);
break;
case 3:
viewlist();
@ -96,5 +168,10 @@ int main()
printf("\ninvalid choice");
}
}
return (0);
}
else
{
printf("Invalid choice");
}
return 0;
}