关于C语言单向链表的相关一系列操作(作为备忘)

#ifndef _2_H

#define _2_H

typedef struct stu

{

int  ID;

int  score;

struct stu  *next;

}STU,*PSTU;

static STU *creatList(int n);

staticint getListLen(STU *h);

staticvoid initList(STU *h,int a[],int b[]);

staticvoid showList(STU *h);

staticvoid insertListBack(STU *h,int t[],int pos);

staticvoid reverse(STU *h);

staticvoid sortList(STU *h);

staticvoid showSelectList(STU *h,int score);

staticvoid selectDel(STU *h,int score);

staticvoid delList(STU *h,int pos);

staticvoid destroy(STU *h);

#endif

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "2.h"

STU *creatList(int n)//¥¯Õ∑Ω·µ„

{

STU *p,*r,*H;

int i;

H = (STU*)malloc(sizeof(STU));

r = H;

if(H ==NULL)

{

returnNULL;

}

for(i =0;i < n;i++)

{

p = (STU*)malloc(sizeof(STU));

r->next = p;

r = p;

}

r->next =NULL;

return H;

}

int getListLen(STU *h)

{

STU *p = h->next;

int n=0;

while(p)

{

p = p->next;

n++;

}

return n;

}

void initList(STU *h,int a[],int b[])

{

STU *p;

int i=0;

for(p=h->next;p;p=p->next)

{

p->ID = a[i];

p->score = b[i];

i++;

}

return;

}

void showList(STU *h)

{

STU *p = h->next;

if(!h->next)

{

printf("this list is empty!\n");

}

while(p)

{

printf("00%d,%d\n",p->ID,p->score);

p = p->next;

}

return;

}

void insertListBack(STU *h,int t[],int pos)

{

int i;

STU *p = h,*q;

int len = getListLen(h);

if (pos > len)

{

pos = len;

}

for(i=0;i<pos;i++)

{

p=p->next;

}

q = (STU*)malloc(sizeof(STU));

q->ID = t[0];

q->score = t[1];

q->next = p->next;

p->next = q;

return;

}

void reverse(STU *h)

{

STU *p,*q;

p = h->next;

h->next =NULL;

while (p)

{

q = p;

p = p->next;

q->next = h->next;

h->next = q;

}

}

void sortList(STU *h)

{

int n;

n =sizeof(STU)-4;

STU *tmp =(STU *)malloc(n);

STU *p,*q;

p = h->next;

//for(p = h->next;p;p = p->next)

while(p)

{

if(p->next ==NULL)

{

break;

}

//for(q = p->next;q;q = q->next)

q = p->next;

while(q)

{

if(p->score < q->score)

{

memcpy(tmp,p,n);

memcpy(p,q,n);

memcpy(q,tmp,n);

}

q = q->next;

}

p = p->next;

}

free(tmp);

return;

}

void showSelectList(STU *h,int score)

{

int n,i;

STU *p;

n = getListLen(h);

p = h->next;

for(i =0;i < n;i++)

{

if(p->score >= score)

{

printf("00%d,%d\n",p->ID,p->score);

}

p = p->next;

}

return;

}

void selectDel(STU *h,int score)

{

STU *p,*q;

q=h;

while (q->next)

{

if(q->next->score<score)

{

p=q->next;

q->next=p->next;

free(p);

}

else

{

q=q->next;

}

}

return;

}

void delList(STU *h,int pos)

{

int i;

STU *p = h,*q;

int len = getListLen(h);

if (pos > len)

{

pos = len;

}

for(i=0;i<pos-1;i++)

{

p=p->next;

}

q = p->next;

p->next = q->next;

free(q);

return;

}

void destroy(STU *h)

{

STU *p = h;

while(p->next)

{

delList(h,1);

}

free(p);

return;

}

int main()

{

int i,j;

STU *head,*p,*q,*tmp;

int aa[5]={1,2,3,4,5};

int bb[5]={40,55,90,55,95};

int a[2]={6,75};

int b[2]={7,50};

int c[2]={8,50};

printf("FIRST\n");

head = creatList(5);

p = head;

initList(p,aa,bb);

showList(head);

printf("\n------------------------\n");

printf("SECOND\n");

insertListBack(p, a,0);

insertListBack(p, b,100);

insertListBack(p, c,4);

showList(head);

printf("\n------------------------\n");

printf("THIRD\n");

showSelectList(head,90);

printf("\n------------------------\n");

showSelectList(head,85);

printf("\n------------------------\n");

printf("FOURTH\n");

p = head;

reverse(p);

showList(head);

printf("\n------------------------\n");

printf("FIFTH\n");

sortList(head);

showList(head);

printf("\n------------------------\n");

printf("SIXTH\n");

selectDel(head,60);

showList(head);

printf("\n------------------------\n");

destroy(head);

showList(head);

return0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zwarwolf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值