#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *period;
struct node *next;
}ElemSN;
ElemSN *Createlink(int *Data,int n)
{
ElemSN *np,*head=NULL,*tail;
for(int i=0;i<n;i++){//创建新结点
np=(ElemSN *)malloc(sizeof(ElemSN));
np->data=*(Data+i);
np->next=np->period=NULL;
if(!head){//挪头指针
head=tail=np;
}
else{//挂链挪尾指针
tail->next=np;
np->period=tail;
tail=tail->next;
}
}
return head;
}
void Printlink(ElemSN *head)
{
ElemSN *p;
for(p=head;p;p=p->next)
printf("%5d",p->data);//正向输出
}
int main()
{
int n,i;
int *Data;
ElemSN *head=NULL;
printf("Please input the n:");
scanf("%d",&n);//输入数组长度
Data=(int *)malloc(n*sizeof(int));
printf("Please input the Data[i]:");
for(i=0;i<n;i++){//输入数组元素
scanf("%d",Data+i);
}
head=Createlink(Data,n);//创建双向链表
Printlink(head);//输出
free(Data);//释放内存空间
return 0;
}
双向链表的创建
最新推荐文章于 2023-09-17 20:54:31 发布