Pages

Thursday, July 11, 2013

Link LIst In Structures in C++

/*
QUESTION:
1. Write a menu­ driven program for managing the operations of a Store. The program would

allow a user to perform the operations given below on store items stored in a linked list. A store

item has an ID (an integer), a name (up to 50 characters) and quantity of that item in the store

(an integer).

2. Add a new item to the store. Ask the user to specify the position where the new item is added.

An error message should be displayed if the position is invalid. For example, an error should be

displayed if the user enters a negative value or zero for the position, or if the position exceeds

one more than the position of the last element in the list.

3. Sell an item. Prompt the user for the ID of the item and quantity to be sold. Locate that item in

the linked list, verify if you have enough quantity of that available in the store. If enough quantity is

available, sell the item by updating the quantity. If the remaining quantity of that item is zero,

delete that item’s record from the linked list.

4. Delete a specific Item from the store. Prompt the user for the ID of the item to be deleted.

Remove the record of that item from the list free its memory using delete. An error message

should be displayed if there is no item in the store with the given ID.

5. Modify the name of an item. Prompt the user for the ID and the new name of the item to be

renamed. Locate that item in the linked list and update its name. An error message should be

displayed if there is no item in the store with the given ID.

6. Find an item. Prompt the user for the name of the item to be found. Locate that item in the

linked list and display its ID, name and remaining quantity in the store. An error message should

be displayed if there is no item in the store with the given name.

7. Print the ID, name and available quantity of all the items in the store.
 


CODE:

*/


#include <iostream>
#include <windows.h>
using namespace std;
int Counter=0;
///////////////////////////////////////Structure
struct node {
 int id;
 char name[50];
 int Quanty;
 node *next;

};
///////////////////////////////////Taking data from User
node *get_data()
{
 ++Counter;
 node *p = new node;
 cout<<"ENTER ID:  "; cin >> p->id;
 cout<<"ENTER Name:  "; cin >> p->name;
 cout<<"ENTER Quantity:  "; cin >> p->Quanty;
 cout<<"\n\n\n";
 p->next = NULL;

 return p;

}
////////////////////////////////////////////////////////////////Build Linked List
void insert_node (node **ph , node **pt , node *pd)
{
 if(*ph == NULL && *pt== NULL)
 {
  *ph = pd;
  *pt = pd;

 }
 else
 {
  (*pt)->next=pd;
  *pt = pd;
 }
}
////////////////////////////////////////////////////////////////////ISERT at User Desire position
void insertkaar(node **HEAD ,node *newn , int position_no ){   
 node *Front_ptr=*HEAD ,   *Back_ptr=NULL; int Loop=1;
 while (Loop != position_no )
 {
  Back_ptr=Front_ptr;         Front_ptr=Front_ptr->next;    
  Loop++;
 }
 Back_ptr->next=newn ; 
 newn->next=Front_ptr;

}
///////////////////////////////////////According to User Desire Insert New record  at begining of Linked list
void insertion(node **P_H ,node *n)
{
 n->next = *P_H;
 *P_H = n;

}
/////////////////////////////////////According to User Desire Insert New record  at the END of Linked list
void Insert_Tail(node ** Tail , node *Temp)
{
 (*Tail)->next = Temp  ;
 *Tail = Temp ;
}
 //////////////////////////Deleting a memory From List
void  Delete_record(node **HEAD , int position_no )
{
 node *Front_ptr=*HEAD ,   *Back_ptr=NULL; int Loop=1; node * Delt_ptr=NULL;
 while (Loop != position_no )
 {
  Back_ptr=Front_ptr;
  Front_ptr=Front_ptr->next;
  Delt_ptr=Front_ptr;
  Loop++;
 }

 Front_ptr=Front_ptr->next ; 
 Back_ptr->next=Front_ptr;

    delete Delt_ptr;
 Delt_ptr=NULL;
}
 ///////////////Find ID of User desired
void Find_ID(node *HEAD , int Target)
{
 node *p = HEAD;
 while(p != NULL)
 {
  if(p->id == Target)
  {
   cout<<"Info of that Person is follow as\nNAME is:  "<<p->name<<"\nID is:  "<<p->id;
   cout<<"\nRemaining Quantity is:  "<<p->Quanty<<endl<<endl;

  }
  p=p->next;
 }


}
 ///////////////////////Display whole Linked List
void display_data(node *p)
{
 while(p != NULL)
 {
  cout<<"ID  "<<p->id<<endl;
  cout<<"Name    "<<p->name<<endl;
  cout<<"Quantity    "<<p->Quanty<<"\n\n\n";
  p=p->next;
 }

}
 ////////////////////////////MAIN Start
int main()
{
 int menu_no;  int position_no;
 node *head=NULL , *tail=NULL , *temp=NULL;

start:
 cout<<"press 1 for Enter a Data of new item to store\n";
 cout<<"press 2 for Insert a new item in existing linked List\n";
    cout<<"press 3 to delete a specific item from the store\n";
 cout<<"press 4 to find an item\n";
 cout<<"press 5 to see data of items stored\n\n\n";
 cout<<"choose a number from menu and Enter that for further procedure:  ";
 cin>>menu_no;
   switch (menu_no)
 {
 case 1:

  temp = get_data();
  insert_node(&head , &tail , temp);
  break;
 case 2:
  cout<<"\n\nEnter position number wher you want to insert  data:  "; cin>>position_no;
  if (position_no < 0  || position_no == 0 || position_no > Counter + 1 )
  {
   cout<<"You have entered Invalid position number.Plz enter correct Location";
  }

  else
   if( position_no == 1 )
   {
  
    temp = get_data();
    insertion(&head , temp);
   }
   else
    if( position_no < Counter + 1 )
    {
     temp = get_data();
     insertkaar( &head ,temp , position_no  );
   
    }
    else
     if( position_no == Counter + 1 )
    
     {
      temp = get_data();
      Insert_Tail(&tail , temp);
     }
     break;
   
 case 3:

  cout<<"From which Location you want to delete data:   "; cin>> position_no;
  if (position_no < 0  || position_no == 0 || position_no > Counter + 1 )
  {
   cout<<"You have entered Invalid position number.Plz enter correct Location";
  }
  else
   Delete_record( &head , position_no );

  break;
 case 4:
        int Target;
        cout<<"\n\nEnter a ID that u want to find in existing List:   "; cin>> Target;
        Find_ID(head ,  Target);
        break;
       
       
 case 5:
  display_data(head);

  break;



 }

 if (menu_no < 7)
 {
  goto start;

 }
 return 0;
}

No comments:

Post a Comment