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)

Multilevelinheritance inc++, develop a program read the following informati...

develop a program read the following information from the keyboard in which base class consist of employee name code and destingnation the derived class contain the data members th

Working of ordered linked list, Working Ordered linked list: • Eachint...

Working Ordered linked list: • Eachinteger in the queue is stored inside of a QueueItem. The QueueItem contains the integer, and a pointer to the next item in the queue. Fo

Write a c program to input three real numbers, Write a C program to input t...

Write a C program to input three real numbers and  print them out as follows : The first variable is ....... and the second one is ...... The last variable is .......... T

Roshdy, While spending the summer as a surveyor’s assistant, you decide to ...

While spending the summer as a surveyor’s assistant, you decide to write a program that transforms compass headings in degrees (0-360) to compass bearings. A compass bearing consis

Program to display stock containing item code, THIS PROGRAM IS TO DISPLAY A...

THIS PROGRAM IS TO DISPLAY A STOCK CONTAINING ITEM CODE,ITEM NAME,PRICE AND ANOTHER STOCK WITH CODE & QUANTITY AND DISPLAY COMPLETE INFORMATION #include #include #include

Constructor: Copy, b) Why copy constructor accepts reference to an object a...

b) Why copy constructor accepts reference to an object and not the object itself? What happens if we do otherwise?

Encryption/Decryption, I need to include files so you can understand easier...

I need to include files so you can understand easier.

Define name mangling in c++??, A: The procedure of encoding the parameter t...

A: The procedure of encoding the parameter types along with the function/method name into a unique name is called as name mangling. The inverse procedure is called demangling. F

Use of a windows box, This program requires the use of a windows box, no co...

This program requires the use of a windows box, no console applications are allowed. Prepare for Christmas now.  Each year, Reindeer Gift Emporium publishes a Christmas price list

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