双向链表
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int id;
struct Node* pNext;
struct Node* pLast;
}List;
void AddNode(List** ppHead,List** ppEnd,int id)
{
List* pTemp = (List*)malloc(sizeof(List));
pTemp->id = id;
pTemp->pLast = NULL;
pTemp->pNext = NULL;
if (*ppHead == NULL)
{
*ppHead = pTemp;
*ppEnd = pTemp;
}
else
{
(*ppEnd)->pNext = pTemp;
pTemp->pLast = *ppEnd;
*ppEnd = pTemp;
}
}
int main()
{
List* pHead = NULL;
List* pEnd = NULL;
AddNode(&pHead, &pEnd, 1);
AddNode(&pHead, &pEnd, 2);
AddNode(&pHead, &pEnd, 3);
AddNode(&pHead, &pEnd, 4);
return 0;
}
队列
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int value;
struct Node* pNext;
}Queue;
void Q_Push(Queue** ppHead,Queue** ppTail,int n)
{
Queue* pTemp = (Queue*)malloc(sizeof(Queue));
pTemp->value = n;
pTemp->pNext = NULL;
if (*ppHead == NULL)
{
*ppHead = pTemp;
}
else
{
(*ppTail)->pNext = pTemp;
}
*ppTail = pTemp;
}
Queue* Q_Pop(Queue**ppHead,Queue**ppTail)
{
if (NULL == ppHead || NULL == *ppHead)
{
return NULL;
}
Queue* pMark = NULL;
pMark = *ppHead;
*ppHead = (*ppHead)->pNext;
if (pMark->pNext == NULL)
{
*ppTail = NULL;
}
return pMark;
}
int main()
{
Queue* pHead = NULL;
Queue* pTail = NULL;
Q_Push(&pHead, &pTail, 1);
Q_Push(&pHead, &pTail, 2);
Q_Push(&pHead, &pTail, 3);
Q_Push(&pHead, &pTail, 4);
printf("%d\n", Q_Pop(&pHead, &pTail)->value);
printf("%d\n", Q_Pop(&pHead, &pTail)->value);
printf("%d\n", Q_Pop(&pHead, &pTail)->value);
printf("%d\n", Q_Pop(&pHead, &pTail)->value);
return 0;
}
栈
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int value;
struct Node* pNext;
}Stack;
void S_Push(Stack** ppTop, int n)
{
Stack* pTemp = (Stack*)malloc(sizeof(Stack));
pTemp->value = n;
pTemp->pNext = NULL;
pTemp->pNext = *ppTop;
*ppTop = pTemp;
}
int S_Pop(Stack** ppTop)
{
Stack* pMark = NULL;
int value;
if (ppTop == NULL || *ppTop == NULL)
{
return -1;
}
pMark = *ppTop;
*ppTop = (*ppTop)->pNext;
value = pMark->value;
free(pMark);
pMark = NULL;
return value;
}
int main()
{
Stack* pTop = NULL;
S_Push(&pTop, 1);
S_Push(&pTop, 2);
S_Push(&pTop, 3);
S_Push(&pTop, 4);
printf("%d\n",S_Pop(&pTop));
printf("%d\n", S_Pop(&pTop));
printf("%d\n", S_Pop(&pTop));
printf("%d\n", S_Pop(&pTop));
return 0;
}