3324順序表應用1:多餘元素刪除之移位演算法
阿新 • • 發佈:2018-12-15
#include <stdio.h> #include <stdlib.h> struct node { int a; struct node *next; }; int main() { struct node *p, *q, *head, *tail; int n, i, m, k, b; scanf("%d", &m); while(m--)//建立連結串列,並輸入第一個值 { scanf("%d", &n); head = (struct node *)malloc(sizeof(struct node)); head->next = NULL; q = head; p = (struct node *)malloc(sizeof(struct node)); scanf("%d", &p->a); p->next = NULL; q->next = p; q = p; n--; while(n--)//輸入值,判斷是否重複 { int t = 1; tail = head; p = (struct node *)malloc(sizeof(struct node)); scanf("%d", &k); while(tail->next!=NULL) { tail = tail->next; if(tail->a == k)//重複退出 { t = 0; break; } } if(t)//不重複指向下一個 { p->a = k; p->next = NULL; q->next = p; q = p; } } p = head; while(p->next!=NULL)//輸出連結串列 { p = p->next; printf("%d ", p->a); } printf("\n"); } }