adding numbers to list without predefined variables
I took a look through my previous post and learned how to add the add data to the list (but only when i have the variables predefined).
What i mean by predifined is when i have made head, first,second,third and tail nodes to store them. My question is how would i create a list without the nodes predifined? Also, to get a little more specific, without using an STL, list, stack, vector, Only by using pointers. (the STL and others are a little advanced for me at the moment)
here is what i have started. My question is with the insert() function.
Code:
#include<iostream>
using namespace std;
/* 1. Write a working program.
2. This program will insert and retrieve
3. You have to define the linked list and the nodes.
4. The linked list will be sorted, you will have to remember this when doing inserts.
5. The problem defines what the insert and retrieve function headers will return and what will be passed in as parameters.
6. When writing the insert and retrieve, remember that this is recursive.
*/
struct node
{
int data;
node *next;
};
node *head = new node;
// insert a number into the pointer based sorted linked list of integers
// insert numbers into list until 0 is entered
// sorts the list
void insert()
{
node *tmp = new node;
do
{
node *tmp1=new node;
cout << "Enter the numbers to be inserted (0 to end) : " ;
cin >> tmp->data;
tmp->next=head;
head->next=new node;
}while (tmp->data !=0);
}
// Retrieve the numbers based on the sorted list
// you enter the numbers to be retrieved
// 0 to end the function
// and display the position of each in
int retrieval(int *ptr, int item)
{
// return items position or -1 if item is not in list
}
int main() {
insert();
system("pause");
return 0;
}
How do you recover the list
I seem to run into the most problems with lack of clear instructions from books.
I am trying to recover what was or i think being placed into a linked list. I know the concepts of linked list now, but with numbers not having a variable assigned to it, how are the numbers being traversed. I'd rather if someone would be so kind to post a really easy example of how to make one with just a head pointer and traverse it with a temporary pointer.
I have code from previous posts but i dont get too far.