链表实现
内存零碎数据的有效组织。比如,当我们用 malloc 来进行内存申请的时候,当内存足够,而由于碎片太多,没有连续内存时,只能以申请失败而告终,而用链表这种数据结 构来组织数据,就可以解决上类问题。
链表原理的实现(静态链表)
#include <stdio.h>
typedef struct node
{
int data;
struct node * next;
}Node;
int main(void)
{
Node a;
Node b;
Node c;
a.data = 10;
b.data = 20;
c.data = 30;
a.next = &b;
b.next = &c;
c.next = NULL;
Node * head = &a;
while (head != NULL)
{
printf("data = %d\n",head->data);
head = head->next;
}
return 0;
}
运行结果
data = 10
data = 20
data = 30
动态链表
静态链表的意义不是很大,主要原因,数据存储在栈上,栈的存储空间有限,不能动态分配。所空链表要实现存储的自由,要动态的申请堆里的空间。
定义结构体表示节点
typedef struct node
{
int data;
struct node * next;
}Node;
头插法创建链表
// 头插法创建链表
Node *createList_h()
{
Node * head = (Node *)malloc(sizeof(Node));
head->next = NULL;
Node * cur;
int temp;
while (1) {
scanf("%d",&temp);
if(temp == 0)
break;
cur = (Node *)malloc(sizeof(Node));
cur->data = temp;
cur->next = head->next;
head->next = cur;
}
return head;
}
尾插法创建链表
//尾插法创建链表
Node * createList_l()
{
Node * head = (Node*)malloc(sizeof(Node));
head->next = NULL;
Node *pre = head;
Node *last;
int data;
scanf("%d",&data);
while (data) {
last = (Node*)malloc(sizeof(Node));
last->data = data;
last->next = NULL;
pre->next = last;
pre = last;
scanf("%d",&data);
}
return head;
}
链表遍历
//链表遍历
void traverseList(Node *head)
{
head = head->next;
while (head !=NULL) {
printf("%d\n",head->data);
head = head->next;
}
}
求链表长度
// 求链表长度
int lenList(Node * head)
{
int len = 0;
head = head->next;
while (head) {
len++;
head = head->next;
}
return len;
}
插入
//插入
void insertList(Node * head,int inserData)
{
Node * cur = (Node *)malloc(sizeof(Node));
cur->data = inserData;
cur->next = head->next;
head->next = cur;
}
查找
//查找
Node *searchList(Node *head,int find)
{
head = head->next;
while (head) {
if(head->data == find)
break;
head = head->next;
}
return head;
}
删除
//删除
void deleteList(Node *head , Node *del)
{
while (head->next != del)
head = head->next;
head->next = del->next;
free(del);
}
排序 换值
//排序 换值
void sortList(Node * head, int len)
{
for (int i=0; i<len-1; i++)
{
Node * cur = head->next;
for (int j=0; j<len-1; j++)
{
if(cur->data>cur->next->data)
{
int tmp = cur->data;
cur->data = cur->next->data;
cur->next->data = tmp;
}
cur = cur->next;
}
}
}
排序 换指针
//排序 换指针
void sortHeadByPtr(Node *head,int len)
{
Node * subHead,*p,*q,*tmp;
int i,j;
for (i=0; i<len-1; i++)
{
subHead = head;
p = head->next;
q = p->next;
for (j = 0; j<len-1; j++)
{
if(p->data>q->data)
{
subHead->next = p->next;
p->next = q->next;
q->next = p;
tmp = p;
p = q;
q = tmp;
}
subHead = subHead->next;
p = p->next;
q = q->next;
}
}
}
链表反转
//链表反转
void reverseList(Node * head)
{
Node* p = head ->next,*q;
head->next = NULL;
while (p != NULL) {
q = p->next;
p->next = head->next;
head->next = p;
p = q;
}
}
测试
int main(void)
{
// Node *head = createList_l();
Node *head = createList_h();
traverseList(head)
printf("the length of List is %d\n",lenList(head));
insertList(head, 666);
traverseList(head);
Node *pfind = searchList(head, 666);
if(pfind == NULL)
printf("find none\n");
else
{
printf("your find in list %p\n",pfind);
deleteList(head, pfind);
}
traverseList(head);
// sortList(head, lenList(head));
sortHeadByPtr(head, lenList(head));
printf("the List after sort is:\n");
traverseList(head);
reverseList(head);
traverseList(head);
destroyList(head);
printf("after destory!!\n");
traverseList(head);
return 0;
}