Skip to content

Instantly share code, notes, and snippets.

@thecoducer
Last active August 30, 2017 14:11
Show Gist options
  • Save thecoducer/e4cc7497837cc2ba2ddd6c05e2fd956b to your computer and use it in GitHub Desktop.
Save thecoducer/e4cc7497837cc2ba2ddd6c05e2fd956b to your computer and use it in GitHub Desktop.
//Language:- C
#include<stdio.h>
#include<stdlib.h>
typedef struct node //globally declared structure
{
int data;
struct node *next; //self-referencing pointer, to know more refer C programming book by D. Ritchie
}node;
void main()
{
int choice, data, reply, position;
node *head;
head=NULL; //Empty Linked List created
do
{
printf("\n1. Insert an element.");
printf("\n2. Delete an element.");
printf("\n3. Display the contents of the Linked List.");
printf("\n4. Delete the whole list.");
printf("\n5. Exit.");
printf("\nEnter your choice:- ");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
printf("\nEnter the position:- ");
scanf("%d", &position);
printf("\nEnter the data:- ");
scanf("%d", &data);
reply=insertnode(&head, position, data); //to update head's value in both functions, pass it by reference
if(reply==1)
printf("\nValue Inserted.\n");
else
printf("\nMemory allocation failed!\nTry again!\n");
break;
}
case 2:
{
printf("\nEnter the position of node to delete:- ");
scanf("%d", &position);
reply=deletenode(&head, position);
if(reply==0)
printf("\nThe List is empty.\n");
else if(reply==1)
printf("\nDeleted.\n");
else
printf("\nThe entered position doesn't exist.\n");
break;
}
case 3:
{
display(head);
break;
}
case 4:
{
deletelist(&head);
break;
}
case 5:
exit(0);
default:
{
printf("\nInvalid Input. Try Again.\n");
break;
}
}
}while(1);
}
int insertnode(node **head, int position, int data)
{
int k=1;
node *p, *q, *newnode;
newnode=(node*)malloc(sizeof(node)); //to create a memory block for a new node
if(!newnode) //if malloc fails
return 0;
newnode->data=data; //storing the input data in the new node of the LL
p=*head; //here * is used as value at operator. head is copied to p
//Inserting at the beginning
if(position==1||p==NULL)
{
newnode->next=p; //p contains the address of head. Now, newnode->next stores the address of head
*head=newnode; //after insertion is done, new node is the head of the LL
return 1; //show value inserted
}
else
{
//Traverse LL until the position where we want to insert
while((p!=NULL)&&(k<position))
{
k++; //just a counter, counting nodes in LL
q=p; //q is position node it is updated as p is traversing the nodes in LL
p=p->next; //fetching the next pointer and storing it in p
}
q->next=newnode;
newnode->next=p;
return 1; //show value inserted
}
}
int deletenode(node **head, int position)
{
int k=1;
node *p, *q;
if(*head==NULL) //when list is empty
return 0;
p=*head;
//From beginning
if(position==1)
{
*head=(*head)->next; //updating next node as head
free(p);
return 1;
}
else
{
//Traverse the list until the position from which we want to delete
while((p!=NULL)&&(k<position))
{
k++;
q=p;
p=p->next;
}
if(p==NULL) //At the end
return -1; //position does not exist
else //From middle
{
q->next=p->next;
free(p);
return 1;
}
}
}
void display(node *node)
{
int i=1;
if(node==NULL)
printf("\nThe List is empty.\n");
else
{
while(node!=NULL)
{
printf("Element %d - %d\n", i++, node->data);
node=node->next;
}
}
}
void deletelist(node **head)
{
node *temp, *p;
p=*head;
if(*head==NULL)
printf("\nThe list is empty.\n");
else
{
while(p!=NULL)
{
temp=p->next;
free(p);
p=temp;
}
*head=NULL; //will affect the real head in the caller as head was called by reference
printf("\nThe Linked List has been deleted.\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment