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 ;
//填空
}
}
*/
没有注释,逻辑上不是很复杂,应能读懂
#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 ;
//填空
}
}
*/
相关文章推荐
- 【转载】C++基本功和 Design Pattern系列 ctor & dtor
- C语言约瑟夫环链表
- java jni jbyteArray传参给c++
- 抽象工厂 C++实现
- c++学习书籍汇总
- C++ 中 ZeroMemory、memset 危险需慎用
- QFile和C语言对文件操作的性能比较.--读取double型二进制数据文件
- C语言判断分支循环函数杂记
- 常量指针与指向常量的指针
- C语言数据类型杂记
- c++新人,结构化程序设计与面向对象程序设计的比较(1)
- LeetCode 47
- C++冒号表达式
- SM2算法第十五篇:ECDSA数字签名算法的C语言实现
- typedef C/C++
- L1-002. 打印沙漏(C++)
- C/C++的字符串操作(二)
- C语言随记(一)—函数
- c/c++原子操作 接口函数 自查文档
- C/C++的字符串操作(一)