70,020
社区成员




#include<stdio.h>
#include<stdlib.h>
typedef struct inlist
{
int data;
struct inlist *next;
} inlist;
inlist *creatlist(inlist *l);
int len_list(inlist *I);
void print_list(inlist *l);
void free_list(inlist *l);
int main()
{
inlist *l=(inlist *)malloc(sizeof(inlist));
int x=0,k=0,len=0;
if(l == NULL) {
exit(1);
}
l->next = NULL;
l=creatlist(l);
print_list(l);
len=len_list(l);
printf("len=%d\n", len);
free_list(l);
return 0;
}
inlist *creatlist(inlist *l)
{
inlist *p = NULL; //=(inlist *)malloc(sizeof(inlist));
inlist *q = l;
//*q=malloc(sizeof(inlist));
int e=0;
//p=l;
while(1)
{
//p->next=q;
//p=p->next;
scanf("%d",&e);
if(e == -1) {
break;
}
p=(inlist *)malloc(sizeof(inlist));
if(p == NULL) {
exit(1);
}
p->data=e;
p->next = NULL;
q->next = p;
q = p;
}
q->next = NULL;
//free(p);
//free(q);
return l;
}
int len_list(inlist *l)
{
inlist *p; /*=malloc(sizeof(inlist));*/
int i=0;
p=l->next;
//p=p->next;
//while(p->next!=NULL) //*一执行到p->next=NULL时,就出现Unhandle exception in 21302.exe:0x0000005:Access Violation 为什么?????
while(p != NULL)
{
i++;
p=p->next;
}
return i;
}
void print_list(inlist *l)
{
inlist *p;
p=l->next;
while(p != NULL)
{
printf("%d ", p->data);
p=p->next;
}
printf("\n");
}
void free_list(inlist *l)
{
inlist *p, *q;
p = l;
while(p != NULL)
{
q = p->next;
free(p);
p = q;
}
}
#include <iostream>
#include "malloc.h"
#define LEN sizeof(struct node)
struct node
{
int m;
struct node *prev,*next;
};
struct node* create(int n)
{
struct node *first,*last,*temp;
first =last = new node;
first->m = 1;
first->next = NULL;
last = first;
for (int i =2;i<=n;i++)
{
temp =(struct node*)malloc(LEN);
temp ->m =i;
temp->next = NULL;
last->next =temp;
temp->prev =last;
last = temp;
}
last->next =first;
first->prev =last;
return first;
}
void main()
{
int n;
scanf("%d",&n);
struct node *move,*link,*temp;
move =link = create(n);
while(move !=link);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
printf("%d",move->m);
move=move->next;
}
move = move->next;
printf("\n");
}
free(move);
}*/
inlist *creatlist(inlist *l)
{
inlist *p=malloc(sizeof(inlist)),
*q=malloc(sizeof(inlist));
q->next=NULL; //先初始化一下
int e=0;
l->next=p;//p=l;
while(e!=-1)
{
p->next=q;
p=p->next;
scanf("%d",&e);
p->data=e;
q=malloc(sizeof(inlist));
q->next=NULL; //加上这个
}
p->next=NULL;
//free(p);
free(q);
return l;
}
inlist *creatlist(inlist *l)
{
inlist *p=malloc(sizeof(inlist)),
*q=malloc(sizeof(inlist));
int e=0;
p=l;
while(e!=-1)
{
p->next=q; // 是这里有问题吧
p=p->next;
scanf("%d",&e);
p->data=e;