Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int size;
- struct node
- {
- int orderId;
- string flavour;
- node *next;
- };
- class list
- {
- private:
- node *head, *tail;
- public:
- list()
- {
- head=NULL;
- tail=NULL;
- }
- void display() //print out list
- {
- node *temp=new node;
- temp=head;
- if(head==NULL)
- {
- cout<<"You haven't ordered any cake(s)"<<endl;
- }
- while(temp!=NULL)
- {
- cout<<temp->orderId<<"\t"<<temp->flavour<<endl;
- temp=temp->next;
- }
- }
- void createnode(int orderId_value, string flavour_value) //create node at last position
- {
- node *temp=new node;
- temp->orderId=orderId_value;
- temp->flavour=flavour_value;
- temp->next=NULL;
- if(head==NULL)
- {
- head=temp;
- tail=temp;
- temp=NULL;
- }
- else
- {
- tail->next=temp;
- tail=temp;
- }
- size++;
- }
- bool update_data(int key) //update node data with given position
- {
- node *current=new node;
- current=head;
- string updated_flavour;
- if(head==NULL)
- {
- cout<<"No list"<<endl;
- }
- cout<<"Enter new flavour"<<endl;
- cin>>updated_flavour;
- for(int i=1;i<key;i++){
- current = current->next;
- }
- current->flavour=updated_flavour;
- }
- void delete_position(int pos) //delete node with given position
- {
- node *current=new node;
- node *previous=new node;
- current=head;
- if(head==NULL)
- {
- cout<<"You haven't ordered any cake(s)"<<endl;
- }
- for(int i=1;i<pos;i++)
- {
- previous=current;
- current=current->next;
- }
- previous->next=current->next;
- if(pos == 1){
- node *temp=new node;
- temp=head;
- head=head->next;
- delete temp;
- }
- size--;
- }
- };
- int main()
- {
- int orderId_input=0, destroy_input, position_input,checker;
- string flavour_input;
- list list; //created a list named list.
- list.display();
- cout<<"====Take 1===="<<endl;
- cout<<"Enter flavour for cake!"<<endl;
- cin>>flavour_input;
- orderId_input++;
- list.createnode(orderId_input,flavour_input);
- list.display();
- cout<<"====Take 2===="<<endl;
- cout<<"Enter flavour for cake!"<<endl;
- cin>>flavour_input;
- orderId_input++;
- list.createnode(orderId_input,flavour_input);
- list.display();
- cout<<"====Take 3===="<<endl;
- cout<<"Enter flavour for cake!"<<endl;
- cin>>flavour_input;
- orderId_input++;
- list.createnode(orderId_input,flavour_input);
- list.display();
- cout<<"====destroy node===="<<endl;
- cout<<"Which cake would you like to destroy?"<<endl;
- do{
- cin>>destroy_input;
- }while(destroy_input > size);
- list.delete_position(destroy_input);
- cout<<"====Result after destroy===="<<endl;
- list.display();
- cout<<"There are currently "<<size<<" cake(s)"<<endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement