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)

Padovan string, padovan string for a natural number is defined

padovan string for a natural number is defined

Write a program to calculate the total resistance, Write a program to calcu...

Write a program to calculate the total resistance of a series or parallel circuit. The maximum number of resistors is two.   We need to decide whether the user wants the to

How to use turbo c, Sir i want to know all the basic knowledge of turbo c.

Sir i want to know all the basic knowledge of turbo c.

Explain conversion functions, Conversion Functions Conversion functions...

Conversion Functions Conversion functions are member functions used for the following purposes: 1. Conversion of object to basic data type. 2. Conversion of basic data ty

What is structure, What is Structure? An Array is a data structure who...

What is Structure? An Array is a data structure whose elements are all of the similar data type. The structure is a data structure whose individual elements are able to differ

Use of random function - c program , Use of random function: int main(...

Use of random function: int main(void) {    int i,j;         for(j=0;j       {      // randomize();       for(i=0;i                  printf("%d\n", ran

What is precedence and order of evaluation, Precedence and Order of evaluat...

Precedence and Order of evaluation The languages follow a standard precedence for basic operators. Precedence rules help in deleting ambiguity of the order of operations perfor

C program to check prime numbers , C Program to check PRIME NUMBERS   ...

C Program to check PRIME NUMBERS   main() {           int i,k,r,flag;           clrscr();           printf("ENTER THE NO. TO CHECK IT IS PRIME OR NOT: ");

Explain multiple inheritance with a common base, Multiple inheritance with ...

Multiple inheritance with a common base (Hybrid Inheritance) Inheritance is an important and powerful feature of OOP. Only the imagination of the person concerned is the limit.

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