请帮忙编写一个程序 ,关于查找、删除 、插入的同学成绩 的线性表 ,C语言的数据结构
3个回答
展开全部
这是我去年做的一个例子,和你这个很相似,只要稍微修改就好了。楼主请采纳我吧,我很需要分数去提问。谢谢!
/*
时间:2013年11月16日20:23:01
说明:用静态数组表示循环队列(对列:插入和删除分别在两端进行)
错误总结:
1. 公式必须是 pa->front = (pa->front + 1) % N 不能将 % 写成 /
2. 结构体中存放着 front rear 和 数组的首地址,用结构体成员管理结构体内的数组
3. front 和 rear 必须是数字,代表位置,不能使数组元素的地址
4. return 和 exit(-1) 不能混着用,各自的作用要搞清楚,exit(-1)用在该操作不成功,其他操作都无法进行的情况下,终止整个程序
5. 遍历整个队列必须用函数遍历,不能用printf("%d ", pa->arr[number]) 否则显示的元素的顺序就乱掉,用函数遍历才能体现队列先进后出的含义、
6. 函数Traver_queue() 遍历函数 中一定要定义一个中间变量Trave_num代替pa->front,不能使得pa->front的值在遍历的过程中改变
*7. 函数Traver_queue()中Trave_num 每完成一次输出,也要进行 Trave_num = ( Trave_num + 1 ) % N; 的变化,否则无法实现循环遍历
*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define N 6 //定义循环数组长度
struct pArr
{
int * arr; //定义数组
int front; //定义头位置
int rear; //定义尾位置
};
void Create_queue( struct pArr * pa, int len ); //创建数组
void Insert_queue( struct pArr * pa, int val ); //插入元素(入队)
void Delete_queue( struct pArr * pa ); //删除元素(出队)
void Traver_queue( struct pArr * pa ); //遍历队列
bool is_full( struct pArr * pa ); //判断队列是否已满
int main(void)
{
int num;
int num_1;
int num_2;
struct pArr a;
Create_queue( &a, N );
printf("input %d params\n", N - 1 );
for (int i = 0; i < (N - 1); i++)
{
scanf("%d", &num);
Insert_queue( &a, num );
}
printf("The queue as following:\n");
Traver_queue( &a );
printf("what do you want to do?\n1 insert queue\n2 delete queue\n");
scanf("%d", &num );
if ( 1 == num )
{
printf("input a numer: ");
scanf("%d", &num_1);
Insert_queue( &a, num_1 );
printf("The inserted queue as following:\n");
Traver_queue( &a );
}
else if (2 == num)
{
printf("queue has been delete! \n");
printf("The delete queue has following:\n");
Delete_queue( &a );
Traver_queue( &a );
}
/* 释放内存,重置变量 */
free( a.arr );
a.arr = NULL;
a.front = 0;
a.rear = 0;
return 0;
}
void Create_queue( struct pArr * pa, int len )
{
pa->arr = NULL; //初始化队列数组
pa->arr = ( int * )malloc( sizeof( int ) * len );//创建队列
if ( NULL == pa->arr )
{
printf("Create_queue error!\n");
/*
这里的exit( -1 )很重要,因为如果创建数组失败,那么就不可能完成接下来的插入、删除操作
exit 和 return的区别是当该操作未成功,其他操作建立在该操作基础上都无法实现,那么直接exit
return则比较温和
*/
exit( -1 );
}
else
{
pa->front = pa->rear = 0; //初始化位置变量
return;
}
}
void Insert_queue( struct pArr * pa, int val )
{
if ( !is_full( pa ) )
{
pa->arr[pa->rear] = val;
pa->rear = ( pa->rear + 1 ) % N; //是取余数% 不是 /
}
else
{
printf("The queue is full!\n");
//这里可以return不用exit( -1 )因为当前的元素插入不成功,不代表其他元素插入不成功,还可以对其他插入成功的元素进行操作
//如果直接exit( -1 ) 就把前面插入成功的数字也否定了
return;
}
return;
}
bool is_full( struct pArr * pa )
{
if ( (pa->rear + 1) % N == pa->front ) //是取余数% 不是 /
{
return true; // 数组已满
}
else
{
return false;
}
}
void Delete_queue( struct pArr * pa )
{
if ( pa->front == pa->rear )
{
printf("The arr is empty and Dot Delete!\n");
return;
}
else
{
pa->front = ( pa->front + 1 ) % N; //是取余数% 不是 /
return;
}
}
void Traver_queue( struct pArr * pa )
{
int Trave_num = pa->front;
while ( Trave_num % N != pa->rear )
{
printf("%d ", pa->arr[Trave_num]);
Trave_num = (Trave_num + 1) % N;
}
printf("\n");
return;
}
展开全部
1、查找成绩
#include<stdio.h>
#include<string.h>
#define MAX 101
struct aa/*定义结构体aa存储姓名和电话号码*/
{
char name[15];
char tel[15];
};
int readin(struct aa *a)/*自定义函数readin,用来存储姓名及电话号码*/
{
int i=0,n=0;
while(1)
{
scanf("%s",a[i].name);/*输入姓名*/
if(!strcmp(a[i].name,"#"))
break;
scanf("%s",a[i].tel);/*输入电话号码*/
i++;
n++;/*记录的条数*/
}
return n;/*返回条数*/
}
search(struct aa *b,char *x,int n)/*自定义函数search,查找姓名所对应的电话号码*/
{
int i;
i=0;
while(1)
{
if(!strcmp(b[i].name,x))/*查找与输入姓名相匹配的记录*/
{
printf("name:%s tel:%s\n",b[i].name,b[i].tel);/*输出查找到的姓名所对应的电话号码*/
break;
}
else
i++;
n--;
if(n==0)
{
printf("No found!");/*若没查找到记录输出提示信息*/
break;
}
}
}
main()
{
struct aa s[MAX];/*定义结构体数组s*/
int num;
char name[15];
num=readin(s);/*调用函数readin*/
printf("input the name:");
scanf("%s",name);/*输入要查找的姓名*/
search(s,name,num);/*调用函数search*/
}
2、删除成绩
#include "stdio.h"
struct student
{
int num;
struct student *next;
};
struct student *create(int n)
{
int i;
struct student *head, *p1, *p2;
int a;
head = NULL;
printf("The record:\n");
for (i = n; i > 0; --i)
{
p1 = (struct student*)malloc(sizeof(struct student)); /*分配空间*/
scanf("%d", &a);
p1->num = a; /*数据域赋值*/
if (head == NULL)
{
head = p1;
p2 = p1;
}
else
{
p2->next = p1; /*指定后继指针*/
p2 = p1;
}
}
p2->next = NULL;
return head;
}
struct student *delnode(struct student *head, int i)
{
int j = 0;
struct student *p, *r;
p = head;
while (p && j < i - 1)
{
p = p->next;
++j;
}
if (!p->next || j > i - 1)
exit(1);
r = p->next;
p->next = r->next;
free(r);
}
void main()
{
int n, i;
int x;
struct student *q;
printf("Input the count of the nodes you want to creat:");
scanf("%d", &n);
q = create(n);
i = 2;
delnode(q, i);
printf("The third record is deleted!\nThe result is:\n");
while (q)
{
printf("%d ", q->num);
q = q->next;
} getch();
}
3、插入成绩
#include "stdio.h"
struct student
{
int num;
struct student *next;
};
struct student *create(int n)
{
int i;
struct student *head, *p1, *p2;
int a;
char b;
head = NULL;
printf("The record:\n");
for (i = n; i > 0; --i)
{
p1 = (struct student*)malloc(sizeof(struct student)); /*分配空间*/
scanf("%d", &a);
p1->num = a; /*数据域赋值*/
if (head == NULL)
{
head = p1;
p2 = p1;
}
else
{
p2->next = p1; /*指定后继指针*/
p2 = p1;
}
}
p2->next = NULL;
return head;
}
struct student *insertnode(struct student *head, char x, int i)
{
int j = 0;
struct student *p, *s;
p = head;
while (p && j < i - 1)
{
p = p->next;
++j;
}
if (!p || j > i - 1)
exit(1);
s = (struct student*)malloc(sizeof(struct student));
s->num = x;
s->next = p->next;
p->next = s;
}
void main()
{
int n, i;
int x;
struct student *q;
printf("Input the count of the nodes you want to creat:");
scanf("%d", &n);
q = create(n);
i = 2;
x = 123;
insertnode(q, x, i);
printf("The result is:\n");
while (q)
{
printf("%d ", q->num);
q = q->next;
}
}
#include<stdio.h>
#include<string.h>
#define MAX 101
struct aa/*定义结构体aa存储姓名和电话号码*/
{
char name[15];
char tel[15];
};
int readin(struct aa *a)/*自定义函数readin,用来存储姓名及电话号码*/
{
int i=0,n=0;
while(1)
{
scanf("%s",a[i].name);/*输入姓名*/
if(!strcmp(a[i].name,"#"))
break;
scanf("%s",a[i].tel);/*输入电话号码*/
i++;
n++;/*记录的条数*/
}
return n;/*返回条数*/
}
search(struct aa *b,char *x,int n)/*自定义函数search,查找姓名所对应的电话号码*/
{
int i;
i=0;
while(1)
{
if(!strcmp(b[i].name,x))/*查找与输入姓名相匹配的记录*/
{
printf("name:%s tel:%s\n",b[i].name,b[i].tel);/*输出查找到的姓名所对应的电话号码*/
break;
}
else
i++;
n--;
if(n==0)
{
printf("No found!");/*若没查找到记录输出提示信息*/
break;
}
}
}
main()
{
struct aa s[MAX];/*定义结构体数组s*/
int num;
char name[15];
num=readin(s);/*调用函数readin*/
printf("input the name:");
scanf("%s",name);/*输入要查找的姓名*/
search(s,name,num);/*调用函数search*/
}
2、删除成绩
#include "stdio.h"
struct student
{
int num;
struct student *next;
};
struct student *create(int n)
{
int i;
struct student *head, *p1, *p2;
int a;
head = NULL;
printf("The record:\n");
for (i = n; i > 0; --i)
{
p1 = (struct student*)malloc(sizeof(struct student)); /*分配空间*/
scanf("%d", &a);
p1->num = a; /*数据域赋值*/
if (head == NULL)
{
head = p1;
p2 = p1;
}
else
{
p2->next = p1; /*指定后继指针*/
p2 = p1;
}
}
p2->next = NULL;
return head;
}
struct student *delnode(struct student *head, int i)
{
int j = 0;
struct student *p, *r;
p = head;
while (p && j < i - 1)
{
p = p->next;
++j;
}
if (!p->next || j > i - 1)
exit(1);
r = p->next;
p->next = r->next;
free(r);
}
void main()
{
int n, i;
int x;
struct student *q;
printf("Input the count of the nodes you want to creat:");
scanf("%d", &n);
q = create(n);
i = 2;
delnode(q, i);
printf("The third record is deleted!\nThe result is:\n");
while (q)
{
printf("%d ", q->num);
q = q->next;
} getch();
}
3、插入成绩
#include "stdio.h"
struct student
{
int num;
struct student *next;
};
struct student *create(int n)
{
int i;
struct student *head, *p1, *p2;
int a;
char b;
head = NULL;
printf("The record:\n");
for (i = n; i > 0; --i)
{
p1 = (struct student*)malloc(sizeof(struct student)); /*分配空间*/
scanf("%d", &a);
p1->num = a; /*数据域赋值*/
if (head == NULL)
{
head = p1;
p2 = p1;
}
else
{
p2->next = p1; /*指定后继指针*/
p2 = p1;
}
}
p2->next = NULL;
return head;
}
struct student *insertnode(struct student *head, char x, int i)
{
int j = 0;
struct student *p, *s;
p = head;
while (p && j < i - 1)
{
p = p->next;
++j;
}
if (!p || j > i - 1)
exit(1);
s = (struct student*)malloc(sizeof(struct student));
s->num = x;
s->next = p->next;
p->next = s;
}
void main()
{
int n, i;
int x;
struct student *q;
printf("Input the count of the nodes you want to creat:");
scanf("%d", &n);
q = create(n);
i = 2;
x = 123;
insertnode(q, x, i);
printf("The result is:\n");
while (q)
{
printf("%d ", q->num);
q = q->next;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我有java的
更多追问追答
追问
我们学的是C语言 对java没有涉猎
追答
好吧,不过编程语言都有共通性,你学的c++?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询