#include
#include
typedef struct node
{
int num;
struct node *next;
}lnode;
lnode *creat()
{
lnode *head,*p,*q;
int n;
head=NULL;
printf("输入要创建的节点数\n");
scanf("%d",&n);
while(n)
{
p=(lnode *)malloc(sizeof(lnode));
printf("输入数据\n");
scanf("%d",&p->num);
if (head==NULL)
{
head=q=p;
}
else
{
q->next=p;
q=p;
}
n--;
}
q->next=NULL;
return head;
}
lnode *insert(lnode *head)
{
lnode *p,*q,*s;
int n;
char ch;
q=p=head;
printf("输入插入的位置\n");
scanf("%d",&n);
printf("请选择是插入在前还是在后(F or B)\n");
getchar();
ch=getchar();
if(ch=='F'||ch=='f')
{
s=(lnode *)malloc(sizeof(lnode));
printf("请输入数据\n");
scanf("%d",&s->num);
while(p&&--n)
{
q=p;
p=p->next;
}
if (q==p)
{
s->next=q;
return s;
}
else
{
q->next=s;
s->next=p;
return head;
}
}
else if (ch=='B'||ch=='b')
{
s=(lnode *)malloc(sizeof(lnode));
printf("请输入数据\n");
scanf("%d",&s->num);
while(p&&n--)
{
q=p;
p=p->next;
}
if (NULL==q->next)
{
q->next=s;
s->next=NULL;
return head;
}
else
{
q->next=s;
s->next=p;
return head;
}
}
else
{
printf("输入错误\n");
}
}
lnode *del(lnode *head)
{
lnode *p,*q;
int n;
int flag=0;
p=q=head;
printf("请输入删除的数据\n");
scanf("%d",&n);
while(p)
{
if (p->num==n)
{
flag=1;
if (head==p)
{
head=head->next;
}
else if(NULL==p->next)
{
q->next=NULL;
}
else
{
q->next=p->next;
}
}
q=p;
p=p->next;
}
if (flag==0)
{
printf("没有找到数据\n");
system("pause");
}
else
{
printf("删除成功\n");
system("pause");
}
return head;
}
lnode *sort(lnode *head)
{
lnode *t,*f,*min,*p_min,*s;
char ch;
f=NULL;
printf("请输入排序方式:升序(A/a),降序(D/d)\n");
getchar();
ch=getchar();
if (ch=='A'||ch=='a')
{
while(NULL!=head)
{
for (min=head,s=head;s->next!=NULL;s=s->next)
{
if (min->num>s->next->num)
{
p_min=s;
min=s->next;
}
}
if (NULL==f)
{
f=min;
t=min;
}
else
{
t->next=min;
t=min;
}
if (min==head)
{
head=head->next;
}
else
{
p_min->next=min->next;
}
}
if (f!=NULL)
{
t->next=NULL;
}
printf("排序完成\n");
system("pause");
head=f;
return f;
}
else if (ch=='D'||ch=='d')
{
while(NULL!=head)
{
for (min=head,s=head;s->next!=NULL;s=s->next)
{
if (min->numnext->num)
{
p_min=s;
min=s->next;
}
}
if (NULL==f)
{
f=min;
t=min;
}
else
{
t->next=min;
t=min;
}
if (min==head)
{
head=head->next;
}
else
{
p_min->next=min->next;
}
}
if (f!=NULL)
{
t->next=NULL;
}
printf("排序完成\n");
system("pause");
head=f;
return f;
}
}
void dispaly(lnode *head)
{
lnode *p;
p=head;
printf("\n");
while(p!=NULL)
{
printf("%d\t",p->num);
p=p->next;
}
}
int getoption()
{
int n;
printf("0 退出\n");
printf("1 创建链表\n");
printf("2 插入节点\n");
printf("3 删除节点\n");
printf("4 排序节点\n");
printf("5 显示链表\n");
printf("请选择操作\t");
scanf("%d",&n);
return n;
}
int main()
{
lnode *temp;
char ch;
int o;
do
{
system("cls");
o=getoption();
switch (o)
{
case 0:
exit(0);
break;
case 1 :
system("cls");
temp=creat();
break;
case 2:
system("cls");
temp=insert(temp);
break;
case 3:
system("cls");
temp=del(temp);
break;
case 4:
system("cls");
temp=sort(temp);
break;
case 5:
system("cls");
dispaly(temp);
system("pause");
break;
}
system("cls");
printf("按0退出,任意键继续\t");
ch=getchar();
if (ch=='0')
{
exit(0);
}
} while (ch!='0');
}