Link to home
Start Free TrialLog in
Avatar of rajashekar2004
rajashekar2004

asked on

dll

hi
 experts

    i have a runtime error in my program in c++.it is a very small program but not able to debug it.there is the source code.

#include <iostream>
using namespace std;
int insert();
int print();
main()
{
   struct node
   {
         int info;
         node *next;
   };

      node* head=NULL;
      node* temp=NULL;
      node* temp1=NULL;
      head=new node;
      head->next=NULL;
      int c;
      cout<<"1--->insert."<<endl<<"2--->delete."<<endl<<"3--->print."<<endl<<"4--->Exit."<<endl;
      cout<<"Enter the choice --";
      cin>>c;
      switch(c)
      {
      case 1:insert();
            {
              int x;
            cout<<"Enter the data to be insterted __ ";
              cin>>x;
              if(head->next==NULL)
                head->info=x;
              else
                  {
                  temp=head;
                  while(temp->next!=NULL)
                    temp=temp->next;
                  if(temp->next==NULL)
                    {
                     temp1=new node;
               temp1->info=x;
                     temp->next=temp1;
                     temp1->next=NULL;
                    }
                  }
            }
            break;
//      case 2:del();break;
    case 3:print();
            {
      if(head==NULL)
            cout<<"No elements to display";
      else
      {
      temp=head;
       do
         {
               cout<<temp->info<<"->";
               temp=temp->next;
         }while(temp->next!=NULL);
         }
}
break;
      case 4:exit(0);break;
      default:cout<<"Entered wrong choice";
      }
      return 0;
}
 

please help me asap.
ASKER CERTIFIED SOLUTION
Avatar of amjedmsa
amjedmsa

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe you want that:

#include <iostream>
using namespace std;

struct node
{
   int info;
   node *next;
};

void insert(node*& head);
void print(node* head);
void deleteAll(node* head);

int main()
{
   
    node* head= NULL;
   
    while (true)
    {
        int c;
        cout<<endl<<"1--->insert."<<endl<<"2--->delete."<<endl<<"3--->print."<<endl<<"4--->Exit."<<endl;
        cout<<"Enter the choice --";
        cin>>c;
       
        switch(c)
        {
        case 1:
            insert(head);
            break;
            //     case 2:del();break;
        case 3:
            print(head);
            break;
        case 4:
            deleteAll(head);
            return 0;
        default:
            cout<<"Entered wrong choice"<<endl;
            break;
        }
    }
    return 1;
}


void insert(node*& head)
{  
    node* temp;
    node* temp1;
    int x;
    cout<<"Enter the data to be inserted __ ";
    cin>>x;
    if(head==NULL)
    {
        head = new node;
        head->next = NULL;
        head->info = x;
        return;
    }
    temp=head;
    while(temp->next!=NULL)
        temp=temp->next;
    temp1=new node;
    temp1->info=x;
    temp->next=temp1;
    temp1->next=NULL;
}

void print(node* head)
{
    if(head==NULL)
        cout<<"No elements to display";
    else
    {
        node* temp=head;
        do
        {
            cout<<temp->info<<"->";
            temp=temp->next;
        }
        while(temp !=NULL);
    }
}


void deleteAll(node* head)
{
    node* temp = head;
    node* temp1;
   
    while (temp != NULL)
    {
        temp1 = temp->next;
        delete temp;
        temp = temp1;
    }
}

Regards, Alex