第1个回答 2011-06-30
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <assert.h>
#define N 10
#define MAX_SIZE 20
int main(void) {
typedef struct node {
int student_num;
int score;
char name[MAX_SIZE];
struct node* next;
} Node;
Node *root = NULL, *current = NULL, *next = NULL, *new_node = NULL;
int i, size=0;
FILE* fp;
current = root;
for (i = 0; i < N; ++i) {
new_node = (Node*)malloc(sizeof(Node));
assert(new_node != NULL);
new_node->next = NULL;
printf("请依次输入学号、分数和姓名:\n");
scanf("%d%d%s", &new_node->student_num, &new_node->score, new_node->name);
/* 在当前指向的节点后插入节点 */
if (current == root) {
root = new_node;
} else {
current->next = new_node;
}
/* 指向下一个 */
current = new_node;
}
/* 打开文件 */
fp = fopen("test.txt", "a");
if (fp == NULL) {
perror("test.txt");
return 1;
}
for (current = root; current != NULL; current = current->next) {
fprintf(fp, "%d %d %s\n", current->score, current->student_num, current->name);
}
fclose(fp);
for (current = root; current != NULL; current = next) {
next = current->next;
free(current);
}
system("echo 按任意test.txt&&pause>nul&&start test.txt");
return 0;
}
第2个回答 2011-06-30
typedef struct {
int no;
char name[20];
int sex;
int age;
}stuinfo;
typedef struct node{
stuinfo data;
struct node* next;
}Stunode *Stulist;
Stunlist RemoveByAge(int tage,Stulist thelist)
{
Stunode *p=thelist,*q=NULL;
q=p->next;
while(q!=null)
{
If(q->data.age==tage)
{
p->next=q->next;
free(q);
}
else
{
p=p->next
}
q=p->next;
}
return thelist;
}
生成链表时假设有头结点本回答被提问者采纳
第3个回答 2011-07-02
do
{
printf("%c",p->data);
}
while((p=p->next)!=NULL); /*遍历 链表*/
}
void main()
{
LINE *head;
Create(head);
printf("输入的文章为:");
OutPut(head);
printf("\n");
printf("全部字母数:%d \n",CountLetter(head));
printf("数字个数:%d \n",CountNumber(head));
printf("空格个数: %d \n",CountSpace(head));
printf("文章总字数: %d \n",CountAll(head));
char str1[20],str2[20];
printf("\n");
printf("请输入要统计的字符串:");
scanf("%s",str1);
printf("%s出现的次数为:%d \n",str1,FindString(head,str1));
printf("\n");
printf("请输入要删除的某一字符串:");
scanf("%s",str2);
DelString(head,str2);
printf("删除%s后的文章为:",str2);
OutPut(head);
}