Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- struct node
- {
- int data;
- struct node *next;
- };
- typedef struct node *list;
- list insert (list l, int data); //insert function adds a new node at the beginning of the list
- void printL(list l); //prints the values of the list
- int main()
- {
- list l = NULL;
- insert(l,8);
- insert(l,9);
- printL (l);
- }
- list insert(list l, int data)
- {
- int k=0;
- list p; //node that is going to be inserted
- list cur=l;
- //counts how many nodes are in the list
- while(cur!=NULL)
- {
- cur=cur->next;
- k++;
- }
- if (k=5) //if there are 5 nodes the list is full and no more nodes can be added
- {
- printf("list is full!\n");
- return l;
- }
- else //else add the node at the beginning
- {
- p=malloc(sizeof(list));
- p->data=data;
- p->next=l;
- l=p;
- printf("%d has been inserted\n", data);
- return l;
- }
- }
- void printL(list l)
- {
- if(l==NULL)
- {
- printf("END\n"); //if the list is empty print END
- }
- else
- {
- printf("%d ->", l->data);
- printL(l->next);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement