关于单向链表的相关一系列操作(作为备忘)
来源:互联网 发布: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;
}
- 关于单向链表的相关一系列操作(作为备忘)
- 关于双向链表的相关一系列操作(作为备忘)
- 有序单向链表的相关操作
- 单向链表的相关操作
- 单向链表相关操作
- 单向链表相关操作
- 链表的一系列操作
- C语言中单向链表的相关操作
- C语言描述:单向链表的相关操作
- 数据结构-单向链表相关操作算法
- 链表的创建等一系列操作
- 单向链表的操作
- 单向链表的操作
- 单向链表的操作
- 单向链表的操作
- 单向链表的操作
- 单向链表的操作
- 单向链表的操作
- JAVA实现EXCEL公式专题(二)——反射解析EXCEL函数
- 剑指offer刷题之c实现的跳台阶
- Node安装之windows篇
- 三角、指数、对数、幂函数
- 编译ffmpeg + opencv
- 关于单向链表的相关一系列操作(作为备忘)
- Git之1——安装和基本仓库的初始化
- Codeforces Round #303 (Div. 2)(A,B,C,D)
- 常见的面试题(整理)
- 二叉树
- iOS蓝牙4.0开发03-执行常见的Periphral角色任务
- 图论500题
- 折腾笔记:架设OpenConnect Server给iPhone提供更顺畅的网络生活
- 关于双向链表的相关一系列操作(作为备忘)