A. c语言指针链表具体怎么弄
#include<stdlib.h>/*含malloc()的头文件*/
#include<stdio.h>
//①定义链表数据结构
structnode
{
intnum;
structnode*next;
};
//函数声明
structnode*creat();
voidprint();
main()
{
structnode*head;
head=NULL;//②建一个空表
head=creat(head);/*创建单链表*/
print(head);/*打印单链表*/
}
/******************************************/
structnode*creat(structnode*head)/*返回的是与节点相同类型的指针*/
{
structnode*p1,*p2;
inti=1;
//③利用malloc()函数向系统申请分配一个节点
p1=p2=(structnode*)malloc(sizeof(structnode));/*新节点*/
printf("请输入值,值小于等于0结束,值存放地址为:p1_ADDR=%d ",p1);
scanf("%d",&p1->num);/*输入节点的值*/
p1->next=NULL;/*将新节点的指针置为空*/
while(p1->num>0)/*输入节点的数值大于0*/
{
//④将新节点的指针成员赋值为空。若是空表,将新节点连接到表头;若是非空表,将新节点接到表尾;
if(head==NULL)
head=p1;/*空表,接入表头*/
else
p2->next=p1;/*非空表,接到表尾*/
p2=p1;
p1=(structnode*)malloc(sizeof(structnode));/*下一个新节点*/
i=i+1;
printf("请输入值,值小于等于0结束,值存放地址为:p%d_ADDR=%d ",i,p2);
scanf("%d",&p1->num);/*输入节点的值*/
//⑤判断一下是否有后续节点要接入链表,若有转到3),否则结束;
}
//==============原来程序更正部分:(多谢@daling_datou提醒)================================
free(p1);//申请到的没录入,所以释放掉
p1=NULL;//使指向空
p2->next=NULL;//到表尾了,指向空
printf("链表输入结束(END) ");
//==============================================
returnhead;/*返回链表的头指针*/
}
/*******************************************/
voidprint(structnode*head)/*出以head为头的链表各节点的值*/
{
structnode*temp;
temp=head;/*取得链表的头指针*/
printf(" 链表存入的值为: ");
while(temp!=NULL)/*只要是非空表*/
{
printf("%6d ",temp->num);/*输出链表节点的值*/
temp=temp->next;/*跟踪链表增长*/
}
printf("链表打印结束!!");
}
B. 有没有数据结构(c语言版)严蔚敏老师教学视频下载地址
哥们儿 你可以在网络里收索 《数据结构C语言版》 严蔚敏老师40多个课时的视频都在
C. 谁有谭浩强《C语言程序设计》第四版的教学视频麻烦了
谭浩强【004】
链接:
若资源有问题欢迎追问~
D. C语言 链表操作
#include <stdio.h>
#include <malloc.h>
int m;
struct Node
{
int data;
struct Node *next;
}* listA, *listB;
//打印AList列芦含枣表
//参数AList为显示的列表
void printList(struct Node *AList)
{
struct Node *post;
post = AList->next;
while (post)
{
printf("%d ",post->data);
post = post->next;
}
printf("\n");
}
//初始化表头,列表含有表头
//参数AList为初始化的列表
void init(struct Node **AList)
{
*AList = (struct Node*)malloc(sizeof(struct Node));
(*AList)->data = 0;
(*AList)->next = 0;
}
//创建列表
//参数AList为要创建的列表
void ReadList(struct Node **AList)
{
int i;
struct Node *post;
struct Node *pre;
// 输入列表长度
printf("请输入列表的长度为:"); scanf("%d",&m);
//读取列表A
pre = *AList;
for(i=1; i<=m; i++)
{
post = (struct Node*)malloc(sizeof(struct Node));
printf("第%d个节点值为:"老磨, i);
scanf("%d", &post->data);
post->next = 0;
pre->next = post;
pre = post;
}
}
//插入节点
//参数AList为要插入的列表
//参数Aindex为要插入的节点的位置
void InsertNode(struct Node **AList, int Aindex = -1)
{
int i;
struct Node *pre, *post;
pre = *AList;
//判断插入位置,默认为头部插入
if((Aindex>0) && (Aindex<=m))
{
post = (struct Node*)malloc(sizeof(struct Node));
printf("请输入要插入的节点值为:");
printf("listN%d = ", Aindex);
scanf("%d", &post->data);
post->next = 0;
//寻找插入点
i = 1;
pre = *AList;
while ( i<Aindex )
{
pre = pre->next;
i = i + 1;
}
//插入节点
post->next = pre->next;
pre->next = post;
}
else //插入到头部
{
post = (struct Node*)malloc(sizeof(struct Node));
printf("listA1 = ");
scanf("%d", &post->data);
post->next = 0;
//插入节点
post->next = pre->next;
pre->陪拆next = post;
}
m = m + 1;
}
//删除节点
//参数AList为要删除的列表
//参数Aindex为要删除的节点的位置
void DeleteNode(struct Node **AList, int Aindex)
{
int i;
struct Node *pre, *post;
pre = *AList;
//判断删除位置
if((Aindex>0) && (Aindex<=m))
{
//寻找删除点
i = 1;
while ( i<Aindex )
{
pre = pre->next;
i = i + 1;
}
//赋值要删除节点
post = pre->next;
pre->next = post->next;
delete post;
m = m - 1;
}
else //输入越界
{
printf("不存在此节点");
}
}
//列表存盘
//参数AList为要存盘的列表
int WriteFile(struct Node *AList)
{
struct Node *post;
FILE *fd;
fd = fopen("data.txt", "wt");
if (fd == NULL)
{
printf("File open error");
return 0;
}
post = AList->next;
while (post)
{
fprintf(fd, "%d ", post->data);
post = post->next;
}
fclose(fd);
return 1;
}
//使用文件创建列表
//参数AList为要创建的列表
int ReadFile(struct Node **AList)
{
struct Node *pre, *post;
FILE *fd;
fd = fopen("data.txt", "rb");
if (fd == NULL)
{
printf("File open error");
return 0;
}
//读取列表
pre = *AList;
while (!feof(fd))
{
post = (struct Node*)malloc(sizeof(struct Node));
fscanf(fd, "%d ", &post->data);
post->next = 0;
pre->next = post;
pre = post;
}
fclose(fd);
return 1;
}
void main(void)
{
//listA使用输入创建
//listB使用读取文件创建
init(&listA);
init(&listB);
ReadList(&listA);
printf("输入的列表为:");
printList(listA);
InsertNode(&listA, 1);
printf("插入第一个节点后的列表为:");
printList(listA);
DeleteNode(&listA, 2);
printf("删除第二个节点后的列表为:");
printList(listA);
WriteFile(listA);
ReadFile(&listB);
printf("使用文件创建的列表为:");
printList(listB);
}
以上程序可以直接运行,且结果正确。
主函数可以再做的复杂一些和友好一些。删除节点的位置和插入节点的位置可以作为输入之类的。
希望对你有所帮助
E. c语言中的链表:
##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); //打印显示链表的信息
}
}
F. C语言中怎样建立链表
参考以前写的这个吧,写的不好,你修改吧
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#define CD sizeof(struct Biao)
struct Biao
{
int num;
struct Biao *next;
};
int m,x;
int main()
{
struct Biao *jianli();
void paixu(struct Biao *n);
void chashu(struct Biao *n);
void shanshu(struct Biao *n);
void qiupingjun(struct Biao *n);
void tuichu();
int n;
struct Biao *t;
printf("1.建立链表\n2.排序\n3.插数\n4.删数\n5.求平均值\n");
printf("请输入选项:\n");
for(;;)
{
scanf("%d",&n);
switch(n)
{
case 1:t=jianli();break;
case 2:paixu(t);break;
case 3:chashu(t);break;
case 4:shanshu(t);break;
case 5:qiupingjun(t);break;
case 6:tuichu();break;
default:printf("输入错误!请重新输入\n");
}
}
}
struct Biao *jianli()
{
int i,j;
printf("建立链表,请输入数据:\n");
struct Biao *head,*p1,*p2;
p1=p2=(struct Biao * )malloc(CD);//if(p1==NULL)建立链表失败
for(i=0;i<=100;i++)
{
if(i==0)
{
head=p1;
p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;
continue;
}
if(p1==NULL)
break;
else
{
scanf("%d",&p1->num);
if(p1->num==-1)
{
p2->num=NULL;
break;
}
p2->next=p1=(struct Biao * )malloc(CD);
p2=p1;
}
}
if(head->next->num==NULL)
printf("您建立了一个空链表\n");
else
{
printf("共有数据:%d\n",i-1);
m=i-1;
printf("输出链表:\n");
p1=head->next;
for(j=0;j<i-1;j++)
{
printf("%d ",p1->num);
p1=p1->next;
}
}
printf("\n");
return(head);
}
void paixu(struct Biao *n)
{
int i,j,a,temp;
a=m;
struct Biao *p1,*p2,*tp;
p2=p1=n->next;
printf("从小到大排序结果:\n");
for(i=0;i<a-1;i++)
{
p1=p2;
tp=p1;
for(j=i+1;j<a;j++)
{
if((p1->next->num)<(tp->num))
{
tp=p1->next;
}
p1=p1->next;
}
temp=tp->num;tp->num=p2->num;p2->num=temp;
p2=p2->next;
}
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
}
void chashu(struct Biao *n)
{
int a,i,j;
struct Biao *p1,*p2,*tp;
m=m+1;
x=m;
a=m;
p1=n;
printf("请插入数字:\n");
p2=(struct Biao *)malloc(CD);
scanf("%d",&p2->num);
p1=n->next;
for(i=0;i<a-1;i++)
{
if(p1->num<=p2->num&&p1->next->num>p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
break;
}
if(p1->num<p2->num&&p1->next->num>p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
break;
}
if(n->next->num>p2->num)
{
tp=n->next;
n->next=p2;
p2->next=tp;
break;
}
p1=p1->next;
}
p1=n;//
for(i=0;i<a-1;i++)
{
p1=p1->next;
}
if(p1->num<=p2->num)
{
tp=p1->next;
p1->next=p2;
p2->next=tp;
}//算法不简便
printf("插入后的数据:\n");
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
printf("数据个数:%d\n",a);
}
void shanshu(struct Biao *n)
{
int a,i,j;
a=x;
struct Biao *p1,*p2;
printf("请输入要删除的数:\n");
scanf("%d",&j);
for(;;)
{
p1=n;
for(i=0;i<a;i++)
{
if(p1->next->num==j)
{
p2=p1->next;
p1->next=p1->next->next;
a-=1;
x-=1;
break;
}
p1=p1->next;
}
if(i==a)
break;
}
printf("结果:\n");
p1=n->next;
for(i=0;i<a;i++)
{
printf("%d ",p1->num);
p1=p1->next;
}
printf("\n");
printf("剩余数据个数:%d\n",x);
}
void qiupingjun(struct Biao *n)
{
int s,i;
struct Biao *p1;
s=0;
p1=n->next;
for(i=0;i<x;i++)
{
s+=p1->num;
p1=p1->next;
}
printf("平均值为:%f\n",s*1.0/x);
}
void tuichu()
{
exit(0);
}
G. C语言中链表的原理该如何使用坐等指教 多谢啦
一楼的答案很标准了,我再通俗地说明下-----你把链表看成一辆火车,火车头就是头结点,然后每节车厢有号码标识,比如1号车厢车尾就连上2号车厢车头,这就像1号节点的指针域里面存储的是下一个节点的地址,每节车厢里面容纳乘客的地方就相当于数据域。
至于链表的种类有很多,有单链表的,双连表的,循环链表的,还有有头结点和无头节点的。总之原理都大同小异。
对于链表的操作主要有插入和删除:
插入:在第i个节点之前插入一个节点:
1、首先查找第i-1个节点 if(p满足条件)
2、给新节点分配空间q =(Node*)malloc(sizeof(Node));给q数据域赋值。
3、q->next = p->next;
4、p->next = q;
删除:如果要删除符合条件的节点(删除q所指节点)
1、首先找到要删除节点的前驱节点 if(p->next->data满足条件)
2、用q指向要删除的节点 q = p->next;
3、p->next = q->next;
4、free(q);
说再多不如你实际操作,你可以写一个很简单的链表数据试试...
希望对你有所帮助!
H. c语言链表高手帮下忙 谢(请用通俗的话)
书上的意思是这样的:你新建立了一个结点,其首址为s,(如s=(LinkList)malloc(sizeof(Node))//这句意思是:开辟新结点,并将该节点地址赋给s,也就是s指向该新结点).。
你要将这个新结点插入到p所指结点的后面,那么:1,s的next指针和p原先的next指针所指向的结点(也就是原先p所指结点的后一个结点)拉手(s->next=p->next;)2.p的next指针和s拉手(p->next=s;)。然后结点就插入了链表。
s是首址,指向你刚刚开辟的新结点(你要在链表中插入,肯定得先开辟一个新结点,链表插入删除之类操作处理的单位就是结点。每个结点包含指针域,数据域,数据域是用来裂含放数据的,和指针没关系,指针域是用来存放next指针,指向该节点的下一个结点,这样个个仔源档节点之间都有链接)。
不知道我讲的通不通俗,希望有帮助念乱