数据结构学习笔记


#include<stdio.h>
#include<stdlib.h>
typedef int DataType ;
typedef struct LNode{
DataType data;
LNode *next;
}LNode;
void qingxuanze(){
printf("1头插法,2尾插法,3删除,4插入,5查找 ,0退出请输入:\n");
}
LNode *toucha(){
LNode *head;
LNode *p;//定义类型为表格指针的表格块
LNode *s;//这个用来做标记用
int c;//接受输入的数字
head=(LNode*)malloc(sizeof(LNode));//给表格头分配一个内存空间
p=head;//标记位置等于表格头
p->next=NULL;//
printf("头插法请输入数字 等于0结束\n");
scanf("%d",&c);
while(c!=0){
s=(LNode*)malloc(sizeof(LNode));//给一个循环里的表格块分配内存空间
s->data=c; //把表格块赋值
s->next=p->next;//头插法的重点
p->next=s;//头插法的重点
scanf("%d",&c);
}
printf("输入完成\n");
return head;
}
LNode *weicha(){
LNode *head;
LNode *p;//定义类型为表格指针的表格块
LNode *s;//这个用来做标记用
int c;//接受输入的数字
head=(LNode*)malloc(sizeof(LNode));//给表格头分配一个内存空间
p=head;//标记位置等于表格头
printf("尾插法输入数字 等于0结束\n");
scanf("%d",&c);
while(c!=0){
s=(LNode*)malloc(sizeof(LNode));//给一个循环里的表格块分配内存空间
s->data=c; //把表格块赋值
p->next=s;//头插法的重点
p=p->next;//头插法的重点
scanf("%d",&c);
}
p->next=NULL;//
printf("输入完成\n");
return head;
}
void shuchu(LNode *head){
printf("当前单链表的数组为");
for(head=head->next;head;head=head->next)
printf("%d ",head->data);
printf("\n");
}
void charu(LNode *head,int a,int weizhi){
int c=weizhi;
int i;
int zuida;
LNode *s;
LNode *p;
p=head;
for(zuida=0;head;head=head->next){zuida++;}

if(zuida>=weizhi){
s=(LNode*)malloc(sizeof(LNode));
s->data=a;
for(i=1;i<c;i++)
p=p->next;
s->next=p->next;
p->next=s;}
else
printf("输入的位置大于当前数组成员量,");
}
void shanchu(LNode *head,int weizhi){
LNode *p;
int i;
LNode *k;
int zuida;
p=head;
k=head;
for(zuida=0;k;k=k->next){zuida++;}
if(zuida>=weizhi){
for(i=1;i<weizhi;i++)
head=head->next;
p=head->next;
head->next=head->next->next;}
else
printf("输入的位置大于当前数组成员量,");
}

int chazhao(LNode *head,int shuzi){
int a=0;
for(;head;head=head->next){
if(head->data==shuzi){
return a;
}
a++;
}
return 0;
}

void main(){
LNode *head;
int weizhi;
int zhi;
int xuanze;
int chuanhuizhi;
printf("请选择:1头插法,2尾插法 来创建链表");
scanf("%d",&xuanze);
while(xuanze!=0){
if(xuanze!=1&&xuanze!=2&&xuanze!=3&&xuanze!=4&&xuanze!=5){
printf("输入错误 请重新输入\n");
qingxuanze();
scanf("%d",&xuanze);
}
if(xuanze==1){
head=toucha();
shuchu(head);
qingxuanze();
scanf("%d",&xuanze);
}
if(xuanze==2){
head=weicha();
shuchu(head);
qingxuanze();
scanf("%d",&xuanze);
}
if(xuanze==3){
printf("请输入删除位置:\n");
scanf("%d",&weizhi);
shanchu(head,weizhi);
shuchu(head);
qingxuanze();
scanf("%d",&xuanze);
}
if(xuanze==4){
printf("请输入插入的数字:\n");
scanf("%d",&zhi);
printf("请输入插入的位置:\n");
scanf("%d",&weizhi);
charu(head,zhi,weizhi);
shuchu(head);
qingxuanze();
scanf("%d",&xuanze);
}
if(xuanze==5){
printf("请输入查找的值:\n");
scanf("%d",&zhi);
chuanhuizhi=chazhao(head,zhi);
if(chuanhuizhi==0)
printf("%d不在当前单链表\n",zhi);
else
printf("%d的所在位置为%d\n",zhi,chuanhuizhi);
qingxuanze();
scanf("%d",&xuanze);
}

}
}

posted on 2016-03-30 20:44  Jor1  阅读(182)  评论(0编辑  收藏  举报