/*#include "malloc.h"
#include "stdio.h"
typedef int DataType;
typedef struct Node
{
DataType data;
struct Node *next;
}LNode,*PNode,*LinkList;
void InitList(PNode &h); //这里一定要加引用为什么?
int ListInsert(PNode &h,int pos,DataType x); //以下四行加不加引用都可以,为什么?这是因为这里的h本身就是指针--实参传递后和形参指向同一个地址
int ListDelete(PNode &h,int pos,DataType *item); //最好都给他们加上& 防止歧义。
void DestroyList(PNode &h);
void TraverseList(PNode h); //这里没有+是因为没有对h链表里面的东西进行修改!
int main()
{
int i;
int data[7]={0,1,1,2,3,5,8}; //insert之前先定义一个数组,将数组的东西给insert进去。
DataType item;
PNode h=NULL; // 先定义一个指针链表。 h是指针变量。为了防止指针乱指,就先让它指向NULL。
InitList(h); //先定义一个初结点。
for(i=0;i<7;i++) //一个一个的insert.
{
if(!ListInsert(h,i+1,data[i])) //这个是对ListInsert函数的判断! 这里是为了返回一个 1来判断程序。
{
printf("插入失败!/n");
return 0;
}
}
printf("/n/n删除前单链表中的元素/n");
TraverseList(h);
if(!ListDelete(h,7,&item))
{
printf("error!/n");
return 0;
}
printf("/n/n删除后单链表的数据元素/n");
TraverseList(h);
DestroyList(h);
return 0;
}
//初始化
void InitList(PNode &h) //&h是引用(别名),但是用&h通过函数能改变主函数中h的值使其得到空间分配;如果去点&,直接将h传递过来
{ //那么h在此函数中确实初始化成功了,但是不能返回到主函数中,即主函数中的h依然没有分配空间----即此函数调用没用
h=(LNode *)malloc(sizeof(LNode)); //初始化链表首先先建立一个表头, 先建立一个h h-next=NULL;
if(!h)
{
printf("初始化失败!/n");
}
h->next=NULL;
}
//插入数据
int ListInsert(PNode &h,int pos,DataType x)
{
PNode p=h,q;
int i=0;
//先找位置
while (p&&i<pos-1)
{
p=p->next;
i++;
}
if(!p||i>pos-1)
{
printf("不能插在此处!/n");
return 0;
}
q=(PNode) malloc (sizeof(LNode));
if(!q)
{
printf("error!/n");return 0;
}
//insert
q->data=x;
q->next=p->next;
p->next=q;
return 1;
}
//删除数据
int ListDelete(PNode &h,int pos,DataType *item)
{
PNode p=h,q;
int i=0;
//先定位
while (p->next&&i<pos-1)
{
p=p->next;
i++;
}
if(!p->next||i>pos-1)
{
printf("Error!/n");
return 0;
}
//再删除
q=p->next;
p->next=q->next;
*item=q->data;
free(q);
return 1;
}
//销毁链表
void DestroyList(PNode &h)
{
PNode p=h->next;
while (h)
{
p=h;
h=h->next;
free(p);
}
}
//遍历链表
void TraverseList(PNode h)
{
PNode p=h->next;
while (p) //循环P是否存在!
{
printf("%d/t",p->data);
p=p->next;
}
printf("/n");
}*/
链表就是 能让指针指向里面一个结点 然后通过操作,可以进行结点分离,结点内插等等
好比是一堆排序整齐得吸铁石,你可以随意去除其中一块 或者加入一块