簡單的動態線性表

來源:互聯網
上載者:User

標籤:簡單的動態線性表

標頭檔

#ifndef __TEST_H__#define __TEST_H__#include<stdio.h>#include<string.h>#include<assert.h>#include<malloc.h>#define MAX 3   //方便測試給一個小空間typedef int datatype;typedef struct Seqlist{ datatype *_Array; size_t _size; size_t _capasize;}Seqlist;void init(Seqlist *pSeq)//初始化{ pSeq->_Array = (datatype*)malloc(sizeof(datatype)*MAX); memset(pSeq->_Array, 0, MAX); pSeq->_size = 0; pSeq->_capasize = MAX;}void capacity(Seqlist *pSeq)//空間不足開闢新空間{ datatype *tmp; tmp = (datatype*)malloc(sizeof(datatype)*(pSeq->_capasize)*2); memcpy(tmp, (pSeq->_Array),sizeof(datatype)*(pSeq->_capasize)); free(pSeq->_Array); pSeq->_Array = tmp; pSeq->_capasize = (pSeq->_capasize) * 2;}void PrintfSqelist(Seqlist *pSeq)//列印資料{ assert(pSeq); int i; for (i = 0; i<(int)(pSeq->_size); i++) {  printf("%d", (pSeq->_Array)[i]); } printf("\n");}void pushback(Seqlist *pSeq,datatype x)//添加尾部資料{ assert(pSeq); if (pSeq->_size == pSeq->_capasize) {  capacity(pSeq); } (pSeq->_Array)[pSeq->_size] = x; pSeq->_size++;}void pushfront(Seqlist *pSeq, datatype x)//添加頭部資料{ assert(pSeq); if (pSeq->_size == pSeq->_capasize) {  capacity(pSeq); } int i; for (i = (int)(pSeq->_size - 1); i >= 0; i--) {  (pSeq->_Array)[i + 1] = (pSeq->_Array)[i]; } (pSeq->_Array)[0] = x; pSeq->_size++;}void popback(Seqlist *pSeq)   //刪除尾部資料{ assert(pSeq); pSeq->_size--;}void popfront(Seqlist *pSeq)   //刪除頭部資料{ assert(pSeq); int i; for (i = 0; i<(int)(pSeq->_size - 1); i++) {  pSeq->_Array[i] = pSeq->_Array[i + 1]; } pSeq->_size--;}int findsign(Seqlist *pSeq, int x)  //尋找一個資料找到返回1,否則-1{ assert(pSeq); int i; for (i = 0; i <= (int)(pSeq->_size - 1); i++) {  if (pSeq->_Array[i] == x)  {   return 1;  } } return -1;}void erase(Seqlist *pSeq, size_t num)  //消除一個位置上面的資料{ assert(pSeq); size_t i; for (i = num; i <= (int)(pSeq->_size - 1); i++) {  pSeq->_Array[i] = pSeq->_Array[i + 1]; } pSeq->_size--;}void removesign(Seqlist *pSeq, datatype x)  //消除第一個找到的x{ assert(pSeq); int i, k; for (i = 0; i <=(int)(pSeq->_size - 1); i++) {  if (pSeq->_Array[i] == x)  {   for (k = i; k < (int)(pSeq->_size - 1); k++)   {    pSeq->_Array[k] = pSeq->_Array[k + 1];   }   pSeq->_size--;   return;  } } printf("沒有此資料");}void removeallsign(Seqlist *pSeq, datatype x)  //消除所有找到的x{ assert(pSeq); size_t start=0; size_t sign=0; int count=0; int i; for (i = 0; i < (int)pSeq->_size; i++) {  if (pSeq->_Array[i] != x)  {   pSeq->_Array[start] = pSeq->_Array[sign];   sign++;   start++;  }  else  {   count++;   sign++;  } } pSeq->_size -= count;}void Modify(Seqlist *pSeq, size_t num, datatype x) //修改某一個位置上的資料{ assert(pSeq); pSeq->_Array[num-1] = x;}#endif

主函數;

#include"test.h"int  main(){ Seqlist pSeq; init(&pSeq);       //初始化 pushback(&pSeq, 1);//尾部添加1 pushback(&pSeq, 2);//尾部添加2 pushback(&pSeq, 3);//尾部添加3 pushback(&pSeq, 3);//尾部添加3 pushback(&pSeq, 4);//尾部添加4 pushfront(&pSeq, 0);//頭部添加0 Modify(&pSeq, 3,2); //修改第三個資料為2 removesign(&pSeq, 3);//刪除第一個找到的資料3 removeallsign(&pSeq, 2);//刪除所有2 popback(&pSeq);      //尾刪除 popfront(&pSeq);     //頭刪除 PrintfSqelist(&pSeq);//列印 return 0;}

650) this.width=650;" title="QQ20151112180446.png" src="http://s3.51cto.com/wyfs02/M00/75/E8/wKiom1ZEZErDP5-mAAAE6xG293c324.png" alt="wKiom1ZEZErDP5-mAAAE6xG293c324.png" />左圖為結果。

本文出自 “痕迹” 部落格,請務必保留此出處http://wpfbcr.blog.51cto.com/10696766/1712276

簡單的動態線性表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.