Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct node
- {
- int data;
- struct node *next;
- };
- int main()
- {
- struct node *ptr, *newNode;
- int i = 1;
- int num;
- struct node * headptr;
- headptr = NULL;
- printf("Enter 1 to make a head-appended linked list.\nEnter 2 to make a tail-appended linked list.\n");
- scanf("%d", &num);
- while (i != 0)
- {
- ptr = (struct node *)malloc(sizeof(struct node));
- printf("Enter a Value: ");
- scanf("%d", &ptr -> data);
- if (headptr == NULL)
- {
- headptr = ptr;
- headptr -> next = NULL;
- }
- else if (headptr != NULL && num == 1)
- {
- ptr -> next = headptr;
- headptr = ptr;
- }
- else if (headptr != NULL && num == 2)
- {
- newNode = (struct node *)malloc(sizeof(struct node));
- while(ptr -> next != NULL) {
- ptr = ptr -> next; }
- newNode -> next = NULL;
- ptr -> next = newNode;
- }
- printf("Do you want to add another number?\n 1 = yes 0 = no || ");
- scanf("%d", &i);
- }
- ptr = headptr;
- while (ptr)
- {
- printf("%d ", ptr->data);
- ptr = ptr->next;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement