Created
September 17, 2012 10:08
-
-
Save atneik/3736542 to your computer and use it in GitHub Desktop.
A basic singly Linked List with addAtEnd(), addAtBeg(), display() and more operations.
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
struct node{ | |
int val; | |
struct node *next; | |
}; | |
int addAtEnd(struct node **h, int x){ | |
if(*h == NULL){ //If the list is empty, add at the head and return. | |
*h = (struct node*)malloc(sizeof(struct node)); | |
(*h)->val = x; | |
(*h)->next = NULL; | |
return 0; | |
} | |
struct node *temp = *h; | |
while (temp->next!=NULL) { //Traverse to the end of the list. | |
temp = temp->next; | |
} | |
temp->next = (struct node*)malloc(sizeof(struct node)); //Create a node at ptr->next, which is NULL. | |
temp->next->val = x; | |
temp->next->next = NULL; | |
return 0; | |
} | |
int addAtBeg(struct node **h, int x){ | |
if(*h == NULL){ //If the list is empty, add at the head and return. | |
*h = (struct node*)malloc(sizeof(struct node)); | |
(*h)->val = x; | |
(*h)->next =NULL; | |
return 0; | |
} | |
struct node *ptr, *temp = *h; | |
ptr = (struct node*)malloc(sizeof(struct node)); //Create a node and then point the head to it. | |
ptr->val = x; | |
ptr->next = NULL; | |
*h = ptr; | |
(*h)->next = temp; | |
return 0; | |
} | |
int addAtBegArray(struct node **h, int array[], int size){ | |
int i; | |
for (i=size-1; i>=0; i--) { | |
addAtBeg(h,array[i]); | |
} | |
return 0; | |
} | |
int addAtEndArray(struct node **h, int array[], int size){ | |
int i; | |
for (i=0; i<size; i++) { | |
addAtEnd(h,array[i]); | |
} | |
return 0; | |
} | |
void display(struct node *h){ | |
if (h == NULL) { | |
printf("Empty"); | |
} | |
while (h!=NULL) { //Traverse and print the value till the current node is not NULL. | |
printf("%d ",h->val); | |
h = h->next; | |
} | |
printf("\n"); | |
} | |
int getSize(struct node *h){ | |
int count = 0; | |
if (h == NULL) { | |
return -1; | |
} | |
while (h!=NULL) { //Traverse and get count. | |
h = h->next; | |
count++; | |
} | |
return count; | |
} | |
int getKthVal(struct node *h, int k){ | |
int count = 0, val; | |
while (h!=NULL) { //Traverse and get count. | |
count++; | |
if (count == k) { | |
return h->val; | |
} | |
h = h->next; | |
} | |
return -1; | |
} | |
struct node *concatList(struct node **l1, struct node **l2){ | |
struct node *temp; | |
temp = *l1; | |
if(*l1 == NULL) | |
{ | |
return *l2; | |
} | |
while (temp->next!=NULL) { | |
temp = temp->next; | |
} | |
temp->next = *l2; | |
return *l1; | |
} | |
struct node *copyList(struct node *l1){ | |
struct node *temp=NULL; | |
while (l1!=NULL) { | |
addAtEnd(&temp,l1->val); | |
l1=l1->next; | |
} | |
return temp; | |
} | |
void partitionArnd(struct node **h, int x){ | |
struct node *ll_gt = NULL, *ll_lt = NULL; | |
struct node *temp, *temp_b; | |
temp = *h; | |
while (temp!=NULL) { | |
if (x <= temp->val) { | |
addAtEnd(&ll_gt, temp->val); | |
}else{ | |
addAtEnd(&ll_lt, temp->val); | |
} | |
temp_b = temp; | |
temp = temp->next; | |
} | |
//display(ll_lt); | |
//display(ll_gt); | |
while ((*h)!=NULL) { //free old list | |
temp=(*h); | |
(*h) = (*h)->next; | |
free(temp); | |
} | |
*h = concatList(&ll_lt, &ll_gt); | |
} | |
void sort(struct node **h){ | |
struct node *head_copy = NULL; | |
int i; | |
head_copy = copyList(*h); | |
for (i=0; i<getSize(head_copy); i++) { //sorting O(n2) | |
partitionArnd(h,getKthVal(head_copy, i+1)); | |
//display(*h); | |
} | |
} | |
int delKthVal(){ | |
} | |
int main(){ | |
struct node *head = NULL; //set as NULL or else it may break while adding | |
int array[]={5,7,90,-7}; | |
addAtBeg(&head,2); | |
addAtEnd(&head,0); | |
addAtEnd(&head,-5); | |
addAtEnd(&head,9); | |
addAtBeg(&head,4); | |
addAtBeg(&head,1); | |
display(head); | |
addAtBegArray(&head,array,sizeof(array)/sizeof(int)); | |
display(head); | |
sort(&head); | |
display(head); | |
return 0; | |
} |
This file contains 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
1 4 2 0 -5 9 | |
5 7 90 -7 1 4 2 0 -5 9 | |
-7 -5 0 1 2 4 5 7 9 90 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment