Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

need help in this linklist code!!

vnayakvnayak Member Posts: 16
Hi everyone!!
I am trying to write the code of self-organizing linked list.I am trying to learn linklist using C++ by myself.
A self-organizing list will keep the most recently accessed items at the beginning of the list. Thus, the most frequently accessed items should remain at the beginning of the list, and less frequently accessed items should drift to end of the list, making the list more efficient to search. Items are inserted at the beginning of the list, and each time an item is accessed (that is, found) it is deleted from its current position and inserted at the beginning of the list.
Each node in the linked list will contain an id number, a counter and a pointer field. The id number will be an integer.

menu driven using the following options:

1. Insert a new item.
2. Find and print the id and count for a particular item.
3. Delete an item.
4. Print the list in self-organizing order
5. Print the list in reverse self-organizing order .
6. Print the list in sorted order based on the id.

what i did in this code , I have done up to 3 rd item. Can anyone tell me how can i find and print the id and count for a perticular item.
Please try to cooperate with me.Bec i am stuck at this point..pla help me in this code .i will be very thankful to you...[code][/code] #include
using namespace std;


struct node
{ char item[20]; // Name of up to 20 letters
int id; // D.O.B. would be better
int num_items; // In metres
node *nxt;// Pointer to next node
};

node *start_ptr = NULL;
node *current; // Used to move along the list
int option = 0;

void add_node_at_end()
{ node *temp, *temp2; // Temporary pointers

// Reserve space for new node and fill it with data
temp = new node;
cout << "Please enter the item : ";
cin >> temp->item;
cout << "Please enter the ID number of the item: ";
cin >> temp->id;

temp->nxt = NULL;

// Set up link to this node
if (start_ptr == NULL)
{ start_ptr = temp;
current = start_ptr;
}
else
{ temp2 = start_ptr;
// We know this is not NULL - list not empty!
while (temp2->nxt != NULL)
{ temp2 = temp2->nxt;
// Move to next link in chain
}
temp2->nxt = temp;
}
}

void display_list()
{ node *temp;
temp = start_ptr;
cout << endl;
if (temp == NULL)
cout << "The list is empty!" << endl;
else
{ while (temp != NULL)
{ // Display details for what temp points to
cout << "item : " << temp->item << " ";
cout << "ID : " << temp->id << " ";

if (temp == current)
cout << " <-- Current node";
cout << endl;
temp = temp->nxt;

}
cout << "End of list!" << endl;
}
}

void delete_start_node()
{ node *temp;
temp = start_ptr;
start_ptr = start_ptr->nxt;
delete temp;
}

void delete_end_node()
{ node *temp1, *temp2;
if (start_ptr == NULL)
cout << "The list is empty!" << endl;
else
{ temp1 = start_ptr;
if (temp1->nxt == NULL)
{ delete temp1;
start_ptr = NULL;
}
else
{ while (temp1->nxt != NULL)
{ temp2 = temp1;
temp1 = temp1->nxt;
}
delete temp1;
temp2->nxt = NULL;
}
}
}



void search(){
node *tmp;
//char item[20];
int n;
cout<<"
*****Search*****
";
cout<<" Enter data to be searched : ";
cin>> tmp->item;
for(tmp=start_ptr,n=1; tmp!=NULL; tmp=tmp->nxt,n++)
{
if(tmp->nxt== tmp->item)

{
cout<<"
Search is located at "<<n<<" location";
}
else
{

cout<<"
Search Not Found";
}

}
}

int main()
{ start_ptr = NULL;

do
{
display_list();
cout << endl;
cout << "Please select an option : " << endl;
cout << "0. Exit the program." << endl;
cout<<" 1.Insert a new item."<<endl;
cout <<" 2.Find and print the id and count for a particular item."<<endl;
cout <<" 3.Delete an item."<<endl;
// cout <<" 4.Print the list in self-organizing order "<<endl;
// cout << "5.Print the list in reverse self-organizing order "<<endl;
// cout << "6.Print the list in sorted order based on the id. "<<endl;

cout << endl << " >> ";
cin >> option;

switch (option)
{
case 1 : add_node_at_end(); break;
case 2: search();break;
case 3 : delete_start_node();
// case 4:print_list();break;
// case 5:print_reverse();break;
// case 6:print_sortedID();
}
}
while (option != 0);
return 0;
}
I got the errors error C2446: '==' : no conversion from 'char *' to 'node and
error C2440: '==' : cannot convert from 'char [20]' to 'node *'
Plz help me in this code......i will be so thankful to you..

Sign In or Register to comment.