C 演算法4 鍊錶去重

2021-08-22 14:57:02 字數 3126 閱讀 3427

1.給定排序的鍊錶,刪除重複元素,只保留重複元素第一次出現的結點。

(1)**:

# include 

using

namespace

std;

//定義節點結構

typedef

struct snode

}node;

//宣告函式

void outlink(node *phead);

void destroy(node *p);

void delsame(node *head);

//輸出鍊錶

void outlink(node *phead)

node *pcur = phead->pnext;

while (pcur != null)

cout

<< endl;

}//刪除指標

void destroy(node *p)

}//刪除重複

void delsame(node *head)

else

}}//主函式

int main()

; int size = sizeof(array) / sizeof(int);

for (int i = 0; i < size; i++)

cout

<< "刪除之前"

<< endl;

outlink(head);

delsame(head);

cout

<< "刪除之後"

<< endl;

outlink(head);

destroy(head);

return

0;}

(2)結果:

刪除之前

->1->2->2->3->4->4->4->5->6->6->6->6->7

刪除之後

->1->2->3->4->5->6->7

請按任意鍵繼續. . .

2.給定排序的鍊錶,刪除重複元素,只保留重複元素最後一次出現的結點。

(1)**:

# include 

using

namespace

std;

//定義節點結構

typedef

struct snode

}node;

//宣告函式

void outlink(node *phead);

void destroy(node *p);

void delsame(node *head);

//輸出鍊錶

void outlink(node *phead)

node *pcur = phead->pnext;

while (pcur != null)

cout

<< endl;

}//刪除指標

void destroy(node *p)

}//刪除重複

void delsame(node *head)

else

now = pre->pnext;

}}//主函式

int main()

; int size = sizeof(array) / sizeof(int);

for (int i = 0; i < size-1; i++)

cout

<< "刪除之前"

<< endl;

outlink(head);

delsame(head);

cout

<< "刪除之後"

<< endl;

outlink(head);

destroy(head);

return

0;}

(2)結果:

刪除之前

->1->2->2->3->4->4->4->5->6->6->6->6->7

刪除之後

->1->2->3->4->5->6->7

請按任意鍵繼續. . .

3.給定排序的鍊錶,刪除所有重複元素。

(1)**:

# include 

using

namespace

std;

//定義節點結構

typedef

struct snode

}node;

//宣告函式

void outlink(node *phead);

void destroy(node *p);

void delsame(node *head);

//輸出鍊錶

void outlink(node *phead)

node *pcur = phead->pnext;

while (pcur != null)

cout

<< endl;

}//刪除指標

void destroy(node *p)

}//刪除重複

void delsame(node *head)

if (delnum)

else

now = next;

}}//主函式

int main()

; int size = sizeof(array) / sizeof(int);

for (int i = 0; i < size - 1; i++)

cout

<< "刪除之前"

<< endl;

outlink(head);

delsame(head);

cout

<< "刪除之後"

<< endl;

outlink(head);

destroy(head);

return

0;}

(2)結果:

刪除之前

->1->2->2->3->4->4->4->5->6->6->6->6->7

刪除之後

->1->3->5->7

請按任意鍵繼續. . .

PTA 鍊錶去重 C語言

給定乙個帶整數鍵值的鍊錶 l,你需要把其中絕對值重複的鍵值結點刪掉。即對每個鍵值 k,只有第乙個絕對值等於 k 的結點被保留。同時,所有被刪除的結點須被儲存在另乙個鍊錶上。例如給定 l 為 21 15 15 7 15,你需要輸出去重後的鍊錶 21 15 7,還有被刪除的鍊錶 15 15。輸入格式 輸...

PAT鍊錶去重

時間限制 300 ms 記憶體限制 65536 kb 長度限制 8000 b 判題程式 standard 作者 陳越 給定乙個帶整數鍵值的單鏈表l,本題要求你編寫程式,刪除那些鍵值的絕對值有重複的結點。即對任意鍵值k,只有鍵值或其絕對值等於k的第乙個結點可以被保留。同時,所有被刪除的結點必須被儲存在...

PAT 鍊錶去重

給定乙個帶整數鍵值的鍊錶 l,你需要把其中絕對值重複的鍵值結點刪掉。即對每個鍵值 k,只有第乙個絕對值等於 k 的結點被保留。同時,所有被刪除的結點須被儲存在另乙個鍊錶上。例如給定 l 為 21 15 15 7 15,你需要輸出去重後的鍊錶 21 15 7,還有被刪除的鍊錶 15 15。輸入在第一行...