链表基本操作



#include<stdio.h>

#include<string.h>

#include<stdlib.h>

typedef struct stu{ 

int d;

struct stu *l;

}st;

void xj(st *h)//生成单链表

{ st *l; 

l=h;

int m;

scanf("%d",&m);

h=(st *)malloc(sizeof(st));

while(m--)

{  

st *s;

s=new st;

scanf("%d",&s->d);

l->l=s;

l=s;

}  

l->l=NULL;

}

void shc(st *h)//输出链表

{ h=h->l;

while(h->l!=NULL)

{

printf("%d ",h->d);

h=h->l;

}

printf("%d\n",h->d);

}

void chr(st *h)//按大小插入元素

{ int n;

scanf("%d",&n);

h=h->l;

while(h!=NULL)

{  

if(n<=h->l->d)

{

st *s;

s=new st;

s->d=n;

s->l=h->l;

h->l=s;

break;

}

else

h=h->l;

}

}

void shac(st *h)//删除链表第n个元素

{ int n;

scanf("%d",&n);

int k=0;

while(h!=NULL)

{ if(k==n-1)

{

st *s;

s=new st;

s=h->l;

h->l=h->l->l;

free(s);

break;

}

h=h->l;

k++;

}

}

void dz(st *h)//倒置链表

{ st *l;

l=new st;

l=h->l->l;

h->l->l=NULL;

while(l!=NULL)

{ st *s;

s=new st;

s->d=l->d;

s->l=h->l;

h->l=s;

l=l->l;

}

}

int main()

{ st *h,*h1;

h=new st;

h1=new st;

h=h1;

xj(h);

h=h1;

shc(h);

h=h1;

chr(h);

h=h1;

shc(h);

h=h1;

shac(h);

h=h1;

shc(h);

h=h1;

dz(h);

shc(h);

return 0;

}

#include<stdio.h> #include < string .h> #include <stdlib.h> typedef struct stu{ int d; struct stu * l;}st; void xj(st * h)//生成单链表{ st * l; l = h; int m; scanf( " %d " ,& m); h =(st *) malloc ( sizeof (st)); while (m-- ) { st * s; s = new st; scanf( " %d " ,&s-> d); l ->l= s; l = s; } l ->l= NULL;} void shc(st * h)//输出链表{ h =h-> l; while (h->l!= NULL) { printf( " %d " ,h-> d); h =h-> l; } printf( " %d\n " ,h-> d);} void chr(st * h)//按大小插入元素{ int n; scanf( " %d " ,& n); h =h-> l; while (h!= NULL) { if (n<=h->l-> d) { st * s; s = new st; s ->d= n; s ->l=h-> l; h ->l= s; break ; } else h =h-> l; }} void shac(st * h)//删除链表第n个元素{ int n; scanf( " %d " ,& n); int k= 0 ; while (h!= NULL) { if (k==n- 1 ) { st * s; s = new st; s =h-> l; h ->l=h->l-> l; free (s); break ; } h =h-> l; k ++ ; }} void dz(st * h)//倒置链表{ st * l; l = new st; l =h->l-> l; h ->l->l= NULL; while (l!= NULL) { st * s; s = new st; s ->d=l-> d; s ->l=h-> l; h ->l= s; l =l-> l; }} int main(){ st *h,* h1; h = new st; h1 = new st; h = h1; xj(h); h = h1; shc(h); h = h1; chr(h); h = h1; shc(h); h = h1; shac(h); h = h1; shc(h); h = h1; dz(h); shc(h); return 0 ;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值