main函数:
#include <stdio.h>
#include "./03linklist.h"
int main(int argc, const char *argv[])
{
linkList* head = create_linkList();
insert(head,10);
insert(head,20);
insert(head,30);
insert(head,40);
insert(head,50);
showlinklist(head);
printf("---------------\n");
insertTail(head,999);
insertTail(head,888);
insertTail(head,777);
showlinklist(head);
printf("---------------\n");
deleteHead(head);
showlinklist(head);
printf("---------------\n");
deleteTail(head);
showlinklist(head);
printf("---------------\n");
insertByPos(head,2,10000000);
showlinklist(head);
printf("---------------\n");
deleteByPos(head,2);
showlinklist(head);
compareInsertSort(head,8000);
compareInsertSort(head,100);
compareInsertSort(head,3000);
compareInsertSort(head,48000);
compareInsertSort(head,50000);
compareInsertSort(head,600);
showlinklist(head);
return 0;
}
函数文件:
#include <stdio.h>
#include <stdlib.h>
#include "./03linklist.h"
/*
* function: 创建一个带头结点的空的单链表
* @param [ in]
* @param [out]
* @return
*/
linkList* create_linkList()
{
linkList* head = (linkList*)malloc(sizeof(linkList));
head->text.len = 0;
head->next = NULL;
return head;
}
/*
* function: 插入节点
* @param [ in]
* @param [out]
* @return
*/
int insert(linkList* head,int num)
{
linkList* temp = (linkList*)malloc(sizeof(linkList));
temp->text.data = num;
temp->next = NULL;
//头插法
temp->next = head->next;
head->next = temp;
//更新链表长度
head->text.len++;
return 0;
}
/*
* function: 尾插法插入数据
* @param [ in]
* @param [out]
* @return
*/
int insertTail(linkList* head,int num)
{
linkList* temp = (linkList*)malloc(sizeof(linkList));
temp->next = NULL;
temp->text.data = num;
linkList* p = head;
while(p->next != NULL)
{
p = p->next;
}
p->next = temp;
temp->next = NULL;
head->text.len++;
return 0;
}
/*
* function: 链表遍历
* @param [ in]
* @param [out]
* @return
*/
void showlinklist(linkList* head)
{
linkList* p = head;
while(p->next != NULL)
{
p = p->next;
printf("%d\n",p->text.data);
}
return;
}
/*
* function: 头删法
* @param [ in]
* @param [out]
* @return
*/
void deleteHead(linkList* head)
{
linkList* temp = head->next;
head->next = temp->next;
free(temp);
temp = NULL;
head->text.len--;
}
/*
* function: 尾删法
* @param [ in]
* @param [out]
* @return
*/
void deleteTail(linkList* head)
{
linkList* temp = head;
while(temp->next->next != NULL)
{
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
head->text.len--;
}
/*
* function: 按位置插入
* @param [ in]
* @param [out]
* @return
*/
void insertByPos(linkList* head,int pos,int num)
{
linkList* p = head;
for(int i = 0;i<pos-1;i++)
{
p = p->next;
}
linkList* temp = (linkList*)malloc(sizeof(linkList));
temp->text.data = num;
temp->next = NULL;
temp->next = p->next;
p->next = temp;
head->text.len++;
}
/*
* function: 按位置删除
* @param [ in]
* @param [out]
* @return
*/
void deleteByPos(linkList* head,int pos)
{
linkList* p = head;
for(int i = 0;i<pos-1;i++)
{
p = p->next;
}
linkList* temp = p->next;
p->next = temp->next;
temp->next = NULL;
free(temp);
temp = NULL;
head->text.len--;
}
void compareInsertSort(linkList* head,int num)
{
linkList* temp = (linkList*)malloc(sizeof(linkList));
temp->text.data = num;
temp->next = NULL;
linkList* p = head;
while(p->next != NULL)
{
if(p->next->text.data > temp->text.data)
break;
else
{
p = p->next;
}
}
temp->next = p->next;
p->next = temp;
head->text.len++;
}
头文件:
#ifndef __LINKLIST_H__
#define __LINKLIST_H__
union msg{
int data;
int len;
};
typedef struct node
{
union msg text;
struct node* next;
}linkList;
linkList* create_linkList();
int insert(linkList* head,int num);
void showlinklist(linkList* head);
int insertTail(linkList* head,int num);
void deleteHead(linkList* head);
void deleteTail(linkList* head);
void insertByPos(linkList* head,int pos,int num);
void deleteByPos(linkList* head,int pos);
void compareInsertSort(linkList* head,int num);
#endif