Skip to content

Instantly share code, notes, and snippets.

@merrypuck
Created November 17, 2013 20:20
Show Gist options
  • Save merrypuck/7517796 to your computer and use it in GitHub Desktop.
Save merrypuck/7517796 to your computer and use it in GitHub Desktop.
LinkedList implementation in C.
#include <stdio.h>
#include <stdlib.h>
// Define node struct type
struct node
{
int data;
struct node *next;
};
typedef struct node Node;
int length(struct node*head)
{
int count = 0;
struct node*current = head;
while(current != NULL)
{
count++;
current = current -> next;
}
return count;
}
int getNth(struct node*head, int key)
{
int positionCount = 0;
struct node*current = head;
while(current != NULL)
{
if(positionCount == key) {
return(current->data);
}
positionCount++;
current = current->next;
}
return(0);
}
Node insertNth(struct node*head, int position, int insertData)
{
int count = 0;
struct node*current = head;
struct node*replacement;
replacement = (Node *)malloc(sizeof(Node));
struct node*temp = NULL;
while(current != NULL)
{
if(position == 0) {
replacement->next = head;
replacement->data = insertData;
head = replacement;
return *replacement;
}
if(count + 1 == position) {
temp = current->next;
printf("Temp data is: %d\n", temp->data);
current->next = replacement;
replacement->next = temp;
replacement->data = insertData;
return *replacement;
}
current = current->next;
count++;
}
}
/*
void sortedInsert(struct node*head, struct node*newNode )
{
struct node*current1 = head;
struct node*current2 = (Node *)malloc(sizeof(Node));
while(current1 != NULL) {
current2 = current1->next;
if(current2 == NULL) {
current1->next = newNode;
newNode->
}
if(newNode->data >= current1->data && newNode->data <= current2->data) {
current1->next = newNode;
newNode->next = current2;
}
}
}
*/
void deleteList(struct node*head)
{
struct node*current1;
struct node*current2;
current1 = head;
while(current1 != NULL) {
current2 = current1;
current1 = current1->next;
free(current2);
} f
ree(current1);
}
Node pop(struct node*head)
{
struct node*current;
current = head;
head = head->next;
free(current);
return *head;
}
/* -------------------------
main() - Initializes linkedlist with 214 links
------------------------- */
int main()
{
Node *current1, *current2, *head, *tail;
tail = NULL;
//create new space for currents
current2 = (Node *)malloc(sizeof(Node));
int i;
for(i = 1; i < 215; i++) {
current1 = current2;
// Assign value to data
current1->data = i;
current2 = (Node *)malloc(sizeof(Node));
// Point to last memory spot or if current first spot, points to null
current1->next = current2;
// Going to look like: head(1) -> 2 -> 3 -> 4 -> 5 ... -> 214 -> tail(NULL)
if(i == 1){
// head is start of list
head = current1;
}
else if(i == 214) {
tail = current2;
}
}
// Inserts new link into linked list
// insertNth(node*head, int position, int insertData)
insertNth(head, 113, 93);
// returns value at a position in linked list else returns 0
// getNth(struct node*head, int key)
int positionValue = getNth(head, 13);
/*
while(current1 != NULL) {
printf("Current data while looping through linkedlist : %d\n", current1-> data);
current1 = current1->next;
}
printf("length of list: %d\n", length(head));*/
return 0;
}
@junghoon59
Copy link

junghoon59 commented Sep 20, 2016

you decided to typedeg struct node ->Node, but didn't use it....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment