fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Node {
  4. int data;
  5. struct Node*next;
  6. };
  7. struct Node*createNote(int data){
  8. struct Node*newNode =(struct Node*)malloc(sizeof(struct Node));
  9. if(!newNode) {
  10. printf("Memory allocation error!\n");
  11. exit(1);
  12. }
  13. newNode->data=data;
  14. newNode->next=NULL;
  15. return newNode;
  16. }
  17. void insertAtEnd(struct Node**head,int data){
  18. struct Node*newNode = createNote(data);
  19. if(*head==NULL){
  20. *head=newNode;
  21. } else{
  22. struct Node*temp=*head;
  23. while(temp->next!=NULL)
  24. temp=temp->next;
  25. }
  26. }
  27. void displayList(struct Node*head){
  28. if(head==NULL){
  29. printf("List is empty.\n");
  30. }else {
  31. struct Node*temp=head;
  32. while(temp!=NULL){
  33. printf("%d->",temp->data);
  34. temp=temp->next;
  35. }
  36. printf("NULL\n");
  37. {
  38.  
  39. void freeList(struct Node*head) {
  40. struct Node*temp;
  41. while(head!=NULL) {
  42. temp=head;
  43. head=head->next;
  44. free(temp);
  45. }
  46. }
  47. int main() {
  48. struct Node*head=NULL;
  49. insertAtEnd(&head,10);
  50. insertAtEnd(&head,20);
  51. insertAtEnd(&head,30);
  52. printf("Linked list:");
  53. displayList(head);
  54. freeList(head);
  55. return 0;
  56. }
Success #stdin #stdout 0.03s 25628KB
stdin
Standard input is empty
stdout
#include <stdio.h>
#include <stdlib.h>
struct Node {
    int data;
    struct Node*next;
};
struct Node*createNote(int data){
    struct Node*newNode =(struct Node*)malloc(sizeof(struct Node));
    if(!newNode) {
        printf("Memory allocation error!\n");
        exit(1);
    }
    newNode->data=data;
    newNode->next=NULL;
    return newNode;
}
void insertAtEnd(struct Node**head,int data){
    struct Node*newNode = createNote(data);
    if(*head==NULL){
        *head=newNode;
       } else{
            struct Node*temp=*head;
            while(temp->next!=NULL) 
                temp=temp->next;
       }
        }
        void displayList(struct Node*head){
            if(head==NULL){
                printf("List is empty.\n");
            }else {
                struct Node*temp=head;
                while(temp!=NULL){
                    printf("%d->",temp->data);
                    temp=temp->next;
                }
                printf("NULL\n");
            {
        
        void freeList(struct Node*head) {
            struct Node*temp;
            while(head!=NULL) {
                temp=head;
                head=head->next;
                free(temp);
}
}
int main() {
    struct Node*head=NULL;
    insertAtEnd(&head,10);
    insertAtEnd(&head,20);
    insertAtEnd(&head,30);
    printf("Linked list:");
    displayList(head);
    freeList(head);
    return 0;
}