Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- struct node {
- int value;
- struct node *next;
- };
- typedef struct node *tlista;
- void print_list(tlista head) {
- tlista current = head;
- if(current == NULL){
- printf("NULL\n");
- }
- while (current != NULL) {
- printf("%d -> ", current->value);
- current = current->next;
- if(!current){
- printf("NULL\n");
- }
- }
- printf("\n");
- }
- void pushLast(tlista head){
- tlista current = head;
- while (current->next != NULL){
- current = current->next;
- }
- current->next = head;
- }
- void push(tlista head, int val) {
- tlista current = head;
- while (current->next != NULL) {
- current = current->next;
- }
- /* now we can add a new variable */
- current->next = (tlista)malloc(sizeof(struct node));
- current->next->value = val;
- current->next->next = NULL;
- }
- void free_list(tlista head) {
- tlista tmp;
- while (head != NULL) {
- tmp = head;
- head = head->next;
- free(tmp);
- }
- }
- int main(){
- tlista l1 = NULL;
- l1 = malloc(sizeof(struct node));
- l1->value = 4;
- l1->next = NULL;
- push(l1, 6);
- push(l1, 2);
- push(l1, 6);
- push(l1, 7);
- push(l1, 9);
- pushLast(l1);
- printf("lista1: ");
- print_list(l1);
- free(l1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement