|
发表于 2021-2-21 19:26:03
|
显示全部楼层
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct Node
{
int value;
struct Node *next;
}Node;
void printList(Node *head)
{
head = head->next;
int i=1;
while(head!=NULL)
{
printf("%d ",head->value);
head = head->next;
}
printf("\n");
}
void makeList(Node **head,int len,int Max)
{
Node *r,*n;
r = *head;
srand((unsigned)time(NULL));
int i=1;
while(i++ <=len)
{
n = (Node*)malloc(sizeof(Node));
n->value = rand()%Max+1;
r->next =n;
n->next = NULL;
r= n;
}
printf("链表创建完毕!\n");
}
void Mid(Node *head)
{
Node *max,*min;
max = head;
min = head;
while(max->next != NULL)
{
if(max->next->next != NULL)
{
max = max->next->next;
min = min->next;
}
}
printf("中间节点的值为:%d\n",min->value);
}
int main()
{
Node *head = (Node *)malloc(sizeof(Node));
printf("1,查看链表\n");
printf("2,创建链表(尾插法)\n");
printf("3,链表长度\n");
printf("4,中间节点值\n");
printf("0,退出\n");
int ch;
int len=20,Max = 100;
while(1)
{
printf("\n请输入编号选择操作(0/1/2/3/4):\n");
scanf("%d",&ch);
switch(ch)
{
case 2:makeList(&head,len,Max);break;
case 1:printList(head);break;
case 3:printf("Length = %d\n",len);break;
case 4:Mid(head);break;
case 0:return;
default:printf("输入有误!请重新输入!");
}
}
return 0;
}
|
|