mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4mobile wallpaper 5mobile wallpaper 6mobile wallpaper 7
66 字
1 分钟
结构体与链表
2026-07-12

一、结构体基础#

1.1 计算学生平均成绩#

#include<stdio.h>
#define N 8
typedef struct stu
{
char num[10];
double s[N];
double ave;
} STREC;
void fun(STREC *a)
{
int i = 0;
a->ave = 0.0;
for(i = 0; i < N; i++)
{
a->ave += a->s[i];
}
a->ave /= N;
}
int main()
{
STREC s = {"GA005", 85.5, 76, 69.5, 85, 91, 72, 64.5, 87.5};
int i;
fun(&s);
printf("%s \n", s.num);
for(i = 0; i < N; i++)
{
printf("%4.1f ", s.s[i]);
}
printf("%7.3f ", s.ave);
return 0;
}

1.2 筛选高于平均分的学生#

#include <stdio.h>
#include <string.h>
#define N 12
typedef struct
{
char num[10];
int s;
} STREC;
double fun(STREC *a, STREC *b, int *n)
{
float avg = 0.0;
for(int i = 0; i < N; i++)
{
avg += a[i].s;
}
avg /= N;
for(int i = 0, j = 0; i < N; i++)
{
if(a[i].s > avg)
{
b[j++] = a[i];
(*n)++;
}
}
return avg;
}
void main()
{
STREC s[N] = {{"GA05", 85}, {"GA03", 76}, {"GA02", 69}, {"GA04", 85}, {"GA01", 91}, {"GA07", 72}, {"GA08", 64}, {"GA06", 87}, {"GA09", 60}, {"GA11", 79}, {"GA12", 73}, {"GA10", 90}};
STREC h[N];
int i, n;
double ave;
ave = fun(s, h, &n);
printf("The %d student data which is higher than %7.3f:\n", n, ave);
for (i = 0; i < n; i++)
printf("%s %d\n", h[i].num, h[i].s);
printf("\n");
}

二、链表操作#

2.1 创建链表并插入节点#

#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{
int data;
struct list* next;
} SLIST;
void fun(SLIST* h, int x)
{
SLIST* p, * q, * s;
s = (SLIST*)malloc(sizeof(SLIST));
s->data = x;
q = h;
p = h->next;
while (p != NULL && x > p->data)
{
q = p;
p = p->next;
}
s->next = p;
q->next = s;
}
SLIST* creatlist(int* a)
{
SLIST* h, * p, * q;
int i;
h = p = (SLIST*)malloc(sizeof(SLIST));
for (i = 0; i < N; i++)
{
q = (SLIST*)malloc(sizeof(SLIST));
q->data = a[i];
p->next = q;
p = q;
}
p->next = 0;
return h;
}
void outlist(SLIST* h)
{
SLIST* p;
p = h->next;
if (p == NULL)
printf("\nThe list is NULL!\n");
else
{
printf("\nHead");
do
{
printf("->%d", p->data);
p = p->next;
} while (p != NULL);
printf("->End\n");
}
}
void main()
{
SLIST* head;
int x;
int a[N] = { 11, 12, 15, 18, 19, 22, 25, 29 };
head = creatlist(a);
printf("\nThe list before inserting:\n");
outlist(head);
printf("\nEnter a number: ");
scanf("%d", &x);
fun(head, x);
printf("\nThe list after inserting:\n");
outlist(head);
}

2.2 链表逆序#

#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct node
{
int data;
struct node* next;
} NODE;
void fun(NODE* h)
{
NODE* p, * q, * r;
p = h->next;
if (p == NULL)
return;
q = p->next;
p->next = NULL;
while (q)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
h->next = p;
}
NODE* creatlist(int a[])
{
NODE* h, * p, * q;
int i;
h = (NODE*)malloc(sizeof(NODE));
h->next = NULL;
for (i = 0; i < N; i++)
{
q = (NODE*)malloc(sizeof(NODE));
q->data = a[i];
q->next = NULL;
if (h->next == NULL)
h->next = p = q;
else
{
p->next = q;
p = q;
}
}
return h;
}
void outlist(NODE* h)
{
NODE* p;
p = h->next;
if (p == NULL)
printf("The list is NULL!\n");
else
{
printf("\nHead ");
do
{
printf("->%d", p->data);
p = p->next;
} while (p != NULL);
printf("->End\n");
}
}
void main()
{
NODE* head;
int a[N] = { 2, 4, 6, 8, 10 };
head = creatlist(a);
printf("\nThe original list:\n");
outlist(head);
fun(head);
printf("\nThe list after inverting:\n");
outlist(head);
}

2.3 链表查找最大值#

#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{
int data;
struct list* next;
} SLIST;
SLIST* creatlist(char*);
void outlist(SLIST*);
int fun(SLIST* h, char ch)
{
SLIST* p;
int n = 0;
p = h->next;
while (p != NULL)
{
n++;
if (p->data == ch)
return n;
else
p = p->next;
}
return 0;
}
void main()
{
SLIST* head;
int k;
char ch;
char a[N] = { 'm', 'p', 'g', 'a', 'w', 'x', 'r', 'd' };
head = creatlist(a);
outlist(head);
printf("Enter a letter:");
scanf("%c", &ch);
k = fun(head, ch);
if (k == 0)
printf("\nNot found!\n");
else
printf("The sequence number is: %d\n", k);
}
SLIST* creatlist(char* a)
{
SLIST* h, * p, * q;
int i;
h = p = (SLIST*)malloc(sizeof(SLIST));
for (i = 0; i < N; i++)
{
q = (SLIST*)malloc(sizeof(SLIST));
q->data = a[i];
p->next = q;
p = q;
}
p->next = 0;
return h;
}
void outlist(SLIST* h)
{
SLIST* p;
p = h->next;
if (p == NULL)
printf("\nThe list is NULL!\n");
else
{
printf("\nHead");
do
{
printf("->%c", p->data);
p = p->next;
} while (p != NULL);
printf("->End\n");
}
}

2.4 删除链表中重复节点#

#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{
int data;
struct list* next;
} SLIST;
void fun(SLIST* h)
{
SLIST* p, * q;
p = h->next;
if (p != NULL)
{
q = p->next;
while (q != NULL)
{
if (p->data == q->data)
{
p->next = q->next;
free(q);
q = p->next;
}
else
{
p = q;
q = q->next;
}
}
}
}
SLIST* creatlist(int* a)
{
SLIST* h, * p, * q;
int i;
h = p = (SLIST*)malloc(sizeof(SLIST));
for (i = 0; i < N; i++)
{
q = (SLIST*)malloc(sizeof(SLIST));
q->data = a[i];
p->next = q;
p = q;
}
p->next = 0;
return h;
}
void outlist(SLIST* h)
{
SLIST* p;
p = h->next;
if (p == NULL)
printf("\nThe list is NULL!\n");
else
{
printf("\nHead");
do
{
printf("->%d", p->data);
p = p->next;
} while (p != NULL);
printf("->End\n");
}
}
void main()
{
SLIST* head;
int a[N] = { 1, 2, 2, 3, 4, 4, 4, 5 };
head = creatlist(a);
printf("\nThe list before deleting:\n");
outlist(head);
fun(head);
printf("\nThe list after deleting:\n");
outlist(head);
}
分享

如果这篇文章对你有帮助,欢迎分享给更多人!

结构体与链表
https://blog.radarweb.top/posts/c/结构体与链表/
作者
Sherry
发布于
2026-07-12
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时

目录