Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node {
- int data;
- struct Node* next;
- }*head = NULL;
- void add(int arr[], int n, struct Node** head)
- {
- struct Node* first;
- first = new Node;
- first->data = arr[0];
- first->next = NULL;
- struct Node* prev = first;
- for (int i = 1; i < n; i++) {
- struct Node* t = new Node;
- t->data = arr[i];
- t->next = NULL;
- prev->next = t;
- prev = t;
- }
- (*head) = first;
- prev->next = first;
- }
- void display(struct Node* p)
- {
- struct Node* t = p;
- do {
- cout << p->data << endl;
- p = p->next;
- } while (p != t);
- }
- int main()
- {
- int arr[] = { 10, 20, 30 };
- add(arr, sizeof(arr) / sizeof(arr[0]), &head);
- display(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement