您的位置:首页 > 其它

单向链表的基本操作

2013-09-02 00:24 387 查看
#include <stdio.h>

#include <string.h>

#include <stdlib.h>

typedef struct Node

{

int data;

struct Node *next;

}Node;

/*创建头节点为phead的链表,当输入的数小于0时退出*/

Node *ListCreate(Node *phead)

{

Node *p1 = NULL;

Node *p2 = NULL;

p1 = p2 = (Node *)malloc(sizeof(Node));

if(p1 == NULL || p2 == NULL)

{

printf("malloc failed!\n");

exit(0);

}

memset(p1,0,sizeof(Node));

memset(p2,0,sizeof(Node));

printf("please input num:");

int s;

scanf("%d",&s);

p1->next = NULL;

p1->data = s;

while(s > 0) //输入小于0时退出

{

if(phead == NULL)

{

phead = p1;

}

else

{

p2->next = p1;

}

p2 = p1;

p1 = (Node *)malloc(sizeof(Node));

if(p1 == NULL)

{

printf("malloc failed!\n");

exit(0);

}

printf("please input num:");

scanf("%d",&s);

p1->next = NULL;

p1->data = s;

}

return phead;

}

/*

插入节点,在你想插入的第n个节点后插入,要插入的数值由用户输入

*/

Node *ListInsert(Node *phead,int n)

{

Node *pInsert = (Node *)malloc(sizeof(Node));

if(pInsert == NULL)

{

printf("malloc failed!\n");

exit(0);

}

pInsert->next = NULL;

printf("please input you want insert num:");

scanf("%d",&pInsert->data);

int i = 0;

Node *ptr = phead;

while(ptr != NULL)

{

i++;

if(i == n)

break;

ptr = ptr->next;

}

if(i < (n-1))

{

printf("the Node you want to input is out off the list!\n");

exit(0);

}

Node *p1 = ptr->next;

ptr->next = pInsert;

pInsert->next = p1;

return phead;

}

/*

删除第n号节点

*/

Node *ListDel(Node *phead, int n)

{

Node *ptr = phead;

int i = 0;

while(ptr != NULL)

{

i++;

if(i == (n-1))

break;

ptr = ptr->next;

}

if(i < (n-1))

{

printf("the Node you want to del is out of the list!\n");

exit(0);

}

Node *p1 = ptr->next;

Node *p2 = p1->next;

ptr->next = p2;

free(p1);

p1 = NULL;

return phead;

}

/*

查找链表,查找第n个节点元素,并把它打印出来

*/

void ListFind(Node *phead, int n)

{

Node *ptr = phead;

int i = 0;

while(ptr != NULL)

{

i++;

if(i == n)

break;

ptr = ptr->next;

}

if(i < n)

{

//如果要查找的节点超出链表范围,打印提示并退出

printf("the Node you want to find is out of the list!\n");

exit(0);

}

printf("the Node you find is:%d\n",ptr->data);

}

/*

*合并两有序链表,使之依然有序

1/ 用一般方法

*/

Node *ListMerge(Node *head1,Node *head2)

{

if(head1 == NULL)

return head2;

if(head2 == NULL)

return head1;

Node *head = NULL;

Node *p1 = NULL;

Node *p2 = NULL;

if(head1->data <= head2->data)

{

head = head1; //记录表头

p1 = head1->next;

p2 = head2;

}

else

{

head = head2;

p1 = head1;

p2 = head2->next;

}

Node *pcurrent = head;

while(p1 != NULL && p2 != NULL)

{

if(p1->data <= p2->data)

{

pcurrent->next = p1;//当前指针指向p1

pcurrent = p1; //更新pcurrent

p1= p1->next; //p1指向下一个节点

}

else

{

pcurrent->next = p2;

pcurrent = p2;

p2 = p2->next;

}

}

if(p1 != NULL)

pcurrent->next = p1;

if(p2 != NULL)

pcurrent->next = p2;

return head;

}

/*

*合并两有序链表,使之依然有序

1/ 用递归的方法

*/

Node *ListMergeD(Node *head1,Node *head2)

{

if(head1 == NULL)

return head2;

if(head2 == NULL)

return head1;

Node *head = NULL;

if(head1->data <= head2->data)

{

head = head1;

head->next = ListMergeD(head1->next,head2);

}

else

{

head = head2;

head->next = ListMergeD(head1,head2->next);

}

return head;

}

/*

链表的遍历

*/

void ListTravel(Node *phead)

{

if(phead == NULL)

printf("the list is empty!\n");

Node *p = NULL;

p = phead;

while(p != NULL)

{

printf("%d ",p->data);

p = p->next;

}

printf("\n");

}

int main()

{

Node *head1 = NULL;

Node *head2 = NULL;

Node *head = NULL;

head1 = ListCreate(head1);

head2 = ListCreate(head2);

//head1 = ListInsert(head1,2);

//head1 = ListDel(head1,2);

//ListFind(head,2);

head = ListMergeD(head1,head2);

ListTravel(head);

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: