Skip to content

Instantly share code, notes, and snippets.

@adohe-zz
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save adohe-zz/c92827ba790b664cac95 to your computer and use it in GitHub Desktop.

Select an option

Save adohe-zz/c92827ba790b664cac95 to your computer and use it in GitHub Desktop.
A list implementation in C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int elementType;
typedef struct Node {
elementType d;
Node* next;
} Node;
void initial(Node **pHead) {
*pHead = NULL;
printf("initial success");
}
Node* create(Node* pHead) {
Node* p1;
Node* p2;
p1 = p2 = (Node*)malloc(sizeof(Node));
if (p1 == NULL || p2 == NULL) {
printf("malloc fail...");
exit(0);
}
memset(p1, 0, sizeof(Node));
scanf("%d", &p1->d);
p1->next = NULL;
while (p1->d > 0) {
if (pHead == NULL) {
pHead = p1;
} else {
p2->next = p1;
}
p2 = p1;
p1 = (Node*)malloc(sizeof(Node));
if (p1 == NULL || p2 == NULL) {
exit(0);
}
memset(p1, 0, sizeof(Node));
scanf("%d", &p1->d);
p1->next = NULL;
}
return pHead;
}
void printList(Node *pHead) {
if (pHead == NULL) {
printf("List is empty...");
exit(0);
}
while(pHead != NULL) {
printf("%d", pHead->d);
pHead = pHead->next;
}
}
bool isExistsLoop(Node *pHead) {
Node *slow = pHead;
Node *fast = pHead;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
break;
}
return !(fast == NULL || fast->next == NULL);
}
Node* findLoopPort(Node *pHead) {
Node *slow = pHead;
Node *fast = pHead;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
break;
}
if (fast == NULL || fast->next == NULL) {
return NULL;
}
slow = pHead;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
void clearList(Node* pHead) {
if (pHead == NULL) {
printf("List is empty");
return;
}
Node *pNext;
while (pHead != NULL) {
pNext = pHead->next;
free(pHead);
pHead = pNext;
}
}
int sizeList(Node* pHead) {
int size = 0;
while (pHead != NULL) {
size ++;
pHead = pHead->next;
}
return size;
}
int isEmptyList(Node *pHead) {
if (pHead == NULL) {
printf("List is empty");
return 1;
}
return 0;
}
elementType getPosElement(Node *pHead, int pos) {
int i = 0;
if (pos < 1) {
printf("position argument illegal...");
return 0;
}
if (pHead == NULL) {
printf("List is empty...");
return 0;
}
while (pHead != NULL) {
++ i;
if (i == pos) {
break;
}
pHead = pHead->next;
}
if (i < pos) {
printf("position out of bounds...");
return 0;
}
return pHead->d;
}
elementType *getElementAddr(Node *pHead, elementType x) {
if (pHead == NULL) {
printf("List is empty...");
return NULL;
}
if (x < 0) {
printf("No such element...");
return NULL;
}
while ((pHead->d != x) && (pHead->next != NULL)) {
pHead = pHead->next;
}
if ((pHead != NULL) && (pHead->d != x)) {
printf("No such element...");
return NULL;
}
return &(pHead->d);
}
int modifyEle(Node *pNode, int pos, elementType x) {
Node *pHead;
pHead = pNode;
int i = 0;
if (pHead == NULL) {
printf("List is empty...");
return 0;
}
if (pos < 1) {
printf("Position argument wrong...");
return 0;
}
while (pHead != NULL) {
++ i;
if (i == pos) {
break;
}
pHead = pHead->next;
}
if (i < pos) {
printf("Position argument wrong...");
return 0;
}
pNode = pHead;
pNode->d = x;
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment