CrossList.h
#ifndef CROSSLIST_H_H #define OK 1 #define ERROR 0 typedef int ElemType; typedef struct OLNode { ElemType data; int i, j; struct OLNode *right, *down; }OLNode; typedef struct { OLNode *rhead, *chead; int mu, nu, tu; }CrossList; int init_crossList(CrossList*); int make_node(OLNode**); int create_crossList(CrossList*); int print_row_crossList(CrossList*); int print_col_crossList(CrossList*); int find_value(CrossList*, ElemType, int*, int*); int destroy_crossList(CrossList*); int insert_crossList(CrossList*, ElemType, int, int); int delete_crossList(CrossList*, int, int); int add_crossList(CrossList*, CrossList*); #endif
CrossList.cpp
#include<stdio.h> #include<stdlib.h> #include"CrossList.h" int init_crossList(CrossList *clist) { clist->rhead = clist->chead = NULL; clist->mu = clist->nu = clist->tu = 0; return OK; } int make_node(OLNode **node) { if(!((*node) = (OLNode*)malloc(sizeof(OLNode)))) return ERROR; (*node)->right = (*node)->down = NULL; return OK; } int create_crossList(CrossList *clist) { if(clist->rhead) { free(clist->rhead); clist->rhead = NULL; return ERROR; } if(clist->chead) { free(clist->chead); clist->chead = NULL; return ERROR; } int mu, nu, tu; int i, j, e; OLNode *node = NULL; mu = nu = tu = 0; printf("please input mu,nu,tu: "); scanf("%d,%d,%d", &mu, &nu, &tu); //生成行頭和列頭指針 if(mu > 0 && nu > 0) { if(!(clist->rhead = (OLNode*)malloc(sizeof(OLNode) * mu))) return ERROR; if(!(clist->chead = (OLNode*)malloc(sizeof(OLNode) * nu))) return ERROR; } clist->mu = mu; clist->nu = nu; clist->tu = tu; //初始化行頭和列頭指針 for(i = 0 ; i < mu ; i++) { clist->rhead[i].right = NULL; } for(i = 0 ; i < nu ; i++) { clist->chead[i].down = NULL; } //輸入數據 while(tu-- > 0) { printf("input i, j, e: "); scanf("%d,%d,%d", &i, &j, &e); //生成新的結點保存元素 if(!make_node(&node)) return ERROR; node->data = e; node->i = i; node->j = j; //搜索元素在行中指定的位置,然後插入鏈表(操作right指針) if(clist->rhead[i].right == NULL || clist->rhead[i].j > j) { node->right = clist->rhead[i].right; clist->rhead[i].right = node; } else { //因爲十字鏈表本身是有序插入的,所以當 //插入的位置不是在開頭的時候,需要從第 //i個位置開始搜索,當搜索到了之後就相當 //於單鏈表的操作那樣插入元素。 OLNode *tmp = NULL; OLNode *tmp1 = NULL; /*for(tmp = clist->rhead[i].right ; tmp && tmp->j < j ; tmp = tmp->right);*/ tmp = &clist->rhead[i]; while(tmp && tmp->j < j) { tmp1 = tmp; tmp = tmp->right; } node->right = tmp1->right; tmp1->right = node; } //搜索元素在列中指定的位置,然後插入鏈表(操作down指針) if(clist->chead[j].down == NULL || clist->chead[j].i > i) { node->down = clist->chead[j].down; clist->chead[j].down = node; } else { OLNode *tmp = NULL; OLNode *tmp1 = NULL; /*for(tmp = clist->chead[j].down ; tmp && tmp->i < i ; tmp = tmp->down);*/ tmp = &clist->chead[j]; while(tmp && tmp->i < i) { tmp1 = tmp; tmp = tmp->down; } node->down = tmp1->down; tmp1->down = node; } } return OK; } int find_value(CrossList *clist, ElemType value, int *row, int *col) { if(!clist->rhead || !clist->chead) return ERROR; int i = 0; OLNode *tmp = clist->rhead[i].right; OLNode *tmp1 = NULL; while(tmp && i < clist->mu) { tmp1 = tmp; while(tmp1) { if(tmp1->data == value) { *row = tmp1->i; *col = tmp1->j; return OK; } tmp1 = tmp1->right; } tmp = clist->rhead[++i].right; } return OK; } int destroy_crossList(CrossList *clist) { if(!clist->rhead || !clist->chead) return ERROR; int i = 0; OLNode *tmp = clist->rhead[i].right; OLNode *tmp1 = NULL; OLNode *tmp2 = NULL; while(tmp && i < clist->mu) { tmp1 = tmp; while(tmp1) { tmp2 = tmp1; tmp1 = tmp1->right; free(tmp2); } tmp = clist->rhead[++i].right; } free(clist->rhead); free(clist->chead); clist->rhead = clist->chead = NULL; clist->mu = clist->nu = clist->tu = 0; return OK; } int insert_crossList(CrossList *clist, ElemType value, int row, int col) { if(!clist->rhead || !clist->chead) return ERROR; if(row < 0 || row > clist->mu - 1 || col < 0 || col > clist->nu - 1) return ERROR; //搜索一遍鏈表,看是否有相同位置的元素 //如果出現相同位置,則相加。 OLNode *tmp = NULL; OLNode *tmp1 = NULL; tmp = clist->rhead[row].right; while(tmp) { if(tmp->i == row && tmp->j == col) { tmp->data += value; if(tmp->data == 0) { //元素相加之後,搜索值爲0的元素,並刪除 delete_crossList(clist, tmp->i, tmp->j); } return OK; } tmp = tmp->right; } OLNode *node = NULL; if(!make_node(&node)) return ERROR; node->data = value; node->i = row; node->j = col; tmp = &clist->rhead[row]; while(tmp && tmp->j < col) { tmp1 = tmp; tmp = tmp->right; } node->right = tmp1->right; tmp1->right = node; tmp = &clist->chead[col]; tmp1 = NULL; while(tmp && tmp->i < row) { tmp1 = tmp; tmp = tmp->down; } node->down = tmp1->down; tmp1->down = node; clist->tu++; return OK; } int delete_crossList(CrossList *clist, int row, int col) { if(!clist->rhead || !clist->chead) return ERROR; OLNode *tmp = NULL; OLNode *tmp1 = NULL; OLNode *tmp2 = NULL; OLNode *t = NULL; OLNode *t1 = NULL; OLNode *t2 = NULL; //刪除一個結點需要修改行結點指針和列結點指針 tmp = &clist->rhead[row]; t = &clist->chead[col]; //行遍歷元素 while(tmp) { tmp1 = tmp; tmp = tmp->right; if(tmp && tmp->i == row && tmp->j == col) { tmp2 = tmp; tmp1->right = tmp2->right; break; } } //列遍歷元素 while(t) { t1 = t; t = t->down; if(t && t->i == row && t->j == col) { t2 = t; t1->down = t2->down; break; } } free(tmp2); clist->tu--; return OK; } //相加後的矩陣爲clist int add_crossList(CrossList *clist, CrossList *clist1) { if(!clist->rhead || !clist->chead || !clist1->rhead || !clist1->chead) return ERROR; if(clist->mu != clist1->mu && clist->nu != clist1->nu) return ERROR; OLNode *tmp = NULL; OLNode *tmp1 = NULL; int i = 0; tmp = clist1->rhead[i].right; //遍歷元素進行相加 while(i < clist1->mu) { tmp1 = tmp; while(tmp1) { insert_crossList(clist, tmp1->data, tmp1->i, tmp1->j); tmp1 = tmp1->right; } tmp = clist1->rhead[++i].right; } return OK; } //行打印 int print_row_crossList(CrossList *clist) { if(!clist->rhead || !clist->chead) return ERROR; int i; OLNode *tmp = clist->rhead[0].right; OLNode *tmp1 = NULL; i = 0; while(i < clist->mu) { tmp1 = tmp; if(!tmp1) { tmp = clist->rhead[++i].right; continue; } while(tmp1) { printf("%5d", tmp1->data); tmp1 = tmp1->right; } printf("/n"); tmp = clist->rhead[++i].right; } return OK; } //列打印 int print_col_crossList(CrossList *clist) { if(!clist->rhead || !clist->chead) return ERROR; OLNode *tmp; OLNode *tmp1; int j = 0; tmp = clist->chead[j].down; while(j < clist->nu) { tmp1 = tmp; if(!tmp1) { tmp = clist->chead[++j].down; continue; } while(tmp1) { printf("%5d", tmp1->data); tmp1 = tmp1->down; } printf("/n"); tmp = clist->chead[++j].down; } return OK; }
二叉平衡查找樹AVL詳解 看懂這篇文章所需的知識點 樹、二叉搜索樹、樹高、樹深、層等概念 AVL樹 概念:任意節點的左右子樹的高度差不能大於1的樹即爲AVL樹,是爲了解決在頻繁插入刪除等動態更新下出現的時間複雜度退化的問題,所以平
AVL樹旋轉原理和簡易實現 二叉搜索樹雖然可以提高搜索效率,但是如果插入的數據有序時很有可能變成單支,如果變成單支樹的時候,那麼查找時效率也不高了。因此引入AVL樹。 AVL樹是當向這棵樹插入節點的時候,要保證每個節點的左右子樹的
紅黑樹的旋轉原理和模擬實現 我們瞭解到AVL樹雖然效率很高,但是它是通過多次的旋轉纔到達一個絕對的平衡,旋轉的消耗其實也很大。因此開始引入近似平衡的一棵樹----紅黑樹(RBTree)。紅黑樹每一個節點不是紅色的就是黑色的,它保證
1.給定100億個整數,設計算法找到只出現一次的整數? ①方法一 100億個整數就是400億個字節,42億九千萬是4G,那麼1G就是10億字節,所以要存下100億個整數需要40G的內存空間。因此我們採用位圖100億個整數大概就是1
布隆過濾器 假如現在有40億個ip地址(string類型),然後給你一個ip地址,讓你查找這個ip地址在不在這40億個ip地址裏?我們應該怎麼做呢? 如果用哈希表來處理的話,這裏有40億的數據,數據量太大,因此太佔用空間 如果用
————————————————18.4.18更新有時我們會遇到這樣的問題:在一棵樹上,每次詢問兩點間路徑上的和或者是最值。但我們用搜索時,時間就會到O(n),這樣根本就完不成算法。但樹上剖分就可以縮短修改的時間。樹上剖分的算法簡介我們定
request作爲前後臺交換的橋樑,有重要作用。 request常用的方法有讀參數:public String getParameter(String paramName);讀取屬性public Object getAttribut
1.非空二叉樹的高度 1.1非遞歸算法實現求解非空二叉樹的高度 算法思想:採用層次遍歷的算法,設置變量level記錄當前結點所在的層數,設置變量last指向當前層的最右的結點,每次層次遍歷出隊的時候與last指針比較(fron
考研加油!!!!!!! 1.1樹的重要概念 1.樹是一種重要的非線性結構;在有n個結點的樹中有n-1條邊; 2.在結點個數爲n(n>1)的各棵樹中,深度最小的樹的深度是多少?它有多少葉子結點?多少分支結點?深度最大的樹的深度是多少?它有多
使用stringstream http://blog.csdn.net/xw20084898/article/details/21939811 stringstream 是 C++ 提供的另一個字串型的串流(stream)
1.什麼是線性結構 線性結構是一種有序數據項的集合,其中每個數據項都有唯一的前驅和後繼(除了第一個沒有前驅,最後一個沒有後繼)。新的數據項加入到數據集中時,只會加入到原有某個數據項之前或之後。具有這種性質的數據集,就稱爲線性結構。
一、Trie的介紹: 1、主要應用場景:搜索引擎的自動補全功能:Trie樹+詞頻(概率)權重因子 IP路由:最長前綴匹配,Trie路由算法
先總結有序數組,無序的後面再總結。。 1.以空間換時間法。 算法思想:目標數組array[length],是一個有序數組,比如int array[]={1,1,2,2,4,4,4,4,4,5,5,6,10};總共有13個元素,
數組求和 方法一:直接一次for循環 int GetSum1(int *a,int n) { int sum=0; for (int i=0; i<n;i++) { sum+=a[i];
題目描述 你有一個長爲N寬爲2的牆壁,給你兩種磚頭:一個長2寬1,另一個是L型覆蓋3個單元的磚頭。如下圖: 0 0 0 00 磚頭可以旋轉,兩種磚頭可以無限制提供。你的任務是計算用這兩種來覆蓋N*2的牆壁的覆蓋方法。例如一個2*3