8.13单链表

 mymain.c



​​
#include"link.h"

int main(int arc,const char *arv[])
{
  Plink L = get_head();
  int i, a[10] = {10,20,30,40,50,60,70,80,90,95};
#if 0
  for(i=0;i<10;i++)
  {
    head_insert(L,a[i]);
  }
#endif

  for(i=0;i<10;i++)
  {
  tail_insert(L,a[i]);
  }
	node_output(L);

   insert_any_pos(L,4,100);
   node_output(L);

   head_dele(L);
   node_output(L);

   tail_dele(L);
   node_output(L);


   dele_any_pos(L,6);
   node_output(L);

   value_find_change(L,3);
   node_output(L);

   pos_find_change(L,3);
   node_output(L);

   node_reverse(L);
   node_output(L);

   simple_sort(L);
   node_output(L);

  node_destroy(L);
  node_output(L);
}

​

 link.h

#ifndef _LINK_H_
#define _LINK_H_
#include<myhead.h>

typedef struct node
{
 union
 {
	 int len;
	 int data;
 };
 struct node *next;
}Link,*Plink;
Plink get_head();
int head_insert(Plink L,int e);
int node_output(Plink L);
int tail_insert(Plink L,int e);
int insert_any_pos(Plink L,int pos,int e);
int head_dele(Plink L);
int tail_dele(Plink L);
int dele_any_pos(Plink L,int pos);

int value_find_change(Plink L,int key);
int pos_find_change(Plink L,int pos);
int node_reverse(Plink L);
int node_destroy(Plink L);
#endif

link.c

#include"link.h"
Plink get_head()
{
Plink p = malloc(sizeof(Link));
if(p==NULL)
{
printf("申请头结点失败\n");
return NULL;
}
p->len=0;
p->next=NULL;
return p;
}
int empty(Plink L)
{
if(L==NULL)
{
  return 1;
}
return 0;
}


int head_insert(Plink L,int e)
{
if(L==NULL)
{
printf("单链表不存在\n");
	return -1;
}
Plink p = malloc(sizeof(Link));
p->data = e;

p->next=L->next;
p->next = p;
L->len++;
}
int node_output(Plink L)
{
if(L==NULL||L->len==0)
{
 printf("单链表不存在或者为空\n");
 return -1;
}
int i;
Plink t = L;
for(i = 0;i<L->len;i++)
{
t = t->next;
printf("%d\t",t->data);
}
}

int tail_insert(Plink L,int e)
{
if(empty(L))
{
printf("单链表不存在\n");
	return -1;
}
Plink t = L;
for(int i = 0;i<L->len;i++)
{
 t=t->next;
}

Plink p = malloc(sizeof(Link));
p->data = e;

t->next = p;
p->next = NULL;
L->len++;
return 0;
}


int insert_any_pos(Plink L,int pos,int e)
{
if(empty(L))
{
 printf("插入失败\n");
 return -1;
}
int i;
Plink t = L;
for(i=0;i<pos-1;i++)
{
 t=t->next;
}

Plink p = malloc(sizeof(Link));
p->data = e;

p->next = t->next;
t->next = p;
L->len++;
}

int head_dele(Plink L)
{
  if(empty(L)||L->len==0)
  {
    printf("单列表不存在或者为空\n");
	return -1;
  }
  Plink t = L->next;
  L->next = t->next;
  L->len--;

  free(t);
  t=NULL;
  return 0;

}



int tail_dele(Plink L)
{
  if(empty(L)||L->len==0)
  {
    printf("单列表不存在或者为空\n");
	return -1;
  }
  int i;
  Plink t = L;
  for(i = 0;i<L->len-1;i++)
  {
    t= t->next;
  }


  Plink Q = t->next;
  L->next = NULL;
  L->len--;

  free(Q);
  t=NULL;
}


int dele_any_pos(Plink L,int pos)
{
  if(empty(L)||L->len==0)
  {
    printf("单列表不存在或者为空\n");
	return -1;
  }
   int i;
   Plink t = L;
   for(i=0;i<pos-1;i++)
   {
    t=t->next;
   }
   Plink Q = t->next;
   t->next = t->next->next;
   L->len--;

   free(Q);
   Q=NULL;
   return 0;
}


int node_reverse(Plink L)
{
  if(empty(L)||L->len==0)
  {
    printf("单列表不存在或者为空\n");
	return -1;
  }
   Plink Q = L->next->next;
   Plink t = L->next;
   while(Q!=NULL)
   {
   t->next = Q->next;
   Q->next = L->next;
   L->next = Q;
   Q = t->next;
   }
 }
int simple_sort(Plink L)
{
    Plink i,j,min;
    int t;
    for(i  = L->next;i!=NULL;i = i->next)
    {
        min = i;
        for(j  = i->next;j!=NULL;j = j->next)
        {
            if(j->data<min->data)
            {
                min = j;
            }
        }
        if(i!=min)
        {
            t = i->data;
            i->data = min->data;
            min->data = t;
        }
    }
    return 0;
}
int node_destroy(Plink L)
{
    if(L==NULL)
    {
        printf("销毁失败\n");
        return -1;
    }
    Plink t = L;
    while(t!=NULL)
    {
        t=t->next;
        free(L);
        L=t;

    }
    printf("销毁成功\n");
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值