Skip to content

Instantly share code, notes, and snippets.

@98chimp
Created March 12, 2015 01:23
Show Gist options
  • Save 98chimp/66544f220f3f94c91693 to your computer and use it in GitHub Desktop.
Save 98chimp/66544f220f3f94c91693 to your computer and use it in GitHub Desktop.
Linked Lists
//
// main.c
// Linked Lists
//
// Created by Shahin on 2015-03-11.
// Copyright (c) 2015 98% Chimp. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
typedef struct linkedListItem {
int data;
struct linkedListItem *nextListItem;
} LinkedListItem;
//typedef struct linkedListItem LinkedListItem;
//
//struct linkedListItem {
// int data;
// LinkedListItem *nextListItem;
//};
LinkedListItem *createChainLink(){
LinkedListItem *chainLink = (LinkedListItem *)malloc(sizeof(LinkedListItem));
return chainLink;
}
void destroychainLink(LinkedListItem *aChainLink ){
if (aChainLink != NULL){
free(aChainLink);
aChainLink = NULL;
}
}
void appendChainLink(LinkedListItem *previousLink, LinkedListItem *nextLink){
if (previousLink != NULL && nextLink != NULL){
// (*linkedList1).nextListItem = linkedList2;
previousLink->nextListItem = nextLink;
}
}
void removeLinkFromLink(LinkedListItem *linkToBeRemoved, LinkedListItem *fromItem){
if (linkToBeRemoved != NULL && fromItem != NULL){
fromItem->nextListItem = linkToBeRemoved->nextListItem;
destroychainLink(linkToBeRemoved);
}
}
void removeAll(){
}
LinkedListItem *createList(unsigned int numberOfLinks){
if ( numberOfLinks == 0){
return NULL;
}
LinkedListItem *rootItem = createChainLink();
LinkedListItem *previousItem = rootItem;
for (int count = 0; count < numberOfLinks; count++){
previousItem->data = count;
LinkedListItem *linkItem = createChainLink();
appendChainLink(previousItem, linkItem);
previousItem = linkItem;
}
return rootItem;
}
int main(int argc, const char * argv[]) {
LinkedListItem *rootItem = createList(10);
printf("%d\n", rootItem->nextListItem->data);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment