Linked List in C++
Posted by Samath
Last Updated: January 07, 2021
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>

using namespace std;

class linklist
{
     private:
             struct node
         {
              char *data;
            node *link;
         }*p;

   public:
             linklist();
         void append( char *num );
         void add_as_first( char *num );
         void addafter( int c, char *num );
         void del( char *num );
         void display();
         int count();
         ~linklist();
};

linklist::linklist()
{
     p=NULL;
}

void linklist::append(char *num)
{
     node *q,*t;
   if( p == NULL )
   {
        p = new node;
      p->data = num;
      p->link = NULL;
   }
   else
   {
        q = p;
      while( q->link != NULL )
           q = q->link;
      t = new node;
      t->data = num;
      t->link = NULL;
      q->link = t;
   }
}

void linklist::add_as_first(char *num)
{
     node *q;
   q = new node;
   q->data = num;
   q->link = p;
   p = q;
}

void linklist::addafter( int c, char *num)
{
     node *q,*t;
   int i;
   for(i=0,q=p;i<c;i++)
   {
        q = q->link;
      if( q == NULL )
      {
           cout<<"\nThere are less than "<<c<<" elements.";
         return;
      }
   }

   t = new node;
   t->data = num;
   t->link = q->link;
   q->link = t;
}

void linklist::del( char *num )
{
     node *q,*r;
   q = p;
   if( q->data == num )
   {
        p = q->link;
      delete q;
      return;
   }

   r = q;
   while( q!=NULL )
   {
        if( q->data == num )
      {
           r->link = q->link;
         delete q;
         return;
      }
      r = q;
      q = q->link;
   }

   cout<<"\nElement "<<num<<" not Found.";
}

void linklist::display()
{
     node *q;
   cout<<endl;
   for( q = p ; q != NULL ; q = q->link )
        cout<<endl<<q->data;
}

int linklist::count()
{
     node *q;
   int c=0;
   for( q=p ; q != NULL ; q = q->link )
        c++;
   return c;
}

linklist::~linklist()
{
     node *q;
   if( p == NULL )
        return;
   while( p != NULL )
   {
        q = p->link;
      delete p;
      p = q;
   }
}

int main()
{
int access;
char choice[21];
linklist ll;
int position;
cout<<"\t\t\t\t*\t*";
    cout<<"\t\t\t\t**\t**";
    cout<<"\t\t\t\t***\t***";
    cout<<"\t\t\t\t****\t****";
    cout<<"\t\t\t\t*****\t*****";
    cout<<"\t\t\t\t******\t******";
    cout<<"\t\t\t\t*******\t*******";
    cout<<"\t\t\t\t*******\t*******";
    cout<<"\t\t\t\t******\t******";
    cout<<"\t\t\t\t*****\t*****";
    cout<<"\t\t\t\t****\t****";
    cout<<"\t\t\t\t***\t***";
    cout<<"\t\t\t\t**\t**";
    cout<<"\t\t\t\t*\t*";
     cout<<"\n\n\n\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
    cout<<"\t\t\tWelcome to the Library System\n";
    cout<<"\t\t\t@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n";

    for(;;)
    {
    cout<<"\n\t====================================================\n";
    cout<<"\tplease select one of the following options\n\n";
    cout<<"\tPress 1 to add a new member at the beginning of the list.\n";
    cout<<"\tPress 2 to add a new member at the end of the list.\n";
    cout<<"\tPress 3 to add a new member at any position of the list.\n";
    cout<<"\tPress 4 to count the members.\n";
      cout<<"\tPress 5 to display the members.\n";
    cout<<"\tPress 6 to delete a member from the list.\n";
   cout<<"\tPress 7 to EXIT the program.\n";
    cout<<"\n\t====================================================\n";

cout<<"\nOption:";
    scanf("%d",&access);
    switch(access)
    {
    case 1: 
cout<<"\nPlease enter New member's name: ";
scanf("%s",choice);
ll.add_as_first(choice);
cout<<"\nPress enter to continue...";
getchar();
 getchar();
system("cls");
    continue;
    case 2: 
cout<<"\nPlease enter New member's name: ";
scanf("%s",choice);
ll.append(choice);
cout<<"\nPress enter to continue...";
getchar();
 getchar();
system("cls");
    continue;
    case 3: 
    cout<<"\nPlease enter New member's name: ";
scanf("%s",choice);
cout<<"\nPlease enter position number: ";
scanf("%d",&position);
ll.addafter(position,choice);
cout<<"\nPress enter to continue...";
getchar();
 getchar();
system("cls");
   continue;
case 4: 
cout<<"\nNo. of members = "<<ll.count();
cout<<"\nPress enter to continue...";
getchar();
 getchar();
system("cls");
    continue;
    case 5: ll.display();
    cout<<"\nPress enter to continue...";
getchar();
 getchar();
 system("cls");
    continue;
    case 6:  ll.del(choice);
    system("cls");
    continue;
    case 7:
exit(0); 
   break;
    default:cout<<"\nInvalid\n\n";
        continue;
    }
    }
   return 0;
}