C语言处理双向链表的排序问题的代码

2021-10-06  本文已影响0人  jiangmm

把开发过程中经常用的一些代码备份一次,如下代码段是关于C语言处理双向链表的排序问题的代码,应该是对大家有一些好处。

#include <stdio.h>

#include <stdlib.h>

typedef struct node{

int data;

} Node;

int get_int(void);

int

main()

{

head->pre = NULL;

head->next = get_node();

head->next->pre = head;

printf("please enter the number 'q' to quit:");

while (1)

{

p = get_node();

p->data = get_int();

if (p->data ==0)

break;

insert(head,p);

}

while (head->next!=NULL)

{

printf("%d ",head->next->data);

head->next = head->next->next;

}

return 0;

}

int

get_int(void)

{

int input;

char ch;

while (scanf("%d",&input)!=1)

{

while((ch=getchar())!='n')

putchar(input);

printf(" is not an integer.nPlease enter an integer value,such as 25,-178,or 3;n");

}

return input;

}

void

{

if (p->next->data == 0)

{

p->next->data = new_node->data;

return;

}

while (1)

{

if (scan->data < new_node->data)

{

if (scan->next != NULL)

scan = scan->next;

else

{

scan->next = new_node;

new_node->pre = scan;

break;

}

}

else

{

new_node->pre = scan->pre;

new_node->next = scan;

scan->pre->next = new_node;

scan->pre = new_node;

break;

}

}

}

get_node(void)

{

new_node->next = NULL;

new_node->pre = NULL;

new_node->data = 0;

return new_node;

}

                               

                       

               

               

           

           

               

上一篇 下一篇

猜你喜欢

热点阅读