
c语言中的链表:
我不懂链表这里有个链表程序高手帮我分析一下(最好每句都能讲讲)#include<stdio.h>#include<stdlib.h>#defineN8typedefstr...
我不懂链表这里有个链表程序高手帮我分析一下(最好每句都能讲讲)
#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
void fun( SLIST *p)
{ SLIST *t, *s;
t=p->next; s=p;
while(t->next != NULL)
{ s=t;
/**********found**********/
t=t->next;
}
/**********found**********/
printf(" %d ",t->data);
s->next=NULL;
/**********found**********/
free(t);
}
SLIST *creatlist(int *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i<N; i++)
{ q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i]; p->next=q; p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("\nThe list is NULL!\n");
else
{ printf("\nHead");
do { printf("->%d",p->data); p=p->next; } while(p!=NULL);
printf("->End\n");
}
}
main()
{ SLIST *head;
int a[N]={11,12,15,18,19,22,25,29};
head=creatlist(a);
printf("\nOutput from head:\n"); outlist(head);
printf("\nOutput from tail: \n");
while (head->next != NULL){
fun(head);
printf("\n\n");
printf("\nOutput from head again :\n"); outlist(head);
}
} 展开
#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
void fun( SLIST *p)
{ SLIST *t, *s;
t=p->next; s=p;
while(t->next != NULL)
{ s=t;
/**********found**********/
t=t->next;
}
/**********found**********/
printf(" %d ",t->data);
s->next=NULL;
/**********found**********/
free(t);
}
SLIST *creatlist(int *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i<N; i++)
{ q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i]; p->next=q; p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("\nThe list is NULL!\n");
else
{ printf("\nHead");
do { printf("->%d",p->data); p=p->next; } while(p!=NULL);
printf("->End\n");
}
}
main()
{ SLIST *head;
int a[N]={11,12,15,18,19,22,25,29};
head=creatlist(a);
printf("\nOutput from head:\n"); outlist(head);
printf("\nOutput from tail: \n");
while (head->next != NULL){
fun(head);
printf("\n\n");
printf("\nOutput from head again :\n"); outlist(head);
}
} 展开
1个回答
展开全部
##include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list //定义链表节点类型,list是链表名,typedef是定义新类型的关键字
{
int data; // 定义数据域
struct list *next; // 定义链域
} SLIST;
void fun( SLIST *p)
{
SLIST *t, *s;
t=p->next; s=p;
while(t->next != NULL) //判断指针t的链域是否为空,如果为空则表明已到链表的尾部
{ s=t; //s用来暂存结点信息,当循环结束时,s保存的是尾结点
/**********found**********/
t=t->next;
}
/**********found**********/
printf(" %d ",t->data);
s->next=NULL;
/**********found**********/
free(t); //释放尾结点
}
SLIST *creatlist(int *a) // creatlist用来创建链表 ,将数组a中的各个元素依次存入新分配的存储单元中
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST)); // 申请头结点,将申请到的存储空间的首地址赋给指针p和h
for(i=0; i<N; i++)
{ q=(SLIST *)malloc(sizeof(SLIST)); //申请新节点,并将数组的内容存到节点的链域中
q->data=a[i]; p->next=q; p=q; // 指针p相当于一个游标,每次申请新结点后就自动往后移一个位置(p=q);
}
p->next=0; //将最后一个结点的链域置空
return h; //返回头结点
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("\nThe list is NULL!\n"); //判断指针p是否为空
else
{ printf("\nHead"); //首先打印显示头结点
do { printf("->%d",p->data); p=p->next; } while(p!=NULL); //然后将结点逐个打印出来
printf("->End\n");
}
}
main()
{ SLIST *head;
int a[N]={11,12,15,18,19,22,25,29};
head=creatlist(a); //创建链表,并将首地址赋给指针head
printf("\nOutput from head:\n"); outlist(head);
printf("\nOutput from tail: \n");
while (head->next != NULL){
fun(head); //调用函数fun,释放尾结点
printf("\n\n");
printf("\nOutput from head again :\n"); outlist(head); //打印显示链表的信息
}
}
#include <stdlib.h>
#define N 8
typedef struct list //定义链表节点类型,list是链表名,typedef是定义新类型的关键字
{
int data; // 定义数据域
struct list *next; // 定义链域
} SLIST;
void fun( SLIST *p)
{
SLIST *t, *s;
t=p->next; s=p;
while(t->next != NULL) //判断指针t的链域是否为空,如果为空则表明已到链表的尾部
{ s=t; //s用来暂存结点信息,当循环结束时,s保存的是尾结点
/**********found**********/
t=t->next;
}
/**********found**********/
printf(" %d ",t->data);
s->next=NULL;
/**********found**********/
free(t); //释放尾结点
}
SLIST *creatlist(int *a) // creatlist用来创建链表 ,将数组a中的各个元素依次存入新分配的存储单元中
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST)); // 申请头结点,将申请到的存储空间的首地址赋给指针p和h
for(i=0; i<N; i++)
{ q=(SLIST *)malloc(sizeof(SLIST)); //申请新节点,并将数组的内容存到节点的链域中
q->data=a[i]; p->next=q; p=q; // 指针p相当于一个游标,每次申请新结点后就自动往后移一个位置(p=q);
}
p->next=0; //将最后一个结点的链域置空
return h; //返回头结点
}
void outlist(SLIST *h)
{ SLIST *p;
p=h->next;
if (p==NULL) printf("\nThe list is NULL!\n"); //判断指针p是否为空
else
{ printf("\nHead"); //首先打印显示头结点
do { printf("->%d",p->data); p=p->next; } while(p!=NULL); //然后将结点逐个打印出来
printf("->End\n");
}
}
main()
{ SLIST *head;
int a[N]={11,12,15,18,19,22,25,29};
head=creatlist(a); //创建链表,并将首地址赋给指针head
printf("\nOutput from head:\n"); outlist(head);
printf("\nOutput from tail: \n");
while (head->next != NULL){
fun(head); //调用函数fun,释放尾结点
printf("\n\n");
printf("\nOutput from head again :\n"); outlist(head); //打印显示链表的信息
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询