#include <stdlib.h>
#include <string.h>
typedef struct node{
char no[20]//存放编号
char name[40]//存放名称
int reserve//库存
struct node *next
}NODE
typedef struct link{
NODE *front//头指针
NODE *rear//尾指针
}LINK;
NODE *create_node(void){
NODE *node = (NODE *)malloc(sizeof(NODE))
printf("请输入货物编号:")
gets(node->no)
printf("请输入货物名称:")
gets(node->name)
printf("请输入货物名称:")
char ch
while( (ch= getchar()) != '\n')//rewind(stdin)
scanf("%d",&node->reserve)
node->next = NULL
return node
}
void init_link(LINK *link){
link->rear = NULL
link->front = NULL
}
int link_empty(LINK *link){
return link->front == NULL ? 1: 0
}
int node_num(LINK *link){
int num = 0
if( link_empty(link)){
return num
}
num = 1
NODE *node = link->front
while(node != link->rear){
node = node->next
++num
}
return num
}
/*NODE *node_find(LINK *link,const int n){
int num = node_num(link)
if(num < n){
printf("公有节点%d个,无法找到第%d个节点\n",num,n)
}
else{
}
}
*/
void node_push(LINK *link){
NODE *node = create_node()
if(link->front == NULL){
link->front = node
link->rear = node
node->next = NULL
}
else{
link->rear->next = node
link->rear = node
node->next = NULL
}
}
void node_insert(LINK *link,const int n){
int num = 0,i = 1
NODE *node = link->front
NODE *new_node = NULL
if ( link_empty(link) ){
printf("链表为空,将建立链表!\n")
node_push(link)
}
else{
if( n <= 1){
printf("在链表头插入数据\n")
new_node = create_node()
new_node->next = link->front
link->front = new_node
}
else if( n>= num = node_num(link) ){
printf("节点数少于%d,将在末尾插入节点.\n",n)
node_push(link)
}
else{
printf("在第n个节点后插入数据\n")
if(num >= n){
while( i != n){
node = node->next
++i
}
new_node = create_node()
new_node-next = node->next
node->next = new_node
}
}
}
void find_node_insert(LIKNK *link,const char *name){
NODE *node = link->front
if( link_empty(link) )
node_push(link)
else {
while(strcmp(node->name,name) != 0){
if(node != link->rear)
node = node->next
else break
}
if(node != NULL){
NODE *new_node = create_node()
new_node-next = node->next
node->next = new_node
}
else {
printf("没有找到相关货物,将在头节点插入数据\n")
intsert(link,0)
}
}
有个以前练习写的。//利用单链表编写一个学生成绩系统。(具有查询成绩、修改成绩、删除成绩、添加成绩、全班平均等功能。)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 100
typedef char nametype
typedef float gradetype
typedef struct node{
nametype name[MAXSIZE]
gradetype grade
struct node *next
}linklist,*plinklist
plinklist init(){
plinklist L
L=(plinklist)malloc(sizeof(linklist))
L->next =NULL
return L
}
plinklist creat_tail(plinklist L){
plinklist s
printf("name &grade:")
s=(plinklist)malloc(sizeof(linklist))
scanf("%s%f",s->name,&s->grade)
s->next=L->next
L->next=s
return L
}
plinklist locate_name(plinklist L,nametype x[]){
plinklist p
p=L
while(p->next!=NULL){
p=p->next
if(strcmp(p->name,x)==0)return p
}
return NULL
}
int delete_list(plinklist L,plinklist p){
plinklist q=L
while(q->next!=NULL){
if(q->next ==p) break
q=q->next
}
if(q->next ==p){
q->next =p->next
free(p)
return 1
}
else{
printf("不在该表中\n")
return 0
}
}
/*********平均分数***********/
void average(plinklist L){
float i
float sum=0.0
plinklist p=L
for(i=0p->next!=NULLi++){
p=p->next
sum=sum+p->grade
}
printf("%.2f\n",sum/i)
}
/*******添加成绩*************/
void insert_linklist(plinklist L){
L=creat_tail(L)
putchar(10)
printf("insert succeed\n")
}
/**********成绩查找**********/
void locate(plinklist L){
nametype name[MAXSIZE]
plinklist p
printf("name search:")
scanf("%s",name)
putchar(10)
p=locate_name(L,name)
printf("%s %.2f\n",p->name,p->grade)
}
/**********修改成绩**************/
void Amendment(plinklist L){
nametype name[MAXSIZE]
plinklist p
printf("name:")
scanf("%s",name)
putchar(10)
p=locate_name(L,name)
printf("输入修改信息,姓名与分数:")
scanf("%s%f",p->name,&p->grade)
printf("\nAmendment succeed\n")
}
/**********删除成绩*********/
void del(plinklist L){
nametype name[MAXSIZE]
plinklist p
printf("name delete:")
scanf("%s",name)
putchar(10)
p=locate_name(L,name)
if(delete_list(L,p))
printf("delete succeed\n")
}
main()
{
int n,i
/******创建学生信息*********/
plinklist H
H=init()
printf("输入学生数量")
scanf("%d",&n)
for(i=0i<ni++)
H=creat_tail(H)
putchar(10)
printf("*****成绩查询*****\n")
locate(H)
printf("****修改成绩*****\n")
Amendment(H)
printf("****删除成绩*****\n")
del(H)
printf("****添加成绩*****\n")
insert_linklist(H)
printf("****全班平均*****\n")
average(H)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)