1. 程式人生 > >3.14(連結串列練習)

3.14(連結串列練習)

題目:編寫一個程式,將一個圖讀入鄰接表,

(1)連結串列:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
struct Node{
    int data;
    struct Node* next;
};

struct H{
    int id;
    struct Node* First;
    struct H* next;
};
typedef struct H* Tu;

Tu Find(
int id,Tu L) { Tu p=L->next; while(p->id!=id&&p!=NULL) p=p->next; return p; } void Insert(int x,Tu L) { Node *h=L->First,*tp,*p; p=h; while(p->next!=NULL) p=p->next; tp=(Node*)malloc(sizeof(struct Node)); tp->data=x; tp->next=NULL; p
->next=tp; } int main(void) { Tu h,p,tp; struct Node *p1,*p2; int n,m,x,y,i; cin>>n>>m; h=(Tu)malloc(sizeof(struct H)); h->next=NULL; p=h; for(i=1;i<=n;i++) { tp=(Tu)malloc(sizeof(struct H)); tp->id=i; tp->First=(Node*)malloc
(sizeof(struct Node)); tp->First->next=NULL; tp->next=p->next; p->next=tp; p=p->next; } for(i=0;i<m;i++) { cin>>x>>y; p=Find(x,h); Insert(y,p); p=Find(y,h); Insert(x,p); } p=h->next; while(p!=NULL) { p1=p->First->next; while(p1!=NULL) { printf("(%d,%d) ",p->id,p1->data); p1=p1->next; } printf("\n"); p=p->next; } return 0; }
View Code

(2)遊標

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
const int maxn = 120;

struct Node1{
    int x1,next1;
};
struct Node1 cur1[maxn];
typedef int List1;
typedef List1 Position1;

void Init1()
{
    int i;
    for(i=0;i<maxn;i++) cur1[i].next1=i+1;
    cur1[i].next1=0;
}

Position1 Malloc1()
{
    Position1 p;
    p=cur1[0].next1;
    cur1[0].next1=cur1[p].next1;
    return p;
}

void Free1(Position1 p)
{
    cur1[p].next1=cur1[0].next1;
    cur1[0].next1=p;
}

Position1 Find1(int x,List1 L)
{
    Position1 p=cur1[L].next1;
    while(cur1[p].x1!=x&&p!=0)
    {
        p=cur1[p].next1;
    }
    return p;
}

Position1 FindEnd(List1 L)
{
    List1 p=L;
    while(cur1[p].next1!=0)
    {
        p=cur1[p].next1;
    }
    return p;
}

void Insert1(int x,List1 L)
{
    Position1 tp=Malloc1(),p;
    p=FindEnd(L);
    cur1[tp].x1=x;
    cur1[tp].next1=cur1[p].next1;
    cur1[p].next1=tp;
}

void Print1(Position1 L1)
{
    Position1 p1=cur1[L1].next1;
    while(p1!=0)
    {
        printf("%d ",cur1[p1].x1);
        p1=cur1[p1].next1;
    }
    printf("\n");
}

struct Node2{
    int x2,next2,first;
};
struct Node2 cur2[maxn];
typedef int List2;
typedef List2 Position2;

void Init2()
{
    int i;
    for(i=0;i<maxn;i++) cur2[i].next2=i+1;
    cur2[i].next2=0;
}

int Malloc2()
{
    Position2 p;
    p=cur2[0].next2;
    cur2[0].next2=cur2[p].next2;
    return p;
}

void Free2(Position2 p)
{
    cur2[p].next2=cur2[0].next2;
    cur2[0].next2=p;
}

Position2 Find2(int x,Position2 L2)
{
    Position2 p=cur2[L2].next2;
    while(cur2[p].x2!=x&&p!=0)
    {
        p=cur2[p].next2;
    }
    return p;
}

void Insert2(int x,List2 L,Position2 p)
{
    Position2 tp=Malloc2();
    cur2[tp].x2=x;
    cur2[tp].first=Malloc1();
    cur1[cur2[tp].first].next1=0;
    cur2[tp].next2=cur2[p].next2;
    cur2[p].next2=tp;
}

void Print2(List2 L2)
{
    Position2 p2=cur2[L2].next2;
    while(p2!=0)
    {
        printf("點%d的鄰接點有:\n",cur2[p2].x2); 
        Print1(cur2[p2].first);
        p2=cur2[p2].next2;
    }
    printf("\n");
}

int main(void)
{
    int n,m,i,x,y;
    cin>>n>>m;
    Init1();
    Init2();
    List2 L2,p2;
    L2=Malloc2();
    cur2[L2].next2=0;
    p2=L2;
    for(i=1;i<=n;i++)
    {
        Insert2(i,L2,p2);
        p2=cur2[p2].next2;
    }
    List1 p1,L1;
    while(m--)
    {
        cin>>x>>y;
        p2=Find2(x,L2);
        Insert1(y,cur2[p2].first);
        
        p2=Find2(y,L2);
        Insert1(x,cur2[p2].first);
    }
    
    Print2(L2);
    return 0;
}
View Code