| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1177 人关注过本帖
标题:简单图形链表操作..(结构体中含共用体的两个变量)
只看楼主 加入收藏
grub2linux
Rank: 1
等 级:新手上路
帖 子:335
专家分:0
注 册:2004-12-25
收藏
 问题点数:0 回复次数:0 
简单图形链表操作..(结构体中含共用体的两个变量)

我改写完善了谭浩强<C程序设计>第二版中例题,"动态链表的操作",建立,插入,删除,显示,增加了简单的图形. 但还没有完全做好,先暂时只能插入或删除一次,明天再改好. 与大家C初学者共勉吧;(我也是刚学完C) 练练手吧; 大家顶一下啊!... #include <stdio.h> #include <conio.h> #include <alloc.h> #define LEN sizeof(struct stu) struct stu { unsigned long num; char name[20]; float score; union data { unsigned long u1; char u2[20]; float u3; }un1,un2;

struct stu *next; }; int n; struct stu *new(void) { int flag=0; struct stu *head; struct stu *p1,*p2; n=0; head=NULL; p1=p2=NULL;

window(15,3,75,20); textcolor(RED); textbackground(GREEN); clrscr();

cprintf("\n please input data:\n\r "); do {

p1=(struct stu *)malloc(LEN); cscanf("%lu%s%f",&p1->num,p1->name,&p1->score); cprintf("\n\r ");

if(p1->num==20040010) {cprintf(" please input the additional data of \"20040010\":\n\r"); cscanf("%lu%s",&p1->un1.u1,p1->un2.u2); cprintf("\n\r"); } if(p1->num==20040020) {cprintf(" please input the additional data of \"20040020\":\n\r"); cscanf("%s%f",p1->un1.u2,&p1->un2.u3); cprintf("\n\r"); } if(p1->num!=0) { n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; } else flag=1; }while(flag!=1); p2->next=NULL; return(head); }

void print(struct stu *head) { struct stu *p; p=head; if(p==NULL) return;

clrscr(); window(4,10,77,22); textcolor(YELLOW); textbackground(LIGHTCYAN); clrscr();

cprintf("\n the records are:\n\r"); cprintf(" number name score nextaddr add_1 add_2\n\r"); do { cprintf(" %-10lu %-20s %-6.2f %-9o",p->num,p->name,p->score,p->next);

if(p->num==20040010) cprintf(" %-10lu %-10s\n\r",p->un1.u1,p->un2.u2); if(p->num==20040020) cprintf(" %-10s %5.2f\n\r",p->un1.u2,p->un2.u3); cprintf("\n\r"); p=p->next; }while(p!=NULL); printf(" "); }

struct stu *del(struct stu *head,unsigned long num) { struct stu *p1,*p2;

window(28,12,52,14); textcolor(MAGENTA); textbackground(BLACK); clrscr();

if(head==NULL) { cprintf("\n list is NULL\n\r "); getch(); getch(); return(head); } p1=head; while(num!=p1->num&&p1->next!=NULL) { p2=p1; p1=p1->next; } if(num==p1->num) { if(p1==head) head=p1->next; else p2->next=p1->next; cprintf("\n %lu deleted\n\r ",num); /*free(p1);*/ getch();

n=n-1; } else cprintf("\n %lu not found!\n\r ",num); getch(); getch(); return(head); }

struct stu *insert(struct stu *head,struct stu *s) { struct stu *p0,*p1,*p2;

window(15,20,78,23); textcolor(LIGHTRED); textbackground(BROWN); clrscr();

p1=head; p0=s;

if(head==NULL) { head=p0; p0->next=NULL; } else { while((p0->num>p1->num)&&(p1->next!=NULL)) { p2=p1; p1=p1->next; } if(p0->num<=p1->num) { if(p1==head) { head=p0; p0->next=p1; } else { p2->next=p0; p0->next=p1; } } else { p1->next=p0; p0->next=NULL; } } n=n+1; return(head); }

main() {

struct stu *p; struct stu a;

unsigned long delnum;

window(20,13,60,18); textattr(128+13+6); clrscr(); cprintf("\n\r WELCOME TO DATA CENTER!\n\r continue?\n\r enter \"q\" to quit\n\r enter any key to start");

while(getch()!='q') {

p=new();

print(p);

getch(); getch();

window(15,8,78,13); textcolor(LIGHTRED); textbackground(BROWN); clrscr();

cprintf("\n input the data to insert:\n\r "); cscanf("%lu%s%f",&a.num,a.name,&a.score);

if(a.num==20040010) cscanf("%lu%s",&a.un1.u1,a.un2.u2); if(a.num==20040020) cscanf("%s%f",a.un1.u2,&a.un2.u3);

p=insert(p,&a); print(p);

getch(); getch();

window(23,18,55,23); textcolor(BLACK); textbackground(MAGENTA); clrscr();

cprintf("\n input the number to delete:\n\r "); cscanf("%lu",&delnum); p=del(p,delnum); print(p);

getch();

window(20,13,60,18); textattr(128+13+6); clrscr(); cprintf("\n\r WELCOME TO DATA CENTER!\n\r continue?\n\r enter \"q\" to quit\n\r enter any key to start");

} }

[此贴子已经被作者于2005-1-6 1:35:32编辑过]

搜索更多相关主题的帖子: 链表 变量 图形 结构体 
2005-01-05 23:30
快速回复:简单图形链表操作..(结构体中含共用体的两个变量)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020815 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved