您的位置:首页 > 编程语言 > C语言/C++

C语言链表各种操作

2016-05-19 20:04 393 查看
当时把在同一份代码里实现了各种操作,可能模块之间有些混乱,见谅

没有注释,逻辑上不是很复杂,应能读懂

#include <stdio.h>

#include <stdlib.h>

void out(struct link * head);

struct link * creat1();

struct link * creat2();

void free_(struct link * head);

struct link * del(struct link * head);

struct link * combine(struct link * head1,struct link * head2);

struct link

{
int data;
struct link * next;

};

void main()

{

    struct link * head1,*head2,*head3;
head1 = creat1();
head2 = creat2();
out(head1);
out(head2);

    head1 = del(head1);
out(head1);
head3 = combine(head1,head2);
out(head3);
free_(head3);

}

 

struct link * creat1()

{

    struct link * head;

    struct link * p;
struct link * q;

  int a[10] = {1, 3, 5, 7, 9, 10 };

    int i = 0;
head = (struct link *)malloc(sizeof(struct link));
p = q = (struct link *)malloc(sizeof(struct link));

    p->data = a[i];
head->next=p;
while (p->data != 10)
{
i++;
p = (struct link *)malloc(sizeof(struct link));
p->data = a[i];
q->next = p;
q = p;
}
p->next = NULL;
return head;

}

struct link * creat2()

{

    struct link * head;

    struct link * p;
struct link * q;

  int a[10] = {2, 4, 6, 8, 10 };

    int i = 0;
head = (struct link *)malloc(sizeof(struct link));
p = q = (struct link *)malloc(sizeof(struct link));

    p->data = a[i];
head->next=p;
while (p->data != 10)
{
i++;
p = (struct link *)malloc(sizeof(struct link));
p->data = a[i];
q->next = p;
q = p;
}
p->next = NULL;
return head;

}

/*

node * create()              头插法创建链表

{
node * head = (node*)malloc(sizeof(node));
//头结点
node *p, *q;
int a[11] = {0,1, 2, 4, 6, 7, 9, 11, 13, 15, 17};
//用于建立链表用数据,免得每次都键盘输入,0表示输入结束
int i = 10;

head->next = NULL;
q=head->next;
while (a[i] != 0)
{
p = (node *)malloc(sizeof(node));
p->data = a[i];
p->next = q;
head->next = p;
//q = q->next;
q = p;
i--;
}
return head;

}

*/

void out(struct link * head)

{
while(head->data != 10)
{
head = head->next;
printf("%d ",head->data);
}
putchar(10);

}

struct link * del(struct link * head)

{

    struct link * p;
struct link * q;
int i;
int c=1;
printf("input the pont:\n");
scanf("%d",&i);
q = head;

    p = head->next;
while(i != c && p->next != NULL)
{

q = p;
p = p->next;
c++;
}
if(c == i && p)
{

        q->next = p->next;
free(p);
}

    return head;

}

void free_(struct link * head)

{

    struct link * p;
p = head->next;

    free(head);
while(head != NULL)
{
head = p->next;

        free(p);
p = head;
}

    free(p);

}

struct link * combine(struct link * head1,struct link * head2)

{

    struct link * l1;
struct link * l2;
struct link * l3;
l3 = head1;
l1 = head1->next;
l2 = head2->next;
while(l1 && l2)
{
if(l1->data <= l2->data)
{

            l3->next = l1;
l3 = l1;
l1 = l1->next;
}
else
{
l3->next = l2;
l3 = l2;
l2 = l2->next;
}
}
if(l1 == NULL)
l3->next = l2;
else if(l2 == NULL)

        l3->next =l1;
free(head2);
return head1;

}

/*

void reverse(node *head)   反转链表

{
node *p, *q;
p = head->next;
head->next = NULL;
while ( p )
//填空
{
q = p;
p = p->next;
q->next = head->next;
head->next =  q ;
//填空
}

}

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