| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 506 人关注过本帖
标题:同样郁闷的printf问题
只看楼主 加入收藏
小林
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-9-7
收藏
 问题点数:0 回复次数:2 
同样郁闷的printf问题

/*琏表的综合操作 2006年9月22日*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
int num;
struct student *next;
};
struct student *head;


struct student *creat() /*创建琏表的函数*/
{
int temp = 0;
struct student *p3,*head,*p1,*p2;
FILE *fp;
char filename[10];
printf("enter the filename\n"); /*输入存储数据的文件*/
scanf("%s",filename);
fp = fopen(filename,"r");
head = (struct student*)malloc(sizeof(struct student));
head -> num = NULL;

p2 = (struct student*)malloc(sizeof(struct student));
head -> next = p2;
p1 = (struct student*)malloc(sizeof(struct student));
p3 = head -> next ;
while(fscanf(fp,"%d",&temp) != EOF)
{

p1 -> num = temp;
p2 -> num = p1 -> num;
p2 = (struct student*)malloc(sizeof(struct student));
p3 -> next = p2;
p3 = p3 -> next;
}
p3 -> next = NULL;
fclose(fp);
return(head);
}


void print(struct student *head) /*对琏表进行输出的函数*/
{
struct student *p;
int m;
m = 0;
p = head -> next;
if(p == NULL)
printf("no link");
else
while(p != NULL)
{
m ++;
printf("%5d",p -> num);
printf("\n");
p = p -> next;
}
printf("\n");
printf("Tht link have %d element\n",m);
}


void reverse(struct student *head) /*对链表进行反向输出的函数*/
{
struct student *p,*p1,*p2;
p = head -> next;
p1 = head->next;
p2 = head;
while(p1 -> next != NULL)
{
p1 = p1 -> next;
p2 = p2 -> next;
}
p1 -> next = p;
p2 -> next = NULL;
head ->next = p1;
}

mydelete(struct student *head) /*删除琏表中某一结点的函数*/
{
struct student *p,*p1,*p2;
int n;
printf("which element you will delete\n");
scanf("%d",&n);
p = head;
p1 = head->next;
if(p1 == NULL)
printf("no link\n");
while(p1 != NULL)
{
if(p1 -> num == n)
{
p -> next = p1->next;
return 1;
}
p = p -> next;
p1 = p -> next;
}
return 0;
}


insert(struct student *head) /*对琏表的开头插入一个元素*/
{
struct student *p1,*p;
int n;
p = head;
p1 = p->next;
printf("input the element\n");
scanf("%d",&n);
while(p1 != NULL)
{
if(p1 -> num==n) /*判断琏表中是否有与要插入的元素相同的结点*/
return 0;
p = p -> next;
p1 = p -> next;
}
if(head -> next == NULL)
{
p1 = (struct student*)malloc(sizeof(struct student));
head -> next = p1;
p1 -> num = n; /*当没有相同元素时,在开始插入元素*/
p1 -> next = NULL;
}
else
{
p1 = (struct student*)malloc(sizeof(struct student));
p1 -> next = head -> next;
head -> next = p1;
p1 -> num = n;
}
printf("%d %dh",6,3); /*加上一个输出两个数的printf就可以得到想要的结果,不管输出的两个数是什么!!这个为什么,不加数就会出现像内存地址一样的数字??
return 1;
}

/*主函数*/
main()
{
struct student * head;
int h,k;
char choose;
/* 打印表头 */
printf(" **********************************************************************\n");
printf(" C--creat link,D--delete element,I--insert link,\n ");
printf("\n");
printf(" R--reverse display P--print link,O--out \n ") ;
printf("**********************************************************************\n");
for(k = 0;k <= 10;k++)
{
printf("choose...\n");
scanf("%s",&choose);

switch(choose) /*选择对琏表的操作*/
{
case'c': /*创建并打印琏表*/
{
head = creat();
printf("the link is\n");
print(head);
break;
}
case 'd':
{
h = mydelete(head);
if(h == 1)
{
printf("the new link\n");
print(head);
}
if(h == 0)
printf("Not Found The Element\n");
}
break;
case 'i': /*插入链表*/
{
h = insert(head);
if(h == 1)
{
printf("new link is\n");

print(head);
}
if(h == 0)
printf("link have had the element\n");
}
break;
case 'p': /* 打印链表*/
{
print(head);
break;
}


case 'r': /*将链表反向输出*/
{
reverse(head);
print(head);
}

default:
printf("error again\n");
}
if(choose == 'o')
break;
}
getch();
}

搜索更多相关主题的帖子: printf 
2006-09-23 16:22
tian111xia11
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-9-23
收藏
得分:0 

看晕了,

2006-09-24 22:24
zhufeifei
Rank: 1
等 级:新手上路
威 望:2
帖 子:402
专家分:0
注 册:2006-8-11
收藏
得分:0 

太长了!


在不断的拼搏与进取中,定能创造一片天地!
2006-10-04 12:46
快速回复:同样郁闷的printf问题
数据加载中...
 
   



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

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