Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Singly Linked List
- Illustration of append() function
- By Rijoanul Hasan Shanto
- Reference: www.shafaetsplanet.com/planetcoding/?p=763
- */
- #include <stdio.h>
- #include <stdlib.h>
- struct Node
- {
- int data;
- struct Node *next;
- };
- typedef struct Node node;
- node *head = 0;
- void append(int data)
- {
- if(head == 0)
- {
- head = (node*) malloc(sizeof (node));
- head->data = data;
- head->next = 0;
- }
- else
- {
- node *temp = head;
- while(temp->next != 0)
- {
- temp = temp->next;
- }
- node *newNode = (node*) malloc(sizeof (node));
- newNode->data = data;
- newNode->next = 0;
- temp->next = newNode;
- }
- }
- void display()
- {
- node *temp = (node*) malloc(sizeof (node));
- temp = head;
- while(temp->next != 0)
- {
- printf("%d\n", temp->data);
- temp = temp->next;
- }
- printf("%d\n", temp->data);
- }
- int main()
- {
- int i;
- for(i = 0; i < 5; i++)
- {
- append(i+2);
- }
- display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement