c语言Slink head=NULL,C语言中数据结构中的单向链表的问题?

#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');

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值