Link list using c++


Link list using c++


Knowledge of linked lists is must for C programmers. This article explains the fundamentals of C linked list with an example C program.

Linked list is a dynamic data structure whose length can be increased or decreased at run time.
How Linked lists are different from arrays? Consider the following points :
  • An array is a static data structure. This means the length of array cannot be altered at run time. While, a linked list is a dynamic data structure.
  • In an array, all the elements are kept at consecutive memory locations while in a linked list the elements (or nodes) may be kept at any location but still connected to each other.


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>

struct linklist
{
                int n;
                struct linklist *next;
};
typedef struct linklist node;

struct linklist *head,*head1;

void displayNode()
{
                node *temp;
                if(head!=NULL)
                {
                                temp=head;

                                printf("\n\t");
                                while(temp!=NULL)
                                {
                                                printf("%d,",temp->n);
                                                temp=temp->next;
                                }
                }
                else
                                printf("\n\t Memory is Empty.");
}

void main()
{
                clrscr();

                void createNode(int);

                int c;

                while(1)
                {
                                printf("\n\n\t Simple linked list Demo \n\t 1. Insert Element. \n\t 2. Display Data. \n\t 3. Exit");
                                printf("\n\t Enter Your Choise : ");
                                scanf("%d",&c);

                                if(c==3)
                                                break;

                                switch(c)
                                {
                                                case 1:
                                                                int n;
                                                                printf("\n\t Enter the Element: ");
                                                                scanf("%d",&n);
                                                                createNode(n);
                                                                break;
                                                case 2:
                                                                displayNode();
                                                                break;
                                }
                                getch();
                                clrscr();
                }

                clrscr();

                void insert_f(int);
                void insert_m(int,int);
                void insert_d(int);
                void delete_f();
                void delete_m(int);
                void delete_r();
                void count();
                void search(int);
                void reverse();
                void sort();

                while(1)
                {
                                printf("\n\n\t Link List Operation. \n\t 1. Insert Front \n\t 2. Insert Middle \n\t 3. Inser Rear \n\t 4. Delete Front \n\t 5. Delete Middle \n\t 6. Delete Rear \n\t 7. Cout \n\t 8. Search \n\t 9. Reverse \n\t 10. Display \n\t 11. Sort List \n\t 12. Exit");
                                printf("\n\t Enter your Choice : ");
                                scanf("%d",&c);

                                if(c==12)
                                                break;

                                switch(c)
                                {
                                                int val;
                                                case 1:
                                                                printf("\n\t Enter the Element You Wanna Inserted : ");
                                                                scanf("%d",&val);
                                                                insert_f(val);
                                                                break;
                                                case 2:
                                                                int af;
                                                                printf("\n\t Enter element after You Have Inserted Your Element : ");
                                                                scanf("%d",&af);
                                                                printf("\n\t Enter the Element You Wanna Inserted : ");
                                                                scanf("%d",&val);
                                                                insert_m(val,af);
                                                                break;
                                                case 3:
                                                                printf("\n\t Enter the Element You Wanna Inserted : ");
                                                                scanf("%d",&val);
                                                                insert_d(val);
                                                                break;
                                                case 4:
                                                                delete_f();
                                                                break;
                                                case 5:
                                                                printf("\n\t Enter Element That After Element are Deleted.");
                                                                scanf("%d",&val);
                                                                delete_m(val);
                                                                break;
                                                case 6:
                                                                delete_r();
                                                                break;
                                                case 7:
                                                                count();
                                                                break;
                                                case 8:
                                                                printf("\n\t Enter Your Element You Wanna Search : ");
                                                                scanf("%d",&val);
                                                                search(val);
                                                                break;
                                                case 9:
                                                                reverse();
                                                                break;
                                                case 10:
                                                                displayNode();
                                                                break;
                                                case 11:
                                                                sort();
                                                                displayNode();
                                                                break;
                                }

                                getch();
                                clrscr();
                }
}
void sort()
{
                node *tmp,*tmp1;
                int val;
                tmp=head;
                if(head==NULL)
                                printf("\n\t List Is Empty.");
                else
                {
                                while(tmp->next!=NULL)
                                {
                                                tmp1=tmp->next;
                                                while(tmp1!=NULL)
                                                {
                                                                if(tmp->n > tmp1->n)
                                                                {
                                                                  val=tmp1->n;
                                                                  tmp1->n=tmp->n;
                                                                  tmp->n=val;
                                                                }
                                                                tmp1=tmp1->next;
                                                }
                                       tmp=tmp->next;
                                }
                }
}

void reverse()
{
                node *tmp,*new1;
                if(head==NULL)
                                printf("\n\t Link List Is Empty.");
                else
                {
                                tmp=head;
                                while(tmp!=NULL)
                                {
                                                new1=(node *)malloc(sizeof(node));
                                                if(head1==NULL)
                                                {
                                                                new1->n=tmp->n;
                                                                new1->next=NULL;
                                                                head1=new1;
                                                }
                                                else
                                                {
                                                                new1->n=tmp->n;
                                                                new1->next=head1;
                                                                head1=new1;
                                                }
                                                tmp=tmp->next;
                                }
                       tmp=head1;

                                printf("\n\t");
                                while(tmp!=NULL)
                                {
                                                printf("%d,",tmp->n);
                                                tmp=tmp->next;
                                }
                }
}
void search(int val)
{
                node *tmp;
                if(head==NULL)
                                printf("\n\t Link List Is Empty.");
                else
                {
                                int c=0;
                                tmp=head;
                                while(tmp!=NULL)
                                {
                                                if(tmp->n==val)
                                                {
                                                                printf("Your Element Are Found : %d",tmp->n);
                                                                c=1;
                                                                break;
                                                }
                                                tmp=tmp->next;
                                }
                                if(c!=1)
                                                printf("\n\t Element Not Found.");
                }
}
void count()
{
                node *tmp;
                int c;
                if(head==NULL)
                                printf("\n\t Link List Is Empty.");
                else
                {
                                c=0;
                                tmp=head;
                                while(tmp!=NULL)
                                {
                                                tmp=tmp->next;
                                                c++;
                                }
                                printf("\n\t In Link List is Your Elements Are : %d ",c);
                }
}
void delete_r()
{
                node *de,*tmp;
                if(head==NULL)
                                printf("\n\t Link List Empty.");
                else
                {
                                tmp=head;
                                while(tmp->next->next != NULL)
                                                tmp=tmp->next;
                                de=tmp->next;
                                tmp->next=NULL;
                                printf("\n\t Your Elements Are Deleted : %d",de->n);
                                free(de);
                }
}

void delete_m(int v)
{
                node *tmp,*de;
                if(head==NULL)
                                printf("\n\t Link List Is Empty.");
                else
                {
                                tmp=head;
                                while(tmp->next->n!=v)
                                                tmp=tmp->next;

                                de=tmp->next;
                                tmp->next=tmp->next->next;
                                printf("\n\t Your Elements Are Deleted : %d",de->n);
                                free(de);
                }
}

void delete_f()
{
                node *tmp;
                if(head==NULL)
                                printf("\n\t Link List Is Empty.");
                else
                {
                                tmp=head;
                                head=tmp->next;
                                printf("\n\t Your Elements Are Deleted : %d",tmp->n);
                                free(tmp);
                }
}

void insert_d(int val)
{
                node *new1,*tmp;
                if(head==NULL)
                                printf("\n\t Link List is Empty.");
                else
                {
                                new1=(node *)malloc(sizeof(node));
                                tmp=head;
                                while(tmp->next!=NULL)
                                                tmp=tmp->next;
                                new1->n=val;
                                new1->next=NULL;
                                tmp->next=new1;

                }
}
void insert_m(int val,int x)
{
                node *new1,*tmp;
                if(head==NULL)
                                printf("\n\t Link List Is Empty.");
                else
                {
                                new1=(node *)malloc(sizeof(node));
                                tmp=head;
                                int c=0;
                                while(tmp != NULL)
                                {
                                                if(tmp->n==x)
                                                {
                                                                new1->n=val;
                                                                new1->next=tmp->next;
                                                                tmp->next=new1;
                                                                c=1;
                                                                break;
                                                }
                                                tmp=tmp->next;
                                }

                                if(c!=1)
                                                printf("\n\t Your Element Not Found So New Element are Not Inserted.");
                }
}

void insert_f(int v)
{
                node *new1;
                if(head==NULL)
                                printf("\n\t Link List Is Empty.");
                else
                {
                                new1=(node *)malloc(sizeof(node));
                                new1->n=v;
                                new1->next=head;
                                head=new1;
                }
}
void createNode(int no)
{
                node *new1,*temp;

                if(head == NULL)
                {
                                new1=(node *)malloc(sizeof(node));
                                new1->n=no;
                                new1->next=NULL;
                                head=new1;
                }
                else
                {
                                temp=head;
                                while(temp->next!=NULL)
                                                temp=temp->next;

                                new1=(node *)malloc(sizeof(node));
                                new1->n=no;
                                temp->next=new1;
                                new1->next=NULL;
                }
}

Comments

Popular posts from this blog

How to create a custom form in wordpress

My new Jquery Plugin name is krDailog