#include <stdio.h>
#include <stdlib.h>
#define ElemType int
struct ListNode
{
ElemType e;
struct ListNode *next;
};
typedef struct
{
struct ListNode *Head;
struct ListNode *Tail;
}List,*pList;
int insertTail(ElemType X, pList L)
{
/*建指针,分内存,赋值*/
struct ListNode *newP = (struct ListNode *)malloc(sizeof(struct ListNode)) ;
if (newP == NULL )
{
perror("out of space!");
return -1;
}
/*元素赋值*/
newP->e = X;
/*插入到链表尾部,定位新的尾指针*/
newP->next = L->Tail->next;
L->Tail->next = newP;
/*鸠占鹊巢*/
L->Tail = newP;
return 0;
}
int main()
{
pList L = (pList)malloc(sizeof(List)) ;
/*为链表分配内存*/
if (L == NULL)
{
perror("out of space!");
return -1;
}
/*分配尾指针*/
L->Tail = (struct ListNode *)malloc(sizeof(struct ListNode));
if (L->Tail == NULL)
{
perror("out of space!");
return -1;
}
/*初始化链表*/
L->Tail->next = L->Tail;
L->Head = L->Tail->next;
/*接个参*/
int i;
printf("来插我呀:>");
for (; scanf("%d",&i) != EOF;)
{
printf("来插我呀:>");
insertTail(i,L);
}
/*遍历节点*/
while (L->Head->next != L->Tail->next)
{
printf("%d ", L->Head->next->e);
L->Head = L->Head->next;
}
printf("\n%d ",L->Tail->e);
puts("\nojbk!");
return 0;
}
尾插法循环链表
最新推荐文章于 2024-06-25 07:11:52 发布