當前位置:編程學習大全網 - 編程軟體 - c語言數據結構,單鏈表的逆置

c語言數據結構,單鏈表的逆置

#include<stdio.h>

#include<stdlib.h>

typedef struct node

{

int data;

struct node*next;

}linklist;

linklist *creat()

{

linklist*head,*p,*t;

int i,n;

printf("input n:");

scanf("%d",&n);

head=(linklist*)malloc(sizeof(linklist));

t=head;

for(i=0;i<n;i++)

{

p=(linklist*)malloc(sizeof(linklist));

scanf("%d",&p->data);

t->next=p;

t=p;

}

p->next=NULL;

return head;

}

void convert(linklist*head)

{

linklist*p,*t;

p=head->next;

head->next=NULL;//把每壹個節點連接到頭結點後面即可,後面的節點與在連接

while(p)

{

t=p;

p=p->next;

t->next=head->next;

head->next=t;

}

}

void output(linklist*head)

{

linklist*p=head->next;

while(p)

{

printf("%d ",p->data);

p=p->next;

}

printf("\n");

}

void main()

{

linklist*p;

p=creat();

convert(p);

output(p);

}

  • 上一篇:如何成為源碼熊編程高手?
  • 下一篇:有哪些 Windows PC 客戶端開發的學習方法或書籍值得推薦
  • copyright 2024編程學習大全網