#include <stdio.h>
#include <malloc.h>
typedef struct node
{
int data;
struct node *next;
} node;node *CreateList(int n)
{ node *head,*p,*s;
int i;
if( n <1 || n >10000)
return 0; head =(node*)malloc(sizeof(node));
p = head;
for(i = 0; i < n-1; i++)
{ s=(node*)malloc(sizeof(node));
// scanf("%d",&s->data);
p->next =s;
p = s;
} p->next=NULL;
return head;
}void searchmid(node *head,node *mid,node *mid1,node *mid2)
{
node *p;
node *q;
p=head;
q=head;
while(p->next!=0)
{
if(p->next->next==0)
{ mid1=q;
mid2=q->next;
break;
}
else
{
mid=q->next;
} p=p->next->next;
q=q->next;
}
printf("%d\n",mid,mid1,mid2);}void main()
{
node* list;
node* mid;
node* mid1;
node* mid2;
//创建链表,把链表做好,把结构体,指针都定义好;
list = CreateList(10);
searchmid(list, mid,mid1,mid2);
//找中间结点
printf("%d\n",mid);
//显示出来中间结点
}链表structmallocsizeofprintf
#include <malloc.h>
typedef struct node
{
int data;
struct node *next;
} node;node *CreateList(int n)
{ node *head,*p,*s;
int i;
if( n <1 || n >10000)
return 0; head =(node*)malloc(sizeof(node));
p = head;
for(i = 0; i < n-1; i++)
{ s=(node*)malloc(sizeof(node));
// scanf("%d",&s->data);
p->next =s;
p = s;
} p->next=NULL;
return head;
}void searchmid(node *head,node *mid,node *mid1,node *mid2)
{
node *p;
node *q;
p=head;
q=head;
while(p->next!=0)
{
if(p->next->next==0)
{ mid1=q;
mid2=q->next;
break;
}
else
{
mid=q->next;
} p=p->next->next;
q=q->next;
}
printf("%d\n",mid,mid1,mid2);}void main()
{
node* list;
node* mid;
node* mid1;
node* mid2;
//创建链表,把链表做好,把结构体,指针都定义好;
list = CreateList(10);
searchmid(list, mid,mid1,mid2);
//找中间结点
printf("%d\n",mid);
//显示出来中间结点
}链表structmallocsizeofprintf
这个函数要用 双星 指针 即:
void searchmid(node *head,node **mid,node **mid1,node **mid2)
因为 head 不变,mid,mid1,mid2的 值 都要改变的。
#include <malloc.h>
typedef struct node
{
int data;
struct node *next;
} node;node *CreateList(int n)
{ node *head,*p,*s;
int i;
if( n <1 || n >10000)
return 0; head =(node*)malloc(sizeof(node));
p = head;
for(i = 0; i < n-1; i++)
{ s=(node*)malloc(sizeof(node));
// scanf("%d",&s->data);
p->next =s;
p = s;
} p->next=NULL;
return head;
}void searchmid(node *head,node **mid,node **mid1,node **mid2)
{
node *p;
node *q;
p=head;
q=head;
while(p->next!=0)
{
if(p->next->next==0)
{ *mid1=q;
*mid2=q->next;
break;
} else
{
*mid=q->next;
} p=p->next->next;
q=q->next;
}
printf("%d\n",mid,mid1,mid2);
}void main()
{
node* list;
node* mid;
node* mid1;
node* mid2;
//创建链表,把链条做好,把结构体,指针都定义好;
list = CreateList(3);
searchmid(list, &mid,&mid1,&mid2);
//找中间结点
printf("%d\n",mid);
//显示出来中间结点
}
#include <malloc.h>typedef struct node
{
int data;
struct node *next;
} node;node *CreateList(int n)
{
node *head,*p,*s;
int i;
if( n <1 || n >10000) return 0; head =(node*)malloc(sizeof(node));
p = head;
for(i = 0; i < n-1; i++)
{
s=(node*)malloc(sizeof(node));
// scanf("%d",&s->data);
p->next =s;
p = s;
p->data=5;
}
p->next=NULL;
return head;
}void searchmid(node *head,node **mid,node **mid1,node **mid2)
{
node *p;
node *q;
p=head;
q=head;
while(p->next!=0)
{
if(p->next->next==0)
{
*mid1=q;
*mid2=q->next;
break;
}
else
{
*mid=q->next;
}
p=p->next->next;
q=q->next;
}
printf("mid=0x%08X mid1=0x%08X mid2=0x%08X\n",mid,mid1,mid2);
}void main()
{
node* list;
node* mid;
node* mid1;
node* mid2;
//创建链表,把链条做好,把结构体,指针都定义好;
list = CreateList(3);
searchmid(list, &mid,&mid1,&mid2);//找中间结点
printf("%d\n",mid->data);
//显示出来中间结点
}
这句显示 节点 位置 的 。
这个 内存地址 由 操作系统 管理,一样不一样 与你 何干 ?
#include <malloc.h>
typedef struct node
{
int data;
struct node *next;
} node;node *CreateList(int n)
{ node *head,*p,*s;
int i;
if( n <1 || n >10000)
return 0; head =(node*)malloc(sizeof(node));
p = head;
for(i = 0; i < n-1; i++)
{ s=(node*)malloc(sizeof(node));
// scanf("%d",&s->data);
p->next =s;
p = s;
} p->next=NULL;
return head;
}void searchmid(node *head,node **mid,node **mid1,node **mid2)
{
int a;
node *p;
node *q;
p=head;
q=head;
a=0;
while(p->next!=0)
{
if(p->next->next==0)
{
a=2;
*mid1=q;
*mid2=q->next;
break;
} else
{
*mid=q->next;
} p=p->next->next;
q=q->next;
}
if(a==2)
{
printf("%x %x\n",*mid1,*mid2);
}
else
{
printf("abc%x\n",*mid); }
}void main()
{
node* list;
node* tempList;
node* mid;
node* mid1;
node* mid2;
int number;
int i;
number = 1;
//创建链表,把链条做好,把结构体,指针都定义好;
list = CreateList(number);
searchmid(list, &mid,&mid1,&mid2);
for ( i = 0; i < number; i++)
{
tempList = list;
list = list->next;
free(tempList);
} return;
//找中间结点
//显示出来中间结点
}
这是我问我哥哥后进行修改后的程序,但就是number等于1时有问题,number为其他值都符合了。。忘记告诉你了,这个题目是一个长度不定的单链表只遍历一次求出其中间结点的位置
=1时,只有head 吧。
mid mid1...