基础数据结构总结

一、链表。

功能:插入,删除,遍历。
 1 struct node
 2 {
 3     int value,next;
 4 }arr[MAXN];
 5 int top;
 6 void insert(int p,int q)
 7 {
 8     arr[++top].value=q;
 9     arr[top].next=arr[p].next;
10     arr[p].next=top;
11 }
12 void del(int p)
13 {
14     int temp=arr[p].next;
15     arr[p].next=arr[temp].next;
16 }
17 void print()
18 {
19     int temp=arr[0].next;
20     while(temp)
21     {
22         printf("%d ",arr[temp].value);
23         temp=arr[temp].next;
24     }
25     printf("\n");
26 }
View Code
二、栈。
功能:先进后出,后进先出。
1 int stack[MAXN],top;
2 inline void push(int x) {stack[++top]=x;}
3 inline int pop() {return stack[top--];}
4 inline bool empty()  {return top<1;}
View Code
三、队列。
功能:先进先出,后进后出。
1 int q[MAXN],head=0,tail=0;
2 inline void push(int x) {q[++tail]=x;}
3 inline int pop() {return q[++head];}
View Code
四、优先队列。
功能:维护序列最大值、最小值。
 1 priority_queue<int,vector<int>,greater<int> >q;
 2 priority_queue<int,vector<int>,less<int> >Q;
 3 void insert(int x)  {q.push(x);}
 4 void print()  
 5 {
 6     while(!q.empty()) 
 7     {
 8         printf("%d ",q.top());  
 9         q.pop();
10     }
11 }
View Code
五、哈希表。
功能:储存、查询。
 1 int hash[MAXN],mod=2323237,step=2;
 2 int find(int x)
 3 {
 4     int temp=x%mod;
 5     while(hash[temp]!=0&&hash[temp]!=x)
 6     {
 7         temp+=step;
 8         if(temp>mod)  temp-=mod;
 9     }
10     return temp;
11 }
12 void insert(int x)
13 {
14     int r=find(x);
15     if(hash[r]==x)  return;
16     else hash[r]=x;
17 }
18 bool ask(int x)
19 {
20     int r=find(x);
21     if(hash[r]==x)  return 1;
22     else return 0;
23 }
View Code
六、并查集。
功能:维护集合关系,支持合并、查找。
 1 int n,f[MAXN];
 2 int find(int x)
 3 {return f[x]==x?x:f[x]=find(f[x]);}
 4 void pre()
 5 {
 6     for(int i=1;i<=n;i++)
 7         f[i]=i;
 8 }
 9 void Union(int x,int y)
10 {
11     x=find(x);  y=find(y);
12     f[x]=y;
13 }
14 bool check(int x,int y)
15 {
16     x=find(x);  y=find(y);
17     return x==y;
18 }
View Code

 

敬请期待:中级数据结构。。。

 

 

posted @ 2016-09-03 22:12  chty  阅读(160)  评论(0编辑  收藏  举报