单链表的创建删除输出操作
【问题描述】输入一整数序列,采用单链表进行存储,按照非递减的方式输出该序列的所有值,每输出一个值,删除其对应的结点,同时输出链表的长度。【输入形式】整数序列,以空格作为分隔,序列最多包含10个整数
【输出形式】整数序列,以空格作为分隔,
【样例输入】
10 90 80 20 40
【样例输出】
10 4 20 3 40 2 80 1 90 0
【样例说明】
【评分标准】
#include<stdio.h> #include<stdlib.h> #include<string.h> #define LEN sizeof(linklist) struct student { int num; struct student* next; }; typedef struct student linklist; int n; linklist* creat() { int count = 0; linklist* head, * end, * node; head = (linklist*)malloc(LEN); end = head; while (1) { node = (linklist*)malloc(LEN); scanf("%d", &node->num); end->next = node; end = node; count++; if (getchar() == '\n') { break; } } n = count; end->next = NULL; return head; } void print(linklist* head) { if (head == NULL) { return; } linklist* p; p= (linklist*)malloc(LEN); p = head; int a[100] = { 0 }, i = 0; while (p->next != NULL) { p = p->next; a[i] = p->num; i++; } int j; int t; int u = n; int k; for (i = 0; i < n-1; i++)//控制循环的次数 { k = i; for (j = i + 1; j < n; j++) { if (a[k] > a[j]) { k = j; } } if (i != k) { t = a[i]; a[i] = a[k]; a[k] = t; } } for (i = 0; i < u; i++,n--) { if (n < 0) { break; } printf("%d %d ",a[i],n-1); } } int main() { linklist* pt; pt = creat(); print(pt); return 0; }
#include<stdio.h> #include<stdlib.h> struct node { int a; struct node* next; }; struct node* creat(int* i) { int a = 0; int r; struct node* head, * p1, * p2; head = NULL; p1 = p2 = (struct node*)malloc(sizeof(struct node)); while (1) { r = scanf("%4d", &p1->a); if (r == EOF) { break; } a++; if (head == NULL) { head = p1; } p2->next = p1; p2 = p1; p1 = (struct node*)malloc(sizeof(struct node)); } *i = a; p2->next = NULL; return(head); } struct node* paixu(struct node* head, int i) { int j, k, t; struct node* p1, * p2; for (j = 0; j < i - 1; j++) { p1 = head; p2 = head->next; for (k = 0; k < i - 1 - j; k++) { if (p1->a > p2->a) { t = p1->a; p1->a = p2->a; p2->a = t; } p1 = p2; p2 = p2->next; } } return(head); } struct node* Delete(struct node* head) { struct node* p1; p1 = head; head = p1->next; free(p1); return(head); } int main() { int i; struct node* head, * p; head = creat(&i); head = paixu(head, i); for (;;) { i--; p = head; printf("%d %d ", p->a, i); head = Delete(head); if (i == 0) { break; } } printf("\n"); return 0; }