Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node
- {
- int val;
- struct node *next;
- }node;
- void insert(int input);
- void print(void);
- node *first[10];
- int main(void)
- {
- for (int i = 0; i < 10; i++)
- {
- insert(i);
- }
- print();
- }
- void print(void)
- {
- node *curr = first[1];
- while (curr != NULL)
- {
- printf("%d\t", curr -> val);
- curr = curr -> next;
- }
- printf("\n");
- }
- // takes the number to be inputted. aslo calculates the right position to insert into
- void insert(int input)
- {
- // calculate which position to insert into
- int index = 1;
- node* new = malloc(sizeof(node));
- new->val = input;
- // insertion in case of empty list
- if (first[index] == NULL)
- {
- first[index] = new;
- new->next = NULL;
- return;
- }
- // if list not empty, insert at front
- else
- {
- // in case number already exists at head of list
- if (first[index]->val == input)
- {
- return;
- }
- new->next = first[index];
- first[index] = new;
- return;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement