学生信息 |
#include<stdio.h>
void main()
{
struct student
{
char name[20];
int num;
float grade;
struct student *next;
}a[100],*h,*p;
int b,c;
float d;
for(b=0;;b++)
{
scanf("%s",a[b].name);
if(a[b].name[0]=='*')
break;
scanf("%d%f",&a[b].num,&a[b].grade);
}
c=b;
h=&a[0];
for(b=0;b<c;b++)
a[b].next=&a[b+1];
a[c-1].next=NULL;
p=a;
while(p!=NULL)
{
d=p->grade+d;
p=p->next;
}
p=a;
d=d/c;
while(p!=NULL)
{
if(p->grade>=d)
printf("%s\n",p->name);
p=p->next;
}
单向链表
#include<stdio.h>
void main()
{
struct student
{
char name[20];
int num;
float grade;
struct student *next;
}a[100],*h,*p;
int b,c;
float d;
for(b=0;;b++)
{
scanf("%s",a[b].name);
if(a[b].name[0]=='*')
break;
scanf("%d%f",&a[b].num,&a[b].grade);
}
c=b;
h=&a[0];
for(b=0;b<c;b++)
a[b].next=&a[b+1];
a[c-1].next=NULL;
p=a;
while(p!=NULL)
{
d=p->grade+d;
p=p->next;
}
p=a;
d=d/c;
while(p!=NULL)
{
if(p->grade>=d)
printf("%s\n",p->name);
p=p->next;
}
}
链表的交集 |
#include<stdio.h>
int main()
{
struct data
{
int number;
struct data *next;
}a[50],b[50],c[50],*head[3],*p[3];
int d,e[2],f,g=0;
for(d=0;;d++)
{
scanf("%d",&a[d].number);
if(a[d].number==-1)
break;
}
e[0]=d-1;
for(d=0;;d++)
{
scanf("%d",&b[d].number);
if(b[d].number==-1)
break;
}
e[1]=d-1;
for(d=0;d<e[0];d++)
{
if(d==0)
head[0]=&a[d];
a[d].next=&a[d+1];
}
a[e[0]].next=NULL;
for(d=0;d<e[1];d++)
{
if(d==0)
head[1]=&b[d];
b[d].next=&b[d+1];
}
b[e[1]].next=NULL;
p[0]=head[0];
p[1]=head[1];
while(p[0]!=NULL)
{
p[1]=head[1];
while(p[1]!=NULL)
{
if(p[0]->number==p[1]->number)
{
c[g].number=p[0]->number;
g++;
}
p[1]=p[1]->next;
}
p[0]=p[0]->next;
}
if(g==0)
printf("NULL\n");
else
{
for(d=0;d<g-1;d++)
{
if(d==0)
head[2]=&c[d];
c[d].next=&c[d+1];
}
p[2]=head[2];
while(p[2]!=NULL)
{
printf("%d ",p[2]->number);
p[2]=p[2]->next;
}
printf("\n");
}
return 0;
}
算法2-8~2-11:链表的基本操作
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct list {
int data;
struct list *next;
} node;
void Greatlist(node *head,int n) {
node *p;
int num;
while(n--) {
scanf("%d",&num);
p = (node *)malloc(sizeof(node));
p->data = num;
p->next = head->next;
head->next = p;
}
}
void output(node *head) {
node *p = head->next;
if(!p) {
printf("Link list is empty");
}
while(p != NULL) {
printf("%d ",p->data);
p = p->next;
}
printf("\n");
}
void deletelist(node *head,int n,int *e) {
if(n < 1) {
printf("delete fail\n");
return;
}
int j = 0;
node *p,*pre = head;
while(pre&&j < n - 1) {
pre = pre->next;
if(!pre) {
printf("delete fail\n");
return;
}
++j;
}
p = (node *)malloc(sizeof(node));
p = pre->next;
pre->next = p->next;
*e = p->data;
free(p);
printf("delete OK\n");
}
void insertlist(node *head,int d,int dd) {
if(d < 1) {
printf("insert fail\n");
return;
}
int j = 0;
node *p,*pre = head;
while(pre&&j < d - 1) {
pre = pre->next;
if(!pre) {
printf("insert fail\n");
return;
}
j++;
}
p = (node *)malloc(sizeof(node));
p->data = dd;
p->next = pre->next;
pre->next = p;
printf("insert OK\n");
}
void getlist(node *head,int d) {
int j = 0;
node *p,*pre = head;
while(pre&&j < d - 1) {
pre = pre->next;
++j;
}
p = (node *)malloc(sizeof(node));
p = pre->next;
printf("%d\n",p->data);
}
int main(void) {
int i,num,site,n,m,e;
node *head;
head = (node *)malloc(sizeof(node));
head->next = NULL;
scanf("%d",&n);
Greatlist(head,n);
scanf("%d",&m);
while(m--) {
int b,bb;
char s[15];
scanf("%s",s);
if(!strcmp(s,"show")) {
output(head);
} else if(!strcmp(s,"delete")) {
scanf("%d",&b);
deletelist(head,b,&e);
} else if(!strcmp(s,"insert")) {
scanf("%d%d",&b,&bb);
insertlist(head,b,bb);
} else if(!strcmp(s,"get")) {
scanf("%d",&b);
getlist(head,b);
}
}
return 0;
}