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

来源:互联网 发布:shutting down mysql 编辑:程序博客网 时间:2024/05/17 23:03

#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
原创粉丝点击