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)

Copy constructor and an overloaded assignment operator, What is the differe...

What is the difference among a copy constructor and an overloaded assignment operator? Ans) A copy constructor constructs a latest object by using the content of the argument

Algorithms, Ask question #Minimum 100 write an algorithm to calculate simpl...

Ask question #Minimum 100 write an algorithm to calculate simple interest and compound interestwords accepted#

Chapter 21, Test scores solution help. Three member variables

Test scores solution help. Three member variables

Area under the curve, Write a program to find the area under the curve y = ...

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

Need discrete math tutor, Project Description: I will need a tutor that ...

Project Description: I will need a tutor that would help me out in Data Structure and learning Algorithm more at the mathematical/algorithmic level. The book is written by "by T

Need android app development, Project Description: I am seeking a develo...

Project Description: I am seeking a developer who can start an app from scratch and get it delivered to me as soon as possible. It is a little android based app. A background on

Common concepts of object oriented, Object Oriented Programming Paradigm: ...

Object Oriented Programming Paradigm: For OOP, Paradigm is a standard set.  The OOP will help to develop software in modules, which will be integrated later as a full packag

Smuggler, Smugglers are becoming very smart day by day. Now they have devel...

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

Program that calculates bowling scores, Homework – Chapter 5 – Bowling Scor...

Homework – Chapter 5 – Bowling Scores 33. Write a complete C++ program to do the following: The main program reads in and prints three bowling scores, score1, score2, and score

Define difference among new & malloc?, Both malloc & new functions are util...

Both malloc & new functions are utilized for dynamic memory allocations & the basic difference is: malloc need a special "typecasting" while it allocates memory for eg. if the poin

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