Skip to content

Instantly share code, notes, and snippets.

@jamesgeorge007
Last active October 19, 2018 17:02
Show Gist options
  • Save jamesgeorge007/0552e5bd971b4083810373e38028d020 to your computer and use it in GitHub Desktop.
Save jamesgeorge007/0552e5bd971b4083810373e38028d020 to your computer and use it in GitHub Desktop.
Linked list operations implemented in C.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* link;
};
struct Node* header = NULL;
int getSize()
{
int size = 0;
struct Node* ptr = header;
while(ptr != NULL)
{
size++;
ptr = ptr->link;
}
return size;
}
void display()
{
struct Node* ptr = header;
// int count = 1;
if(header == NULL)
{
printf("\nEmpty!\n");
exit(1);
}
else
{
printf("\nElements are:\n");
while(ptr != NULL)
{
// printf("\nData at node %d is %d", count, ptr->data);
printf("%d\t", ptr->data);
ptr = ptr->link;
// count++;
}
printf("\nThere are %d elements.\n", getSize());
}
}
void insertAtFront(int el)
{
struct Node* temp = ((struct Node*)malloc(sizeof(struct Node)));
if(temp == NULL)
{
printf("\nNot enough memory!");
exit(1);
}
temp->data = el;
temp->link = header;
header = temp;
display();
}
void insertAtRear(int el)
{
if(header == NULL)
{
insertAtFront(el);
return;
}
struct Node* new_ptr = (struct Node*)malloc(sizeof(struct Node));
new_ptr->data = el;
new_ptr->link = NULL;
struct Node* ptr = header;
while(ptr->link != NULL)
{
ptr = ptr->link;
}
ptr->link = new_ptr;
display();
}
void insertAtAny(int el, int pos)
{
if((pos > getSize()) && (pos != getSize() + 1))
{
printf("\nCannot insert an element at position %d", pos);
return;
}
struct Node* new_ptr = (struct Node*)malloc(sizeof(struct Node));
new_ptr->data = el;
new_ptr->link = NULL;
struct Node* ptr = header;
int i = 2;
if(pos == 1)
{
new_ptr->link = header;
header = new_ptr;
display();
return;
}
while(ptr != NULL && i != pos)
{
ptr = ptr->link;
i++;
}
new_ptr->link = ptr->link;
ptr->link = new_ptr;
display();
}
void deleteAtFront()
{
struct Node* ptr = header;
//struct Node* temp;
if(ptr == NULL)
{
printf("\nList is empty!");
exit(1);
}
/* temp = ptr->link;
header->link = temp; */
header = header->link;
free(ptr);
display();
}
void deleteAtRear()
{
struct Node* ptr = header;
struct Node* temp;
if(ptr->link == NULL)
{
printf("\nList is empty!\n");
exit(1);
}
while(ptr->link != NULL)
{
temp = ptr;
ptr = ptr->link;
}
temp->link = NULL;
free(ptr);
display();
}
void deleteAtAny(int data)
{
struct Node* ptr;
struct Node* temp;
temp = header;
ptr = temp->link;
while(ptr != NULL)
{
if(ptr->data != data)
{
temp = ptr;
ptr = ptr->link;
}
else
{
temp->link = ptr->link;
free(ptr);
display();
return;
}
}
if(ptr == NULL)
{
printf("\nNode with data %d not found within the list!", data);
}
display();
}
/* void deleteAtAny(int pos)
{
if(pos > getSize())
{
printf("\nInvalid position!");
return;
}
struct Node* ptr;
struct Node* temp;
int i = 2;
temp = header;
ptr = temp->link;
if(pos == 1)
{
deleteAtFront();
return;
}
while(ptr->link != NULL && i != pos)
{
temp = ptr;
ptr = ptr->link;
i++;
}
temp->link = ptr->link;
free(ptr);
display();
} */
int main()
{
char choice, cont;
int el, pos, data;
do{
printf("\n1.Insert at front\n2.Insert at rear\n3.Insert at any position\n4.Delete at front\n5.Delete at rear\n6.Delete at any position\n7.Display\nEnter your option: ");
scanf("%c", &choice);
switch(choice)
{
case '1':
printf("\nElement to be inserted: ");
scanf("%d", &el);
insertAtFront(el);
break;
case '2':
printf("\nElement to be inserted: ");
scanf("%d", &el);
insertAtRear(el);
break;
case '3':
printf("\nElement to be inserted: ");
scanf("%d", &el);
printf("\nPosition to be inserted: ");
scanf("%d", &pos);
insertAtAny(el, pos);
break;
case '4':
deleteAtFront();
break;
case '5':
deleteAtRear();
break;
case '6':
printf("\nData of the node to be deleted: ");
scanf("%d", &data);
deleteAtAny(data);
break;
case '7':
display();
break;
default:
printf("\nInvalid option!");
}
printf("\nWant to continue? ");
scanf("%c", &cont);
}while(1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment