473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Qeustion about linked list

2
I have some question about linked list.
how to complete the delete and search function??


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>
  4. using namespace std;
  5.  
  6.  
  7. struct ProductNode{
  8.     char code[5];
  9.     char description[30];
  10.     double price;
  11.     ProductNode *Next;
  12. };
  13.  
  14. typedef ProductNode *Productptr;
  15.  
  16. void Display(Productptr );
  17. Productptr Search(Productptr, char []);
  18. void Delete(Productptr *, char []);
  19. void Insert(Productptr *, ProductNode);
  20.  
  21. main(){
  22.     Productptr PhoneList,temp;
  23.     ProductNode p;
  24.     int sel;
  25.     char id[5];
  26.  
  27.     PhoneList = NULL;
  28.  
  29.     while (sel != 5){
  30.         system("cls");
  31.  
  32.         cout<<"\t1. Add Item."<<endl;
  33.         cout<<"\t2. Add Delete."<<endl;
  34.         cout<<"\t3. Display Item."<<endl;
  35.         cout<<"\t4. Search."<<endl;
  36.         cout<<"\t5. Exit"<<endl;
  37.         cout<<"\t? : ";
  38.         cin>>sel;
  39.  
  40.         system("cls");
  41.  
  42.         switch (sel){
  43.             case 1: cout<<"Item Code :";
  44.                     cin>>p.code ;
  45.                     cout<<"Description : ";
  46.                     cin>>p.description;
  47.                     cout<<"Price : RM ";
  48.                     cin>>p.price;
  49.                     Insert(&PhoneList, p);
  50.                     break;
  51.  
  52.             case 2: cout<<"Item Code :";
  53.                     cin>>id ;
  54.                     Delete(&PhoneList, id);
  55.                     break;
  56.  
  57.             case 3: Display(PhoneList);
  58.                     break;
  59.  
  60.             //case 4: cout<<"Enter item's Code : ";
  61.             //        cin>>id;
  62.  
  63.             //        temp =  Search(PhoneList, id);
  64.             //        if (temp == NULL)
  65.             //            cout<<"Error : Item not Found."<<endl;
  66.             //        else{
  67.             //               cout<<"Code\tDescription\tPrice\n";
  68.             //            cout<<"====\t===========\t=====\n";
  69.             //            cout<<temp->code<<"\t"
  70.             //                <<temp->description<<"\t\t"
  71.             //                <<temp->price<<endl;
  72.             //            }
  73.                     system("pause");
  74.         }    
  75.     }
  76.     return 0;
  77. }
Dec 6 '07 #1
3 1299
Meetee
931 Expert Mod 512MB
I have some question about linked list.
how to complete the delete and search function??

}
That will be equal to giving you a source code. Better you refer the following link for explanation of Linked list. Google for further information.

Linked List Tutorial

Regards
Dec 6 '07 #2
Jakiro
2
if i get something like this .
no error but can't delete the item i want .
somone can help me toubleshoot?



void Delete(Productptr *r, char id[5])
{
Productptr *temp;
Productptr *temp2;
bool found;
if(r==NULL)
cout<<"Cannot delete from and empty list."<<endl;
else
{
temp=r;
found= false;

while(temp!=NULL && !found)
if (strcmp((*temp)->code,id)<=0)
found=true;
else
{
temp2=temp;
(*temp)=(*temp)->Next;
}
if(temp==NULL)
cout<<"Item to be deleted is not in the list."<<endl;
else
if(strcmp((*temp)->code,id)==0)
{
if(r==temp)
{
(*r)=(*r)->Next;
delete temp;
}
else
{
(*temp2)->Next=(*temp)->Next;
delete temp;
}
}
else
cout<<"Item to be deleted not in list."<<endl;
}

}
Dec 6 '07 #3
Hi,

1. It will be better if you posted code within the CODE Tags.


Expand|Select|Wrap|Line Numbers
  1. void Delete(Productptr *r, char id[5])
  2. {
  3. Productptr *temp;
  4. Productptr *temp2;
  5. bool found;
  6. if(r==NULL)
  7.   cout<<"Cannot delete from and empty list."<<endl;
  8. else
  9. {
  10.   temp=r;
  11.   found= false;
  12.  
  13.   while(temp!=NULL && !found)
  14.   if (strcmp((*temp)->code,id)<=0)
  15.     found=true;
  16.   else
  17.   {
  18.     temp2=temp;
  19.     (*temp)=(*temp)->Next;
  20.   }
  21.   if(temp==NULL)
  22.   cout<<"Item to be deleted is not in the list."<<endl;
  23.   else
  24.   if(strcmp((*temp)->code,id)==0)
  25.    {
  26.      if(r==temp)
  27.      {
  28.        (*r)=(*r)->Next;
  29.        delete temp;
  30.      }
  31.      else
  32.      {
  33.         (*temp2)->Next=(*temp)->Next;
  34.         delete temp;
  35.      }
  36.    }
  37.    else
  38.    cout<<"Item to be deleted not in list."<<endl;
  39.  }
  40.  
  41. }
2. Is this exactly the code you have tried?? Because, the while loop does not have a begin brace...

3. What does
Expand|Select|Wrap|Line Numbers
  1. if (strcmp((*temp)->code,id)<=0)
mean in this context?? Shouldn't it be just
Expand|Select|Wrap|Line Numbers
  1. if (strcmp((*temp)->code,id)==0)
4. Shouldn't the control return to main() once r==NULL?
Dec 6 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: C++fan | last post by:
Suppose that I define the following class: class example_class{ public: example_class(); void funtion_1(); void function_2(); protected:
5
by: Dream Catcher | last post by:
1. I don't know once the node is located, how to return that node. Should I return pointer to that node or should I return the struct of that node. 2. Also how to do the fn call in main for that...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
6
by: Steve Lambert | last post by:
Hi, I've knocked up a number of small routines to create and manipulate a linked list of any structure. If anyone could take a look at this code and give me their opinion and details of any...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
12
by: joshd | last post by:
Hello, Im sorry if this question has been asked before, but I did search before posting and couldnt find an answer to my problem. I have two classes each with corresponding linked lists, list1...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
7
by: QiongZ | last post by:
Hi, I just recently started studying C++ and basically copied an example in the textbook into VS2008, but it doesn't compile. I tried to modify the code by eliminating all the templates then it...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.