Skip to content

Instantly share code, notes, and snippets.

@aa-ahmed-aa
Created March 4, 2016 17:24
Show Gist options
  • Save aa-ahmed-aa/9a98a56b73eb3fe7f192 to your computer and use it in GitHub Desktop.
Save aa-ahmed-aa/9a98a56b73eb3fe7f192 to your computer and use it in GitHub Desktop.
# include <iostream>
using namespace std;
struct node
{
int data;
node *next;
}*head;
void display(struct node *r)
{
r = head;
if (r == NULL)
{
return;
}
while (r != NULL) //loop on the nodes
{
cout << r->data<<" "; //show current node
r = r->next; //move to the next node
}
printf("\n");
}
int main()
{
node *temp;
int value;
//loop to take more than one value
while (true)
{
cin >> value;
//will terminate if you enter 0 to the list
if (value == 0)
break;
temp = new node; //create new node to store new value
temp->data = value; //assign the new value to the temp node to add it later to the list
if (head == NULL) //if the head is null then we are at the begining of the list
{
head = temp; //store the temp node to as the begining of the list
head->next = NULL; // the next node is null
}
else
{
temp->next = head; // store head into the next of the tmp node
head = temp; //assign the node into the head and the head now is the complete list and it is saved as public
}
}
//will display all the data into every node
display(head);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment