Advertisement
uopspop

Untitled

May 3rd, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct _node{
  5.     int data;
  6.     struct _node *next;
  7. };
  8. typedef struct _node node;
  9.  
  10. int main(){
  11.     node *head, *ptr;
  12.     head = new node;
  13.     ptr = head; // starting point
  14. // initialize data value
  15.     int n, value;
  16.     cout << "how many struct do you need: ";
  17.     cin >> n;
  18.     for (int i = 0; i < n; i++){
  19.         cout << "Please input the value for " << i+1 << " struct: " ;
  20.         cin >> value;
  21.         ptr->data = value;
  22.         ptr->next = new node;
  23.         ptr = ptr->next;   
  24.     }
  25. // print out
  26.     ptr = head;
  27.     for (int i = 0; i < n; i++){
  28.         cout << "the value for " << i+1 << " struct: " << ptr->data << endl;
  29.         ptr = ptr->next;
  30.     }
  31. // free memory
  32.     delete head;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement