C program for dynamic data structure(linked list), C/C++ Programming

Assignment Help:

Aim: To implement a program for dynamic data structure(linked list).

Code:                      

class node

{

            int data;

            node *next;

            node *start;

            public:

            node();

            void create_list();

            void insert_pos(int d, int p);

            void delete_pos(int p);

            void delete_data(int d);

            void insert_sort(int d);

            void display_list(void);

};

node::node()

{

            start=new node;

}

void node::create_list()

{

            start->data=NULL;

            start->next=NULL;

}

void node::insert_pos(int d, int p)

{

            int pos=0;

            node *temp=start, *t;

            if(temp==NULL )

            {

                        cout<<"\nList is Empty!\nAdding in beggining.";

                        temp->data=d;

                        temp->next=NULL;

                        start=temp;

            }

            else

            {

                        while(temp!=NULL)

                        {

                                    pos++;

                                    if(pos==p)

                                    {

                                                t=new node;

                                                t->data=d;

                                                t->next=temp->next;

                                                temp->next=t;

                                                break;

                                    }

                                    temp=temp->next;

                        }

            }

}

 

void node::delete_pos(int p)

{

            node *temp, *prev;

            prev=start;

            temp =start;

            int pos=0;

            int size=0;

            while(temp!=NULL)

            {

                        temp=temp->next;

                        size++;

            }

            temp=start;

            if(p==0 || p>size )

            {

                        cout<<"\nPosition out of bounds!\n";

 

            }

            while(pos

            {

                        prev=temp;

                        temp=temp->next;

                        pos++;

            }

            if(pos==p && p!=0)

            {

                        cout<<"\n "<data<<" Deleted succesfully.\n";

                        prev->next=temp->next;

                        free(temp);

            }

}

void node::delete_data(int d)

{

            node *temp, *prev;

            temp=start->next;

            prev=start;

            while(temp!=NULL)

            {

                        if(temp->data==d)

                        {

                                    break;

                        }

                        else

                        {

                                    prev=temp;

                                    temp=temp->next;

                        }

            }

            if(temp->data==d)

            {

                        cout<<"\n "<data<<" Deleted Successfully!\n";

                        prev->next=temp->next;

                        free(temp);

            }

            if(temp==NULL)

            {

                        cout<<"\nData not found!\n";

            }

 

}

 

void node::insert_sort(int d)

{

            node *temp;

            temp=start;

            while(

}              

void node::display_list()

{

            node *temp;

            temp=start->next;

            cout<<"\n\nList:";

            while(temp!=NULL)

            {

                        cout<data<<"\t";

                        temp=temp->next;

            }

 

}

 

void main()

{

            node list1;

            int a,b,c,p,ch,d;

            clrscr();

            do

            {

                        cout<<"\n\n1. Create List\n2.Insert node by position";

                        cout<<"\n3. Delete node by position\n4. Delete node by data.";

                        cout<<"\n5. Insert in sorted list\n6. Display List\n0. Exit";

                        cin>>ch;

                        switch (ch)

                        {

                                    case 1:

                                                list1.create_list();

                                                break;

                                    case 2:

                                                cout<<"\nEnter data:";

                                                cin>>b;

                                                cout<<"\nPosition:";

                                                cin>>c;

                                                list1.insert_pos(b,c);

                                                break;

                                    case 3:

                                                cout<<"\nEnter position of node to be deleted:";

                                                cin>>p;

                                                list1.delete_pos(p);

                                                break;

                                    case 4:

                                                cout<<"\nEnter data to be deleted:";

                                                cin>>d;

                                                list1.delete_data(d);

                                                break;

                                    case 5:

                                                cout<<"\nEnter data to be iserted:";

                                                cin>>d;

.                                               list1.insert_sort(d);

                                                break;

                                    case 6:

                                                list1.display_list();

                                                getch();

                                                break;

                                    case 0:

                                                exit(1);

                                                break;

                                    default:

                                                cout<<"Wromg choice!";

                                                break;

                        }

            }while(1);

}

 

Output:

1. Create List

2.Insert node by position

3. Delete node by position

4. Delete node by data.

5. Insert in sorted list

6. Display List

0. Exit1

 

1. Create List

2.Insert node by position

3. Delete node by position

4. Delete node by data.

5. Insert in sorted list

6. Display List

0. Exit2

 

Enter data:25

 

Position:1

 

1. Create List

2.Insert node by position

3. Delete node by position

4. Delete node by data.

5. Insert in sorted list

6. Display List

0. Exit2

 

Enter data:13

 

Position:1

 

1. Create List

2.Insert node by position

3. Delete node by position

4. Delete node by data.

5. Insert in sorted list

6. Display List

0. Exit6

 

List:13            25

1. Create List

2.Insert node by position

3. Delete node by position

4. Delete node by data.

5. Insert in sorted list

6. Display List

0. Exit3

Enter position of node to be deleted:1

 13 Deleted succesfully.


Related Discussions:- C program for dynamic data structure(linked list)

Buffer overflow, Can you help me with a C++ project?

Can you help me with a C++ project?

Insertion sort - c program, Insertion sort - C program: Write a progra...

Insertion sort - C program: Write a program in c to define a insertion sort. void main()  {   clrscr();   int a[100],ch,n;   cout   cin>>n;   for (int i=0

Named what can derived class add?, New data members  New member function...

New data members  New member functions  New constructors and destructor  New friends

Radix, a popular joke among computer is to say............

a popular joke among computer is to say............

Queue, program to enter 5 values in queue and remove value one by one from ...

program to enter 5 values in queue and remove value one by one from queue.

Program to calculate pie, This problem familiarizes you with using random n...

This problem familiarizes you with using random numbers in C++. The program is to compute a good approximation of p using a simulation method called "Monte Carlo". The following fi

Function with unsigned char parameters, Write a function that has four uns...

Write a function that has four unsigned char parameters, combines the four one-byte integer values into an unsigned integer, and returns the unsigned integer. When the four one

Briefly describe how a linear search algorithm works, Question 1: (a) D...

Question 1: (a) Describe the following objects and condition states: (i) ifstream and ofstream objects (ii) eof(), fail(), bad() and good() functions. (b) Write a C++ pr

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd