Created
November 4, 2015 20:49
-
-
Save toboqus/ca62844d2f59902cda95 to your computer and use it in GitHub Desktop.
Linked list in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * LinkedList.c | |
| * | |
| * Created on: 4 Nov 2015 | |
| * Author: Alex | |
| */ | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| struct node { | |
| int val; | |
| struct node* next; | |
| }; | |
| int length(struct node* head){ | |
| struct node* current = head; | |
| int count = 0; | |
| while(current != NULL){ | |
| count++; | |
| current = current->next; | |
| } | |
| return count; | |
| } | |
| void push(struct node** headRef, int val){ | |
| struct node* newNode = malloc(sizeof(struct node)); | |
| newNode->val = val; | |
| newNode->next = *headRef; | |
| *headRef = newNode; | |
| } | |
| void addLast(struct node** headRef, int val){ | |
| struct node* current = *headRef; | |
| struct node* newNode; | |
| newNode = malloc(sizeof(struct node)); | |
| newNode->val = val; | |
| newNode->next = NULL; | |
| if(current == NULL){ | |
| *headRef = newNode; | |
| }else{ | |
| while(current->next != NULL){ | |
| current = current->next; | |
| } | |
| current->next = newNode; | |
| } | |
| } | |
| void printList(struct node* head){ | |
| struct node* current = head; | |
| while(current != NULL){ | |
| printf("%i,",current->val); | |
| current = current->next; | |
| } | |
| printf("\n"); | |
| } | |
| struct node* copyList(struct node* head){ | |
| struct node* current = head; | |
| struct node* new = NULL; | |
| struct node* tail = NULL; | |
| while(current != NULL){ | |
| if(new == NULL){ | |
| new = malloc(sizeof(struct node)); | |
| new->val = current->val; | |
| new->next = NULL; | |
| tail = new; | |
| }else{ | |
| tail->next = malloc(sizeof(struct node)); | |
| tail = tail->next; | |
| tail->val = current->val; | |
| tail->next = NULL; | |
| } | |
| current = current->next; | |
| } | |
| return new; | |
| } | |
| struct node* copyList2(struct node* head){ | |
| struct node* current = head; | |
| struct node* new = NULL; | |
| struct node* tail = NULL; | |
| while(current != NULL){ | |
| if(new == NULL){ | |
| push(&new, current->val); | |
| tail = new; | |
| }else{ | |
| push(&(tail->next),current->val); | |
| tail = tail->next; | |
| } | |
| current = current->next; | |
| } | |
| return new; | |
| } | |
| struct node* copyList3(struct node* head){ | |
| //using dummy node; | |
| struct node* current = head; | |
| struct node* tail = NULL; | |
| struct node dummy; | |
| dummy.next = NULL; | |
| tail = &dummy; | |
| while(current != NULL){ | |
| push(&(tail->next),current->val); | |
| tail = tail->next; | |
| current = current->next; | |
| } | |
| return dummy.next; | |
| } | |
| int main(){ | |
| struct node* list = NULL; | |
| push(&list, 4); | |
| push(&list, 5); | |
| push(&list, 6); | |
| addLast(&list, 10); | |
| addLast(&list, 11); | |
| addLast(&list, 12); | |
| printList(list); | |
| printf("length of list: %i\n", length(list)); | |
| struct node* copy = copyList3(list); | |
| printf("copy list: \n"); | |
| printList(copy); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment