1.反转链表
list reverse(list l)
{
list new_node,old_node,temp;
old_node = l;
new_node = NULL;
while(old_node)
{
temp = old_node->next;//临时变量指向第一个节点的后面
old_node->next = new_node;//第一个节点往后指
new_node = old_node;
old_node = temp;
}
l = new_node;
return l;
}
2.线性表的顺序存储(以数组为中心)
<1>初始化
list makeempty()
{
list l;
l = (list)malloc(sizeof(struct node));
l->next = -1;
return l;
}
<2>查找
int find(list l,int data)
{
int i = 0;
while(i<=l->next&&data[i]!=data)
i++;
if(i>l->next)
return -1//没有找到
else
return i;
}
<3>插入
bool insert(list l,int x, int i)//分别为线性表,需要插入的元素,需要插入的位置
{
if(l->next == maxn - 1)//代表满了
return 0;
if(i<=0||i>l->next + 2)//插入的位置不合法
return 0;
for(int j = l->next; j >=i-1; j --)
{
l->data[j+1] = l->data[j];
}
l->data[i-1] = x;
l->next ++;
return 1;
}
<4>删除
bool Delete(list l,int i)
{
if(i<1||i>l->next + 1)
return 0;
if(l==NULL)
return 0;
for(int j = i; j <= l->next; j ++)
l->data[j-1] = l->data[j];
l->next --;
return 1;
}
3.线性表的链式存储
<1>计算长度
int lenth(list l)
{
int cnt = 0;
while(l)
{
l = l->next;
cnt++;
}
return cnt;
}
<2>查找(按位置查找)
int find(list l,int k)
{
int cnt = 1;
while(l&&cnt<k)
{
l = l->next;
cnt++;
}
if(cnt<k)
return 0;
else
return l->data;
}
查找(按值查找)
int find(list l,int x)
{
int flag = 0;
while(l)
{
if(l->data == x)
{
flag = 1;
break;
}
l = l->next;
}
return flag;
}
<3>插入(无头结点)
list insert(list l,int x,int i)
{
list temp;
temp = (list)malloc(sizeof struct node);
temp->data = x;
if(i==1)
{
temp->next = l;
return temp;
}
int cnt = 1;
list pre;
pre = l;
while(pre&&cnt<i)
{
cnt++;
pre = pre->next;
}
if(pre==NULL||cnt!=i-1)
return 0;
else
{
temp->next = pre->next;
pre->next = temp;
return l;
}
}
插入(有头结点)
bool insert(list l,int x, int i)
{
list temp,pre;
int cnt = 0;
pre = l;
while(pre&&cnt<i-1)
{
pre = pre->next;
cnt++;
}
if(pre==NULL||cnt!=i-1)
return 0;
else
{
temp = (list)malloc(sizeof pre);
temp->data = x;
temp->next = pre->next;
pre->next = temp;
return 1;
}
}
<4>删除(带头节点)
bool Delete(list l,int i)
{
list temp,pre;
int cnt = 0;
pre = l;
while(pre&&cnt<i-1)
{
pre = pre->next;
cnt++;
}
if(pre==NULL||cnt!=i-1||pre->next == NULL)
return 0;
pre->next = pre->next->next;
return 1;
}