Last active
November 16, 2016 01:51
-
-
Save JeonghunLee/8ffab1180bee7fd4adfc5df1dc8d2c2f 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 sListElemt_{ | |
int data; | |
struct sListElemt_ *next ; | |
}sListElemt; | |
typedef struct sList_{ | |
int size; | |
sListElemt *pHead; | |
sListElemt *pTail; | |
}sList; | |
sList list; | |
int initList(sList *pList) | |
{ | |
pList->size = 0; | |
pList->pHead = NULL; | |
pList->pTail = NULL; | |
} | |
int addLisTToTail(sList *pSList,int data) | |
{ | |
sListElemt * pNewsListElemt; | |
if((pNewsListElemt = (sListElemt *) malloc(sizeof(sListElemt))) == NULL) | |
return -1; | |
pNewsListElemt->data = data; | |
pNewsListElemt->next = NULL; | |
if(pSList->size ==0){ | |
pSList->pHead= pNewsListElemt; | |
pSList->pTail= pNewsListElemt; | |
}else { | |
pSList->pTail->next = pNewsListElemt; | |
pSList->pTail = pNewsListElemt; | |
} | |
pSList->size++; | |
return 0; | |
} | |
int addListToHead(sList *pSList, int data) | |
{ | |
sListElemt * pNewsListElemt; | |
if((pNewsListElemt = (sListElemt *) malloc(sizeof(sListElemt))) == NULL) | |
return -1; | |
pNewsListElemt->data = data; | |
pNewsListElemt->next = NULL; | |
if(pSList->size ==0){ | |
pSList->pHead= pNewsListElemt; | |
pSList->pTail= pNewsListElemt; | |
}else { | |
pNewsListElemt->next = pSList->pHead; | |
pSList->pHead = pNewsListElemt; | |
} | |
pSList->size++; | |
return 0; | |
} | |
int getListToTail(sList *pSList, int *data) | |
{ | |
sListElemt * pListElemt; | |
if(pSList->size < 1) | |
return -1; | |
if(pSList->size == 1){ | |
*data = pSList->pTail->data; | |
free(pSList->pTail); | |
pSList->pHead = NULL; | |
pSList->pTail = NULL; | |
}else{ | |
pListElemt = pSList->pHead; | |
while(pListElemt->next != pSList->pTail) | |
pListElemt = pListElemt->next; | |
*data = pSList->pTail->data; | |
free(pSList->pTail); | |
pSList->pTail = pListElemt; | |
} | |
pSList->size--; | |
return 0; | |
} | |
int getListToHead(sList *pSList, int *data) | |
{ | |
sListElemt * pListElemt; | |
if(pSList->size < 1) | |
return -1; | |
if(pSList->size == 1){ | |
*data = pSList->pHead->data; | |
free(pSList->pTail); | |
pSList->pHead = NULL; | |
pSList->pTail = NULL; | |
}else{ | |
*data = pSList->pHead->data; | |
pListElemt = pSList->pHead->next; | |
free(pSList->pHead); | |
pSList->pHead = pListElemt; | |
} | |
pSList->size--; | |
return 0; | |
} | |
int displayListfromHead(sList *pSList) | |
{ | |
sListElemt * pListElemt; | |
if(pSList->size < 1) | |
return -1; | |
pListElemt = pSList->pHead; | |
printf("display len %d \n",pSList->size ); | |
do{ | |
printf("%d->",pListElemt->data); | |
pListElemt = pListElemt->next; | |
}while(pListElemt != NULL); | |
printf("\n"); | |
return 0; | |
} | |
int main(void) { | |
int i,data; | |
initList(&list); | |
for(i=0;i<10;i++) | |
addLisTToTail(&list,i+1); | |
displayListfromHead(&list); | |
for(i=0;i<10;i++) | |
{ | |
// getListToHead(&list,&data); | |
getListToTail(&list,&data); | |
printf("%d->",data); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment