从键盘上输入一批非零整数,采用带头结点的单链表存放, 将单链表中的所有正数放在链表的前面,所有负数放在后面, 并将单链表输出
从键盘上输入一批非零整数,采用带头结点的单链表存放,将单链表中的所有正数放在链表的前面,所有负数放在后面,
并将单链表输出
struct _node { long data; _node *next; }; bool InsertList(_node **_head,_node **_tail,long _data) { if(_head == NULL || _tail == NULL) return false; _node *p; p = (_node*)malloc(sizeof(_node)); p->data = _data; if(_data > 0) { p->next = *_head; *_head = p; } else { p = (_node*)malloc(sizeof(_node)); p->data = _data; (*_tail)->next = p; p->next = NULL; *_tail = p; } return true; } int main(int argc, char* argv[]) { _node *head,*tail,*p,*temp; long int data; head = (_node*)malloc(sizeof(_node)); head->next = NULL; printf("please input the first node:"); scanf("%d",&head->data); tail = head; do { printf("please insert a node:"); scanf("%d",&data); if(data) { InsertList(&head,&tail,data); } } while (data); p = head; while(p != NULL) { printf("%d\t",p->data); temp = p; p = p->next; free(temp); } return 0; }