Last active
February 28, 2016 14:57
-
-
Save x5lcfd/3adf56afd6f938d87030 to your computer and use it in GitHub Desktop.
linked list 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
void delete_node ( struct node** head, elem_type x ) | |
{ | |
for ( struct node** curr = head; *curr; ) | |
{ | |
struct node* entry = *curr; | |
if ( entry->elem == x ) { | |
*curr = entry->next; | |
free ( entry ); | |
} else { | |
curr = &entry->next; | |
} | |
} | |
} |
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; | |
}; | |
typedef struct node node; | |
void reverse_linked_list(node* head) | |
{ | |
node* curr = head->next; | |
node* latter = curr->next; | |
curr->next = NULL; | |
for (; latter; ) | |
{ | |
node* entry = latter; | |
latter = entry->next; | |
entry->next = curr; | |
curr = entry; | |
} | |
head->next = curr; | |
} | |
void traverse_linked_list(node* head) | |
{ | |
for (node* h = head; h->next != NULL;) | |
{ | |
h = h->next; | |
printf("%d ", h->val); | |
} | |
} | |
int main(void) | |
{ | |
node *head; | |
node *prev = head; | |
for (int i = 0; i < 10; i++) | |
{ | |
node *n = (node*)malloc(sizeof(node)); | |
n->val = i; | |
n->next = NULL; | |
prev->next = n; | |
prev = prev->next; | |
} | |
// traverse_linked_list(head); | |
reverse_linked_list(head); | |
traverse_linked_list(head); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment