#include "stdafx.h"
#include <stdlib.h>
struct slist
{
int info;
struct slist* next;
};
struct slist* head;
int a[8]={23,12,34,45,56,67,78,89};
void inlist(struct slist*,int);
void displist();
int main()
{
int i;
struct slist* node;
for(i=0;i<8;i++)
{
node=(struct slist*)malloc(sizeof(struct slist));
inlist(node,a[i]);
}
displist();
return 0;
}
void displist()
{
struct slist* cp;
cp=head;
while(cp!=NULL)
{
printf("%d \n",cp->info);
cp=cp->next;
}
return;
}
void inlist(struct slist* nd,int value)
{
struct slist* cp;
cp=head;
nd->info=value;
if(cp==NULL)
{
head=nd;
nd->next=NULL;
}
else
{
if(cp->info>value)
{
head=nd;
nd->next=cp;
}
else
{
while(cp->next!=NULL&&cp->next->info<value)
cp=cp->next;
nd->next=cp->next;
cp->next=nd;
}
}
return;
}
书上的链表小程序
最新推荐文章于 2024-07-05 19:29:43 发布