Skip to content

Instantly share code, notes, and snippets.

@jatinsharrma
Last active April 15, 2019 18:55
Show Gist options
  • Save jatinsharrma/9d7c9437b7bfd2a92524ab82898465ac to your computer and use it in GitHub Desktop.
Save jatinsharrma/9d7c9437b7bfd2a92524ab82898465ac to your computer and use it in GitHub Desktop.
Linked List implementation
#include<stdio.h>
#include<stdlib.h>
//Prototyping
void append(void);
void add_begin(void);
void add_after(void);
void delete_node(void);
void display(void);
int length(void);
void reverse(void);
// Intializing
struct node{
int data;
struct node* link;
};
struct node* root = NULL;
int len;
//---------------------------------------------------------------------------------------------------
int main(){
while(1){
printf("\nSingle liked list operations : \n");
int option;
printf(" 1. Append \n 2. Add at begin \n 3. Add at after \n 4. Length \n 5. Display \n 6. Delete \n 7. Reverse \n 8. Quit \n");
printf("Enter your choice : ");
scanf("%d", &option);
switch(option){
case 1 : append();
break;
case 2 : add_begin();
break;
case 3 : add_after();
break;
case 4 : length();
break;
case 5 : display();
break;
case 6 : delete_node();
break;
case 7 : reverse();
break;
case 8 : exit(0);
break;
default : printf("\nInvalid Input \n");
}
}
return 0;
}
//---------------------------------------------------------------------------------
void append(){
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nEnter node data : ");
scanf("%d", &temp -> data);
temp -> link = NULL;
if (root == NULL){
root = temp;
}
else{
struct node * p;
p = root;
while (p-> link != NULL ){
p = p -> link;
}
p -> link = temp;
}
}
//-------------------------------------------------------------------------------
int length(){
struct node* temp;
int count = 0 ;
temp = root;
while(temp != NULL){
temp = temp -> link;
count ++;
}
len = count;
if (count == 0){
printf("\nList is empty\n");
}
else {
printf("\nTotal number of elements in list : %d\n",len);
}
return len;
}
//------------------------------------------------------------------------------------------
void display(){
struct node* temp;
temp = root;
if( temp == NULL){
printf("\nNothing to display\n");
}
else{
printf("\n");
while(temp != NULL){
printf("%d --> ",temp -> data);
temp = temp -> link;
}
printf("NULL");
}
printf("\n");
}
//----------------------------------------------------------------------------------------------------
void add_begin(){
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data : ");
scanf("%d", &temp -> data);
temp -> link = root;
root = temp;
}
//------------------------------------------------------------------------------------------------------
void add_after(){
printf("\nEnter the element after which you want to insert : ");
int loc;
scanf("%d",&loc);
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("\nEnter node data : ");
scanf("%d", &temp -> data);
struct node* p;
p = root;
if (p == NULL){
printf("\nThere is no node in list\n");
}
else{
while (p -> link != NULL){
if(p -> data == loc){
break;
}
else{
p = p->link ;
}
}
if (p ->link == NULL && p -> data == loc){
temp -> link = NULL;
p -> link = temp;
}
else if (p -> link == NULL){
printf("\nEntered element does not exist in list\n");
}
else{
temp -> link = p -> link;
p -> link = temp;
}
}
}
//------------------------------------------------------------------------------------------------------
void delete_node(){
struct node* p, *q ,*t;
p = root;
int count = 0;
if (p != NULL){
printf("\nEnter the element to delete : ");
int loc;
scanf("%d", &loc);
while (p-> link != NULL ){
if (p -> data == loc){
break;
}
else{
p = p -> link;
if (count == 0){
t = p;
}
else if(count == 1){
q = p;
}
else {
t = q;
q = p;
}
}
count ++;
}
if (count == 0){
if (p-> link == NULL){
root = NULL;
p -> link = NULL;
free(p);
}
else{
root = p -> link;
p -> link = NULL;
free(p);
}
}
else if (p -> link == NULL && p -> data == loc){
t -> link = NULL;
p -> link = NULL;
free(p);
}
else if (p -> link == NULL){
printf("\nEntered element is not in list.\n");
}
else{
t -> link = p -> link;
p -> link = NULL;
free(p);
}
}
else{
printf("\nNothing to delete here.\n");
}
}
//-------------------------------------------------------------------------
void reverse(){
struct node* prev = NULL;
struct node* current;
current = root;
struct node* next;
while (current != NULL)
{
next = current->link;
current->link = prev;
prev = current;
current = next;
}
if (prev != NULL){
root = prev;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment