Last active
November 16, 2016 01:55
-
-
Save JeonghunLee/75e64416991d156e325f642153dba2d6 to your computer and use it in GitHub Desktop.
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> | |
typedef struct ListElmt_ { | |
void * data; | |
struct ListElmt_ *next; | |
}ListElmt; | |
typedef struct List_ { | |
int size; | |
ListElmt *head; | |
ListElmt *tail; | |
}List; | |
int list_init(List *plist) | |
{ | |
plist->size=0; | |
plist->head = NULL; | |
plist->tail = NULL; | |
return 0; | |
} | |
int list_ins_last(List *plist, void *pdata) | |
{ | |
int ret=0; | |
ListElmt *pNewElemt,*pNext; | |
if( plist->size >= 0 ){ | |
pNewElemt = (ListElmt *) malloc( sizeof(ListElmt) ); | |
pNewElemt->data = pdata; // last node address | |
pNewElemt->next = NULL; | |
if(plist->tail == NULL || plist->head == NULL){ // first insert elements | |
plist->head = pNewElemt; | |
plist->tail = pNewElemt; | |
}else { // second insert elements | |
if( plist->head != NULL){ | |
plist->tail->next = pNewElemt; | |
plist->tail = pNewElemt; | |
}else | |
ret=-2; //error 1 | |
} | |
plist->size++; | |
if(ret < 0) | |
free(pNewElemt); | |
}else | |
ret = -1; // error 1 | |
return ret; | |
} | |
int list_del_head(List *plist,void **pData) // for queue, dequeue | |
{ | |
int ret=0; | |
int *pTest; | |
ListElmt *tmp; | |
if( plist->size > 0 && plist->head != NULL ){ | |
if(plist->head->next == NULL || plist->size == 1){ // last one, | |
*pData = plist->head->data; | |
free(plist->head); | |
// reset | |
plist->head = NULL; | |
plist->tail = NULL; | |
plist->size = 0; | |
pTest = (int *)*pData; | |
}else { // more than 1 | |
*pData = plist->head->data; // head's data | |
tmp = plist->head; | |
plist->head = plist->head->next; // pop | |
free(tmp); | |
pTest = (int *)*pData; | |
plist->size--; | |
} | |
}else | |
ret=-1; | |
return ret; | |
} | |
int list_del_tail(List *plist,void **pData) // for stack, pop | |
{ | |
int ret=0; | |
int *pTest,i; | |
ListElmt *tmp; | |
if( plist->size > 0 && plist->tail != NULL ){ | |
if(plist->size == 1) { | |
tmp = plist->tail; | |
*pData = tmp->data; | |
free(tmp); | |
//reset; | |
plist->head = NULL; | |
plist->tail = NULL; | |
plist->size = 0; | |
}else { //more than 2 elmts | |
tmp = plist->head; | |
for(i=0;i<(plist->size-2);i++) | |
{ | |
if(tmp->next != NULL) | |
tmp = tmp->next; // (tail-1) | |
} | |
*pData = plist->tail->data; | |
free(plist->tail); | |
plist->tail = tmp; | |
plist->size--; | |
} | |
}else | |
ret=-1; | |
return ret; | |
} | |
List list; | |
int main() | |
{ | |
int data[]={1,2,3,4,5,6,7,8,9,10}; | |
int pri=0; | |
int i=0,j; | |
int chk=0; | |
ListElmt *pElmt,*pNext; | |
int *pTestData; | |
int *pTestData2; | |
list_init(&list); | |
for(j=0;j<5;j++) | |
{ | |
for(i=0;i<4+j;i++) | |
{ | |
chk = list_ins_last(&list,(void *)&data[i]); | |
if(chk < 0) | |
printf("error ins!!!!!!!!! chk=%d",chk); | |
printf("enqueue %d/%d data %d \n",i,list.size,data[i]); | |
} | |
printf("\n\n"); | |
for(i=0;i<3+j;i++) | |
{ | |
// chk = list_del_head(&list,(void **)&pTestData2 ); | |
chk = list_del_tail(&list,(void **)&pTestData2 ); | |
if(chk < 0) | |
printf("error del !!!!!!!!! chk=%d",chk); | |
printf("dequeue %d/%d data %d \n",i,list.size, *pTestData2); | |
} | |
printf("\n\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment