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)

Area under curve, Write a program to find the area under the curve y = f(x)...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b.

Hotel booking, Construct a console program to manage the booking of a Hotel...

Construct a console program to manage the booking of a Hotel room.

Integer parameters, write a static method with one integer parameter, x tha...

write a static method with one integer parameter, x that returns the value of the polynomial 3x(2)- 7x + 2

., what is polymorphism

what is polymorphism

Fibonacci series, draw the flow chart to print the fibonacci series upto n ...

draw the flow chart to print the fibonacci series upto n th terms

C++, Write a program to find the area under the curve y = f(x) between x = ...

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b

Luminous jewels polishing game, Luminous Jewels - The Polishing Game Bytel...

Luminous Jewels - The Polishing Game Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various lum

C program to define power of a function, C program to define power of a fun...

C program to define power of a function: Write a program to use power of a function. void main() { int x,y,po=1,i,j; clrscr();   printf("Give x,y\n"); sca

What is the difference between = symbol and = = symbol, What is the differe...

What is the difference between = symbol and == symbol? - The = symbol is generally used in mathematical operations. It's used to assign a value to a given variable whereas the

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