Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

是不是malloc用得太多了?

Posted by FinalLaugh at 2004-05-24 19:19:29 on Problem 1028
In Reply To:高手们帮我看看这程序为什么是access violation Posted by:winifreder at 2004-05-24 07:00:07
> #include<stdio.h>
> #include<malloc.h>
> #include<string.h>
> typedef struct node
> {
> 	char a[80];
> 	struct node *next;
> 	struct node *pre;
> }Node;
> Node *head,*p;
> /*function poiter int (*p[21])()
>   visit()
>   back()
>   forward()  flag
>   */
> 
> 				
> void visit()
> {
> 	Node *q;
> 	while(p->next!=NULL)
> 	{
> 		q=p->next;
> 		p->next=q->next;
> 		free(q);
> 	}
> 	q=(Node*)malloc(sizeof(Node));
> 	scanf("%s",q->a);
> 	q->pre=p;q->next=NULL;p->next=q;
> 	p=q;
> 	printf("%s\n",p->a);
> }
> void forward()
> {
> 	if(p->next==NULL)
> 		printf("Ignored\n");
> 	else
> 	{
> 		p=p->next;
> 		printf("%s\n",p->a);
> 	}
> }
> void back()
> {
> 	if(p->pre==NULL)
> 		printf("Ignored\n");
> 	else 
> 	{
> 		p=p->pre;
> 		printf("%s\n",p->a);
> 	}
> }
> void quit()
> {
> 	Node *q;
> 	
> 		while(p->next!=NULL)
> 	{
> 		q=p->next;
> 		p->next=q->next;
> 		free(q);
> 	}
> 		
> 			while(p->pre!=NULL)
> 		{
> 			q=p->pre;
> 			p->pre=q->next;
> 			free(q);
> 		}
> 			free(p);
> }
> 		
> int main()
> {
> 	void (*fp[20])();
> 	char word[8];
> 	p=(Node*)malloc(sizeof(Node));
> 	strcpy(p->a,"http://www.acm.org/");
> 	head=p;
> 	p->next=p->pre=NULL;
> 	fp['V'-'B']=visit;
> 	fp['F'-'B']=forward;
> 	fp['B'-'B']=back;
> 	scanf("%s",word);
> 	while(word[0]!='Q')
> 	{
> 		(*fp[word[0]-'B'])();
> 		scanf("%s",word);
> 	}
> 	quit();
> 	return 0;
> }

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator