Skip to content

Instantly share code, notes, and snippets.

@mattbriancon
Created July 30, 2012 03:49
Show Gist options
  • Save mattbriancon/3204201 to your computer and use it in GitHub Desktop.
Save mattbriancon/3204201 to your computer and use it in GitHub Desktop.
#include <stdio.h>
struct entry
{
int value;
struct entry *next;
};
void insertEntry (struct entry *newPtr, struct entry *beforePtr)
{
newPtr->next = beforePtr;
beforePtr = newPtr;
}
void insertAfter(struct entry * newEntry, struct entry * targetEntry) {
newEntry->next = targetEntry->next;
targetEntry->next = newEntry;
}
int main (void)
{
void insertEntry (struct entry *newPtr, struct entry *beforePtr);
struct entry n1, n2, n3, n2_3;
struct entry *listPtr = &n1, *entryPtr = &n2_3;
n1.value = 100;
n2.value = 200;
n3.value = 300;
n2_3.value = 250;
/*set up linked list */
n1.next = &n2;
n2.next = &n3;
n3.next = 0;
printf ("The linked list before adding another entry is:\n");
while (listPtr != (struct entry *) 0 )
{
printf ("%i\n", listPtr->value);
listPtr = listPtr->next;
}
printf ("The linked list after adding another entry is:\n");
// insertEntry (entryPtr, n2.next);
insertAfter(entryPtr, &n2);
/*reset to start of list */
listPtr = &n1;
while (listPtr != (struct entry *) 0 )
{
printf ("%i\n", listPtr->value);
listPtr = listPtr->next;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment