0
点赞
收藏
分享

微信扫一扫

数据结构-单链表

GG_lyf 2022-03-10 阅读 60
链表c++
#include<stdio.h>
typedef struct Node{
int data;
struct Node*next;
}NODE;
//链表的结构
struct Node*p=h;
printf("输出链表");
if(p==NULL) printf("链表为空\n");
void dis(struct Node *h) {
else{
while(p->next!=NULL){
printf("%4d",p->data);
p=p->next;
}
printf("%4d",p->data);
}
}
//在链表中间插入一个元素
//假如被插入位置的后方特征是数据项为x个整数,0代表结束");
scanf("%d",
else{
q=h;
while(q->next->data!=x) {
q=q->next;
}
p-next=q->next;
q-next=p;
//创建n个元素链表(有单独头结点的链表,先建立头结点)
struct Node*create(){
struct Node*head,*p,*q;
int n;
head=(struct Node*)malloc(size of(struct Node));
head->next=NULL;//第一阶段,建立一个头节点,并指向NULL;
p=head;
while(1){
printf("请输入一
q=(struct Node*)malloc(sizeof(struct Node));
if(n<=0) break;
q->data=n;//产生要插入的节点
p->next=q;//执行插入过程,第一次插入的时候p=head ,所以就是head->next=q;
q->next=NULL;
p=q;

}
return head;
}
//
//
//
//链表的节点删除
//先创建一个链表
typedef struct student{
int score;
struct student *next;
}linklist; //一般创建链表用typedef struct ,因为这样定义结构体变量时,我们就可以直接用linklist*a,定义结构体变量了;
//再初始化一个链表,n为节点个数
linklist *creat(int n){
linklist *head,*node,*end;
head=(linklist*)malloc(sizeof(linklist));//分配地址
end=head;
for(int i=0;i<n;i++){
node= (linklist*)malloc(sizeof(linklist)) ;
scanf("
%d",&node->score);
end=node;
}
end->next=NULL;
return head;
}
//删除链表节点值
void delet(linklist *list,int n){
linklist *t=list,*in;
int i=0;
while(i<nNULL){
in=t;
t=t->next;
i++;
}
if(t!=NULL){
in->next=t->next;//把前指针越过,要删除的节点指向下下个节点;
free(t);//释放删除节点的空间;
}
else{
puts("
节点不存在");
}
}
//修改链表节点值
void change(linklist *list,int n){
linklist *t=list;
int i=0;
while(i<nNULL){
t=t->next;
i++;
}
if(t!=NULL){
puts("
请输入要修改的值:");
scanf("
%d",
}
else{
puts("
节点不存在");
}


}
//插入节点
void insert(linklist *list,int n){
linklist *t=list ,*in;
int i=0;
while(i<nNULL){
t=t->next;
i++;
}
if(t!=NULL){
in=(linklist*)malloc(sizeof(linklist));
puts("
请输入要插入的值");
scanf("
%d",&in->score) ;
in->next=t->next;
t->next=in;
}
else{
puts("
节点不存在");
}
}
//输出链表
while(h->next!=NULL) {
h=h->next;
printf("
%d ",h->score);
}

举报

相关推荐

0 条评论