Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // we can print it both in forward and reversed order by only slightly adjusting the Print function
- #include <stdio.h>
- #include <stdlib.h>
- struct Node {
- int data;
- struct Node *next;
- };
- void Print(struct Node* p)
- {
- if(p == NULL) return; // duno na rekursiqta
- printf("%d ", p->data); // print the first
- Print(p->next); // recursively call the next one
- }
- struct Node* Insert(struct Node* head, int data)
- {
- struct Node* temp = (struct Node*) malloc(sizeof(struct Node)); // create node
- temp->data = data; // store data in newly created node
- temp->next = NULL; // set it to initially point to null
- if(head == NULL) head = temp; // if empty
- else { // if not empty
- struct Node* temp1 = head; // set new pointer to the start
- while(temp1->next != NULL) temp1 = temp1->next; // go to the last
- temp1->next = temp; // set it to point to the new one
- }
- return head;
- }
- int main()
- {
- struct Node* head = NULL;
- head = Insert(head,5);
- head = Insert(head, 8);
- head = Insert(head, 11);
- head = Insert(head, 14);
- Print(head);
- return 0;
- }
- // Print in reverse order:
- // void Print(struct Node* p)
- //{
- // if(p == NULL) return;
- // Print(p->next);
- // printf("%d ", p->data);
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement