all by chenge
1.初始化链表
2.创建链表,返回头指针
3.打印链表
4.获取长度
5.清空链表(这里有问题,清空之后表头地址变了,待改善)
6.获取第n个结点的data
7.向单链表中第pos个结点位置插入元素为x的结点,若插入成功返回1,否则返回0
8.排序单链表,降序,(算法慢,待优化)
其他操作基本上以此类推可得
------------------------------------------------------------------------------------------------------------------*/#include <stdio.h>
#include<stdlib.h>
typedef int type;
#define LEN sizeof(struct node)
typedef struct node
{
type data;
struct node *next;
}node;
/*------------------------------------------------------------------------------------------------------------
-------------------------------------初始化链表---------------------------------------------------------*/
void initList(node ** phead){
*phead = NULL;
printf("initList函数执行,初始化成功\n");
}
/*------------------------------------------------------------------------------------------------------------------
-------------------------------------创建链表,返回头指针-------------------------------------------------*/
node * creatList(node * head){
node *p, *pl;
head=p=(node * )malloc(LEN);
printf("creatList函数执行,请输入链表数据成员,以0结束\n");
scanf("%d",&p->data);/*头结点的数据成员*/
while(p->data!=0) /*给出0结束条件,退出循环*/
{
pl=p;
p=(node * )malloc(LEN);
scanf("%d",&p->data);/*中间结点数据成员*/
if(p->data!=0){
pl->next=p;/*中间结点的指针成员值*/
}else{
break;
}
}
pl-> next=NULL;/*尾结点的指针成员值*/
return head;
}
/*--------------------------------------------------------------------------------------------------------
-----------------------------------------------打印链表------------------------------------------------*/
void printList(node *head){
if(NULL == head) //链表为空
{
printf("printList函数执行,链表为空\n");
}
else
{
while(NULL != head)
{
printf("%d ",head->data);
head = head->next;
}
printf("\n");
}
}
/*------------------------------------------------------------------------------------------------------------
-------------------------------------------------获取长度-------------------------------------------------*/
int getLength(node * head){
int length = 0;
if(NULL == head) //链表为空
{
printf("链表为空\n");
}
else
{
while(NULL != head)
{
length ++ ;
head = head->next;
}
}
printf("length is : %d\n",length);
return length;
}
/*------------------------------------------------------------------------------------------------------------
-------------------------------------------------清空链表-------------------------------------------------*/
|