链表(求此程序编码)邮箱xiayula10@163.com
发布网友
发布时间:2022-11-11 15:03
我来回答
共1个回答
热心网友
时间:2024-11-25 06:45
/*
http://zhidao.baidu.com/question/359535292.html?fr=uc_push&push=ql&oldq=1
1.在一个有序表中插入一个元素,使得该表仍然有序。
2.将一个链表中的元素进行拆分,将所有奇数放到一个链表中,将所有的偶数放到另一个链表中。
3.将两个链表合并成一个链表。
4.将一个链表中的所有元素逆序存储并显示。
*/
#define AUTHOR ZEERO_FN
typedef struct _node{
int num;
struct _node *next;
} Node, *pNode;
pNode CreateTestNode(int re, int len)
{
pNode head, cur, tail;
int i;
head = cur = tail = NULL;
for(i = 0; i < len; i++)
{
if(NULL == (cur = (pNode)malloc(sizeof(*cur))))
return head;
cur->num = i + re;
cur->next = NULL;
if(!head)
{
head = tail = cur;
}
else
{
tail->next = cur;
tail = tail->next;
}
}
return head;
}
int PrintAllNode(const pNode head) //显示链表元素数据
{
Node const * ph = head;
while(ph)
{
printf("%d ",ph->num);
ph = ph->next;
}
printf("\n");
}
pNode Insert(pNode ph, int n) //1.在一个有序表中插入一个元素,使得该表仍然有序。
{
pNode head = ph, tmp,cur;
if(NULL == (tmp = (pNode)malloc(sizeof(*cur))))
return ph;
tmp->num = n;
while(head && head->num < n)
{
cur = head;
head = head->next;
}
if(ph == head)
{
tmp->next = head;
return tmp;
}
tmp->next = cur->next;
cur->next = tmp;
return ph;
}
pNode Split(pNode h, pNode *hA, pNode *hB)//2.将一个链表中的元素进行拆分,将所有奇数放到一个链表中,将所有的偶数放到另一个链表中。
{
pNode headA, tailA, headB, tailB, tmphead;;
if(h == NULL) return NULL;
*hA = *hB = NULL;
tmphead = h;
headA = tailA = headB = tailB = NULL;
while(tmphead)
{
if(tmphead->num % 2)
{
if(!headA)
{
*hA = headA = &(*tmphead);
}
else
{
headA->next= &(*tmphead);
headA = headA->next;
}
}
else
{
if(!headB)
{
*hB = headB = &(*tmphead);
}
else
{
headB->next= &(*tmphead);
headB = headB->next;
}
}
tmphead = tmphead->next;
}
headA->next = headB->next = NULL;
return NULL;
}
pNode join(pNode headA, pNode headB) //3将两个链表合并成一个链表。
{
pNode head, tail;
if(!headA) return headB;
if(!headB) return headA;
head = tail = headA;
headA = headA->next;
while(headA)
{
tail->next = headA;
tail = tail->next;
headA = headA->next;
}
tail->next = headB;
tail = tail->next;
headB = headB->next;
while(headB)
{
tail->next = headB;
tail = tail->next;
headB = headB->next;
}
tail->next = NULL;
return head;
}
pNode reverse(pNode h) //4将一个链表中的所有元素逆序存储
{
pNode tmphead,tmptail, head = h;
tmphead = tmptail = NULL;
while(head->next)
{
if(!tmphead)
{
tmphead = tmptail = head;
head = head->next;
tmptail->next = NULL;
}
else
{
tmphead = head;
head = head->next;
tmphead->next = tmptail;
tmptail = tmphead;
}
}
head->next = tmphead;
return head;
}
int main(int argc, char *argv[])
{
pNode head, headA, headB;
head = CreateTestNode(5, 20);
printf("测试链表 \n");
PrintAllNode(head);
printf("头插 3 \n");
head = Insert(head, 3); //头插
PrintAllNode(head);
printf("尾插 30 \n");
head = Insert(head, 30); //尾插
PrintAllNode(head);
printf("中间插 15 \n");
head = Insert(head, 15); //中间插
PrintAllNode(head);
printf("拆分 \n");
head = Split(head, &headA, &headB); //拆分
PrintAllNode(headA);
PrintAllNode(headB);
printf("合并 \n");
head = join(headA, headB); //合并
PrintAllNode(head);
printf("翻转 \n");
head = reverse(head); //翻转
PrintAllNode(head);
system("PAUSE");
return 0;
}