How to build a Linked List that can insert any type of data

How to build a Linked List that can insert any type of data. For example: SLinkedList abc1; SLinkedList abc2; SLinkedlist abc3; Please answer in C plus plus, you don\'t need to develop all function, just insert data is enough.

Thank you.

Solution

#include<stdio.h>
#include<stdlib.h>

#define LEFT 2
#define RIGHT 3

typedef struct linked_list
{
   double data;
   struct linked_list *next;
}node;//node declaration for single linked list
/*function prototype declaration*/
void insertion(node **,double);

void erase(node **,node **);

//void left_or_right(node **,node **,int);

node *makenode(double);

void show(node **,node **);
/*main*/
int main()
{
   system(\"clear\");
   char choice;
   double item;
   node *start=NULL,*prev=NULL;
   do
   {
       printf(\"\ ===========================\ \\tMENU\ ===========================\ \\t1.Insert\ \\t2.Exit\ Enter your choice :: \");
       scanf(\"\ %c\",&choice);
       switch(choice)
       {
           case \'1\':
               printf(\"\ Enter item :: \");
               scanf(\"%lf\",&item);
               insertion(&start,item);//insert function calling
               show(&start,&prev);
               break;
           case \'2\':
               erase(&prev,&start);
               printf(\"\ You are exited from the whole program.\ \ \");
               exit(1);
           default:
               printf(\"\ \\tWrong Choice\ \");
       }
   }
   while(choice!=\'2\');
   return 0;
}

void show(node **start,node **prev)/*this function displays the whole linked list*/
{
   if((*start)!=NULL)
   {
       printf(\"\ \ X <- \");
       node *temp=NULL,*p=*prev,*q=NULL;
       while(p!=NULL)
       {
           if(temp==NULL)
           {
               temp=makenode(p->data);
           }
           else
           {
               q=makenode(p->data);
               q->next=temp;
               temp=q;
           }
           p=p->next;
       }
       while(temp!=NULL)
       {
           printf(\"%.0lf <- \",temp->data);
           q=temp;
           temp=temp->next;
           free(q);
           q=NULL;
       }
       printf(\"\\b| \");
       p=*start;
       while(p!=NULL)
       {
           if(p==*start)
           {
               printf(\"%.0lf |-> \",p->data);
           }
           else
           {
               printf(\"%.0lf -> \",p->data);
           }
           p=p->next;
       }
       printf(\" X \ \ \");
   }
   else
   {
       printf(\"\ \ \\tEmpty Linked List\ \ \");
   }
}

void insertion(node **start,double item)//this function inserts a new node in the linked list
{
   node *temp=makenode(item),*p=NULL;
   if(*start==NULL)
   {
       *start=temp;
   }
   else
   {
       p=*start;
       while(p->next!=NULL)
       {
           p=p->next;
       }
       p->next=temp;
   }
}

node *makenode(double item)
{
   node *temp=NULL;
   temp=(node *)malloc(sizeof(node));
   if(temp==NULL)
   {
       printf(\"\ Dynamic memory allocation failed.\ \");
       exit(0);
   }
   temp->data=item;
   temp->next=NULL;
}

void erase(node **prev,node **start)//this function deletes all nodes of the linked list when the program terminates
{
   node *p=NULL;
   while(*start!=NULL)
   {
       p=*start;
       *start=(*start)->next;
       free(p);
       p=NULL;
   }
   while(*prev!=NULL)
   {
       p=*prev;
       *prev=(*prev)->next;
       free(p);
       p=NULL;
   }
}

How to build a Linked List that can insert any type of data. For example: SLinkedList abc1; SLinkedList abc2; SLinkedlist abc3; Please answer in C plus plus, yo
How to build a Linked List that can insert any type of data. For example: SLinkedList abc1; SLinkedList abc2; SLinkedlist abc3; Please answer in C plus plus, yo
How to build a Linked List that can insert any type of data. For example: SLinkedList abc1; SLinkedList abc2; SLinkedlist abc3; Please answer in C plus plus, yo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site