資料結構作業-3

來源:互聯網
上載者:User

題目:編寫演算法程式將非遞減有序的單鏈表中值相同的多餘結點刪除。設線性表為順序儲存結構。

1.需求分析

將非遞減有序的單鏈表中值相同的多餘結點刪除,設線性表為順序儲存結構。

當使用者輸入的數組的關鍵碼為非遞減有序時,重複資料刪除節點;當使用者輸入的數組的關鍵碼並不是有序時,進行排序,然後重複資料刪除的節點。

本程式處理int型和char型的數組。

 

2.概要設計

 

   
總體流程如下如:

 

 

ADT定義:

struct
_Node{

    struct
_Node * next;

    DataType
data;

};

   
主要的函數:

int
append(DataType
_data);//追加到最後

int
delete_current(Node *
nd);//刪除當前節點

int
delete_next(Node *
nd);//刪除下一個節點

Node *
get_next(Node *
nd);//返回下一個節點

Node *
get_list();//產生list

 

3.詳細設計

流程圖為:

 

其中直接插入排序演算法的流程圖如下:

 

4.調試分析

當使用者輸入的數組的長度超過最大值N時,程式發生錯誤。

當DataType為char型時,不能處理多於一位元的整數,如下:

 

數組長度:9

數組元素:1 9 89 3 2 67 7890 90

處理結果:


2  3  6 
7  8  9

 

5.使用說明

   
運行程式,輸入int型或char型數組,斷行符號即出現結果。

 

6.測試結果

測試組一int型

數組長度:5

數組元素:12 0 989 34 0

處理結果:


12  34  98

測試組二char型

數組長度:7

數組元素: 9 4 3 2 0 8 3

處理結果:


2  3  4 
8  9

測試組三char

數組長度:6

數組元素:1 q 3 e 3 w

處理結果:


3  e  q 
w

 

7.附錄

來源程式檔案清單。

 List.h<br />struct _Node{<br />struct _Node * next;<br />DataType data;<br />};<br />typedef _Node Node;<br />int append(DataType _data);//追加到最後<br />int delete_current(Node * nd);//刪除當前節點<br />int delete_next(Node * nd);//刪除下一個節點<br />Node * get_next(Node * nd);//返回下一個節點<br />Node * get_list();//產生list<br />List.cpp<br />#include "StdAfx.h"<br />#include "list.h"<br />#include <string.h><br />#include <stdlib.h></p><p>static Node * list = NULL;<br />int append(DataType _data)<br />{<br />if(list == NULL)<br />{<br />list = (Node*)malloc(sizeof(Node));<br />list->data = _data;<br />list->next = NULL;<br />return 0;<br />}<br />Node * current = list;<br />while(current->next)<br />{<br />current = current->next;<br />}<br />current->next = (Node*)malloc(sizeof(Node));<br />current = current->next;</p><p>current->data = _data;<br />current->next = NULL;<br />return 0;<br />}</p><p>int delete_current(Node * nd)<br />{<br />if(nd == NULL)<br />return -1;</p><p>Node * before = list;<br />while(before->next != nd)<br />{<br />before = before->next;<br />}<br />before->next = nd->next;<br />free(nd);<br />return 0;<br />}</p><p>int delete_next(Node * nd)<br />{<br />if(nd == NULL)<br />return -1;<br />if(nd->next == NULL)<br />return -1;<br />Node * tmp = nd->next;<br />nd->next = nd->next->next;<br />free(tmp);<br />return 0;<br />}</p><p>Node * get_next(Node * nd)<br />{<br />if(nd)<br />return nd->next;<br />return NULL;<br />}</p><p>Node * get_list()<br />{<br />return list;<br />}<br />Main.cpp<br />// New3.cpp : Defines the entry point for the console application.<br />//</p><p>#include "stdafx.h"<br />#include <iostream><br />#include "list.h"<br />using namespace std;<br />#define N 10</p><p>//直接插入排序<br />void InsertSort(DataType array[],int n)<br />{<br />//cout<<"直接插入排序"<<endl;<br />int i,j;<br />DataType temp;<br />for ( i=0;i<n-1;i++)<br />{<br />temp=array[i+1];<br />j=i;<br />while(j>=0 &&temp<array[j])<br />{<br />array[j+1]=array[j];<br />j--;<br />}<br />array[j+1]=temp;<br />}<br />}</p><p>int main(int argc, char* argv[])<br />{<br />cout<<"本程式完成排序,並去除重複元素"<<endl<<endl;<br />// append(1);<br />// append(2);<br />// append(3);<br />// append(4);<br />// append(5);<br />// append(6);<br />// append(7);<br />// append(8);<br />// append(8);<br />// append(9);<br />// append(9);<br />// append(10);</p><p>DataType A[N];<br />int i,n;<br />cout<<"數組長度:";<br />cin>>n;<br />cout<<"數組元素:";<br />for (i=0;i<n;i++)<br />{<br />cin>>A[i];<br />}<br />InsertSort(A,n);</p><p>for (i=0;i<n;i++)<br />{<br />append(A[i]);<br />}</p><p>cout<<"處理結果:"<<endl;<br />Node * node = get_list();</p><p>while(node)<br />{<br />if(node->next)<br />{<br />if(node->data == node->next->data)<br />{<br />delete_next(node);<br />}<br />}<br />printf("%d ",node->data);<br />node = get_next(node);<br />}<br />cout<<endl;<br />system("pause");<br />return 0;<br />}<br />

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.