当前位置: 代码迷 >> C语言 >> 同样郁闷的printf问题
  详细解决方案

同样郁闷的printf问题

热度:421   发布时间:2006-09-23 16:22:17.0
同样郁闷的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  

----------------解决方案--------------------------------------------------------

看晕了,


----------------解决方案--------------------------------------------------------

太长了!


----------------解决方案--------------------------------------------------------
  相关解决方案