数据结构作业的一系列答案

1、顺序表的插入运算

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

const int maxn = 1500;
int a[maxn];

int main()
{
    int elenum;
    memset (a, 0, sizeof(a));
    scanf ("%d", &elenum);
    for (int i = 0; i < elenum; i++) {
        scanf ("%d", &a[i]);
    }
    int x;
    scanf ("%d", &x);
    for (int i = elenum - 1; i >= 0; i--) {
        if (i == 0 && a[0] > x) {
            a[1] = a[0];
            a[0] = x;
            break;
        }
        if (a[i] <= x) {
            a[i + 1] = x;
            //printf ("%d %d\n", a[i], a[i + 1]);
            break;
        }
        a[i + 1] = a[i];
    }
    //printf ("%d", a[0]);
    for (int i = 0; i <= elenum; i++) {
        printf ("%d ", a[i]);
    }
    printf ("\n");
    return 0;
}

2、线性表的就地逆置

#include 
#include 
#include 

using namespace std;

typedef struct Node{
    int data;
    struct Node *next;
}Node, *LinkList;

void insert1(int *a, LinkList &head, int elenum)
{
    LinkList p1 = head;
    LinkList p2;
    for (int i = 1; i <= elenum; i++) {
        scanf ("%d", &a[i]);
        p2 = (LinkList)malloc(sizeof(Node));
        p2->data = a[i];
        p1->next = p2;
        p1 = p2;
    }
    p2->next = NULL;
}

void transform1(int *a, int elenum)
{
    for (int i = 1; i <= elenum / 2; i++) {
        int t = a[i];
        a[i] = a[elenum - i + 1];
        a[elenum - i + 1] = t;
    }
}

void Reverse_l(LinkList &L){
    LinkList p=L->next;
    L->next = NULL;
    while (p!=NULL) {
        LinkList r=p->next;
        p->next=L->next;
        L->next=p;
        p=r;
    }
}

void print1(int *a, LinkList head, int elenum)
{
    for (int i = 1; i <= elenum; i++) {
        printf ("%d ", a[i]);
    }
    printf ("\n");
    LinkList p = head->next;
    while (p != NULL) {
        printf ("%d ", p->data);
        p = p->next;
    }
    printf ("\n");
}

void CreatList_head(LinkList &head, int elenum)
{
    head = (LinkList)malloc(sizeof(LinkList));
    head->next = NULL;
}

int main()
{
    int elenum;
    scanf ("%d", &elenum);
    int a[elenum + 1];
    LinkList head;
    CreatList_head (head, elenum);
    insert1 (a, head, elenum);
    transform1 (a, elenum);
    Reverse_l (head);
    print1 (a, head, elenum);
    return 0;
}

3、顺序表的删除

#include 
#include 
#include 
#include 

using namespace std;
const int maxn = 200;
const int maxm = 2000000;
int a[maxn];
int vis[maxm];

int main()
{
    int m, n, p, b, c;
    memset (vis, 0, sizeof(vis));
    scanf ("%d%d%d", &m, &n, &p);
    for (int i = 0; i < m; i++) {
        scanf ("%d", &a[i]);
    }
    for (int i = 0; i < n; i++) {
        scanf ("%d", &b);
        vis[b] = 1;
    }
    for (int i = 0; i < p; i++) {
        scanf ("%d", &c);
        if (vis[c] == 1) vis[c] = 2;
    }
    int j = 0, k = 0;
    for (int i = 0; i < m; i++) {
        if (vis[a[i]] != 2) printf ("%d ", a[i]);
    }
    printf ("\n");
    return 0;
}

4、单链表的归并

#include 
#include 
#include 
#include 

using namespace std;

typedef struct Node
{
    int data;
    struct Node *next;
}Node, *LinkList;

void CreatList(LinkList &p, int n)
{
     p = (LinkList) malloc (sizeof(Node));
     p->next = NULL;
    LinkList p1 = p;
    LinkList p2;
    for (int i = 0; i < n; i++) {
        int a;
        scanf ("%d", &a);
        p2 = (LinkList) malloc (sizeof(Node));
        p1->next = p2;//将p1->next设为p2,不是把p1->next的值赋给p2
        p2->data = a;
        p1 = p2;
    }
    p1->next = NULL;
}

void Merge(LinkList &a, LinkList &b)
{
    LinkList p1 = a->next;
    LinkList p2 = b->next;
    a->next = NULL;
    LinkList p3;
    while (p1 && p2) {
        if (p1->data <= p2->data) {
            p3 = p1->next;
            p1->next = a->next;
            a->next = p1;
            p1 = p3;
        } else {
            p3 = p2->next;
            p2->next = a->next;
            a->next = p2;
            p2 = p3;
        }
    }
    if (p2) p1 = p2;
    while (p1) {
        p3 = p1->next;
        p1->next = a->next;
        a->next = p1;
        p1 = p3;
    }
    free (b);
}

void PrintList(LinkList a)
{
    LinkList p = a->next;
    while (p) {
        printf ("%d ", p->data);
        p = p->next;
    }
    printf ("\n");
}

int main()
{
    int n, m;
    scanf ("%d%d", &n, &m);
    LinkList a, b;
    CreatList (a, n);
    CreatList (b, m);
    Merge (a, b);
    PrintList (a);
    return 0;
}

5、单链表的删除

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

struct Node;
typedef Node *PNode;
struct Node{
    int data;
    PNode next;
}Node;

void CreatList(PNode &root, int num)
{
    root = (PNode)malloc(sizeof(Node));
    PNode p, now;
    p = root;
    int cnt = num;
    while (cnt--) {
        int data;
        scanf ("%d", &data);
        now = (PNode)malloc(sizeof(Node));
        now->data = data;
        p->next = now;
        p = now;
    }
    now->next = NULL;//Õâ¾ä±ðÍü
}

void PrintList(PNode root)
{
    if (root != NULL) {
        printf ("%d ", root->data);
        PrintList (root->next);
    }
}

void Delete(PNode &a, PNode b, PNode c)
{
    PNode p1 = a->next, p2 = b->next, p3 = c->next, p = a;
    while (p1 && p2 && p3) {
        while (p2 && p2->data < p1->data) p2 = p2->next;
        while (p3 && p3->data < p1->data) p3 = p3->next;
        if (p2 && p3) {
            if (p1->data == p2->data && p1->data == p3->data) {
                while (p1 && p1->data == p2->data) {
                    p->next = p1->next;
                    free (p1);
                    p1 = p->next;
                }
                p2 = p2->next;
                p3 = p3->next;
            } else if (p1->data != p2->data && p1->data != p3->data){
                p = p1;
                p1 = p1->next;
            } else if (p1->data == p2->data) {
                p2 = p2->next;
                p = p1;
                p1 = p1->next;
            } else if (p1->data == p3->data) {
                p3 = p3->next;
                p = p1;
                p1 = p1->next;
            }

        }
    }
}

int main()
{
    int m, n, p;
    scanf ("%d%d%d", &m, &n, &p);
    PNode ra, rb, rc;
    CreatList (ra, m);
    CreatList (rb, n);
    CreatList (rc, p);
    Delete (ra, rb, rc);
    PrintList (ra->next);
    printf ("\n");
    return 0;
}

6、LOCATE操作

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

struct Node;
typedef Node *PNode;
struct Node{
    char data;
    int freq;
    PNode pre, next;
}Node;

void CreatList(int n, PNode &head)
{
    head = (PNode) malloc (sizeof (Node));
    head->next = head->pre = head;//这句话不要忘,判空的时候有用
    int cnt = n;
    PNode p = head, p1;
    while (cnt--) {
        char c;
        cin >> c;
        p1 = (PNode) malloc (sizeof (Node));
        p1->data = c;
        p1->freq = 0;
        p1->next = p->next;
        p->next = p1;
        p1->pre = p;
        p = p1;
    }
}

void Locate(char c, PNode head)
{
    PNode p = head->next;
    while (p->next != head->next) {
        if (p->data == c) {
            p->freq++;
        }
        p = p->next;
    }
}

void Arrange(PNode &head)
{
    PNode p2 = head->next;
    while (p2->next != head->next) {
        PNode p3 = p2->next;
        if (p2->pre->freq < p2->freq) {
            PNode p1 = p2->pre;
            while (p1 != head && p1->freq < p2->freq) p1 = p1->pre;
            PNode p6 = p1->next, p4 = p2->pre;
            p1->next = p2;
            p6->pre = p2;
            p4->next = p3;
            p2->pre = p1;
            p2->next = p6;
            p3->pre = p4;
        }
        p2 = p3;
    }
}

void PrintList(PNode head)
{
    PNode p = head->next;
    while (p->next != head->next) {
        cout << p->data << " ";
        p = p->next;
    }
}

int main()
{
    int m, n;
    cin >> m >> n;
    PNode head;
    CreatList (m, head);
    while (n--) {
        char c;
        cin >> c;
        Locate (c, head);
    }
    Arrange (head);
    PrintList (head);
    return 0;
}

7、表达式括号匹配

#include 
#include 
#include 
#include 

using namespace std;

struct Node;
typedef Node *PNode;
struct Node{
    char c;
    PNode link;
};

struct LinkStack{
    PNode top;
};
typedef struct LinkStack *PLinkStack;

void Pop(PLinkStack &a)
{
    PNode p = a->top;
    a->top = a->top->link;
    free (p);
}

void Push(PLinkStack &a, char c)
{
    PNode p = (PNode)malloc(sizeof(Node));
    p->c = c;
    p->link = a->top;
    a->top = p;
}

char Front(PLinkStack a)
{
    return (a->top->c);
}

int IsEmpty(PLinkStack a)
{
    return (a->top == NULL);
}

int check(string s)
{
    int l = s.length();
    PLinkStack a;
    a = (PLinkStack)malloc(sizeof(struct LinkStack));
    a->top = NULL;
    for (int i = 0; i < l; i++) {
        if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
            Push (a, s[i]);
            continue;
        }
        if (!IsEmpty (a)) {
            if (s[i] == ')') {
                if (Front (a) == '(') Pop (a);
                else return 0;
                continue;
            } else if (s[i] == ']') {
                if (Front (a) == '[') Pop (a);
                else return 0;
                continue;
            } else if (s[i] == '}') {
                if (Front (a) == '{') Pop (a);
                else return 0;
                continue;
            }
        } else {
            if (s[i] == ')' || s[i] == ']' || s[i] == '}') return 0;
        }
    }
    if (!IsEmpty (a)) {
        return 0;
    }
    return 1;
}

int main()
{
    string s;
    cin >> s;
    if (check (s)) cout << "yes";
    else cout << "no";
    return 0;
}

8、逆波兰式

#include 
#include 
#include 

using namespace std;

struct Node;
typedef Node *PNode;
struct Node{
    char data;
    PNode link;
};

struct Stack{
    PNode top;
};

void InitStack(Stack &a)
{
    a.top = NULL;
}

void Push(char c, Stack &a)
{
    PNode p;
    p = (PNode)malloc(sizeof(Node));
    p->data = c;
    p->link = a.top;
    a.top = p;
}

void Pop(Stack &a)
{
    PNode b = a.top;
    a.top = b->link;
    free (b);
}

char Front(Stack a)
{
    return a.top->data;
}

bool IsEmpty(Stack a)
{
    if (a.top == NULL) return true;
    else return false;
}

char Compare(char c1, char c2)
{
    if (c2 == '+' || c2 == '-') {
        if (c1 == '(') return '<';
        else return '>';
    }
    if (c2 == '*' || c2 == '/') {
        if (c1 == '+' || c1 == '-' || c1 == '(') return '<';
        else return '>';
    }
    if (c2 == '(') {
        return '<';
    }
    if (c2 == ')') {
        if (c1 == '*') return '<';
        if (c1 == '(') return '=';
        return '>';
    }
}

bool IsOperater(char c)
{
    if (c == '+' || c == '-' || c == '*' || c == '/' || c == '(' || c == ')') return true;
    return false;
}

int main()
{
    Stack s2;
    InitStack (s2);
    string s;
    cin >> s;
    int l = s.length();
    for (int i = 0; i < l; i++) {
        if (!IsOperater (s[i])) cout << s[i];
        else {
            if (s[i] == '(') Push (s[i], s2);
            else if (s[i] == ')') {
                char c = Front(s2);
                while (c != '(') {
                    cout << c;
                    Pop (s2);
                    c = Front(s2);
                }
                Pop (s2);
            } else {
                if (IsEmpty (s2)) {
                    Push (s[i], s2);
                } else {
                    while (!IsEmpty(s2) && Front(s2) != '(' && Compare (Front (s2), s[i]) != '<') {
                        char c = Front (s2);
                        Pop (s2);
                        cout << c;
                    }
                    Push (s[i], s2);
                }
            }
        }
    }

    while (!IsEmpty (s2)) {
        char c = Front (s2);
        Pop (s2);
        cout << c;
    }
    return 0;
}

//(a+b)*c

9、循环队列(循环队列的题直接用数组做的、、、、就不贴了)
10、k阶斐波那契数列

#include 
#include 
#include 

using namespace std;

int main()
{
    int Max, k;
    cin >> Max >> k;
    int f[200];
    memset (f, 0, sizeof(f));
    f[k] = 1;
    int sum = 0;
    int n;
    for (int i = k + 1; ; i++) {
        sum -= f[i - k - 1];
        sum += f[i - 1];
        f[i] = sum;
        if (f[i] > Max && f[i - 1] <= Max) {
            n = i - 1;
            break;
        }
    }
    for (int i = n - k + 1; i <= n; i++) {
        cout << f[i] << " ";
    }
    return 0;
}

11、循环右移

#include 
#include 

using namespace std;

struct QNode;
typedef QNode *PNode;
struct QNode{
    int data;
    PNode next;//指向下一个节点
};

struct Queue{
    PNode Front;
    PNode Rear;//指向最后一个节点
};

void InitQueue(Queue &q)
{
    q.Front = new (QNode);
    q.Rear = q.Front;
    q.Front->next = NULL;//Front的作用和头结点一样,没有数据
}

void Push(Queue &q, int a)
{
    PNode p;
    p = new (QNode);
    p->data = a;
    p->next = NULL;
    q.Rear->next = p;
    q.Rear = p;
}

bool Pop(Queue &q)
{
    if (q.Front == q.Rear) return false;//队列为空
    PNode p = q.Front->next;
    q.Front->next = p->next;
    if (q.Rear == p) {
        q.Rear = q.Front;
    }
    free (p);
    return true;
}

int Front(Queue q)
{
    return q.Front->next->data;
}

bool IsEmpty(Queue q)
{
    if (q.Front == q.Rear) return true;
    return false;
}

int main()
{
    Queue q;
    InitQueue (q);
    int n, k;
    cin >> n >> k;
    int b1[1000], b2[1000];
    for (int i = 0; i < n; i++) {
        cin >> b1[i];
    }
    for (int i = n - 1; i >= 0; i--) {
        Push (q, b1[i]);
    }
    for (int i = 0; i < k; i++) {
        int a = Front (q);
        Pop (q);
        Push (q, a);
    }
    int cnt = 0;
    while (!IsEmpty (q)) {
        b2[cnt++] = Front (q);
        Pop (q);
    }
    for (int i = n - 1; i >= 0; i--) {
        cout << b2[i] << " ";
    }
    cout << endl;
    return 0;
}

12、以三元组表作为存储结构实现矩阵的相加

#include 

using namespace std;

struct Node{
    int row, column, data;
};

int main()
{
    int t1, t2;
    cin >> t1 >> t2;
    Node a[200], b[200], c[400];
    for (int l = 0; l < t1; l++) {
        cin >> a[l].row >> a[l].column >> a[l].data;
    }
    for (int l = 0; l < t2; l++) {
        cin >> b[l].row >> b[l].column >> b[l].data;
    }
    int i = 0, j = 0, k = 0;
    while (i != t1 && j != t2) {
        if (a[i].row < b[j].row) {
            while (j != t2 && i != t1 && a[i].row < b[j].row) {
                c[k].row = a[i].row;
                c[k].column = a[i].column;
                c[k].data = a[i].data;
                i++;
                k++;
            }
        } else if (a[i].row > b[j].row) {
            while (j != t2 && i != t1 && a[i].row > b[j].row) {
            c[k].row = b[j].row;
            c[k].column = b[j].column;
            c[k].data = b[j].data;
            j++;
            k++;
            }
        } else if (a[i].row == b[j].row) {
            if (a[i].column < b[j].column) {
                c[k].row = a[i].row;
                c[k].column = a[i].column;
                c[k].data = a[i].data;
                i++;
                k++;
            } else if (a[i].column > b[j].column) {
                c[k].row = b[j].row;
                c[k].column = b[j].column;
                c[k].data = b[j].data;
                j++;
                k++;
            } else if (a[i].column == b[j].column) {
                if (a[i].data + b[j].data != 0) {//可能加起来是0
                    c[k].row = b[j].row;
                    c[k].column = b[j].column;
                    c[k].data = b[j].data + a[i].data;
                    k++;
                }
                j++;
                i++;
            }
        }
    }
    while (i != t1) {
        c[k].row = a[i].row;
        c[k].column = a[i].column;
        c[k].data = a[i].data;
        k++;
        i++;
    }
    while (j != t2) {
        c[k].row = b[j].row;
        c[k].column = b[j].column;
        c[k].data = b[j].data;
        k++;
        j++;
    }
    for (int l = 0; l < k; l++) {
        cout << c[l].row << " " << c[l].column << " " << c[l].data << endl;
    }
    return 0;
}

13、以十字链表作为存储结构实现矩阵相加

#include 
#include 

using namespace std;

struct OLNode;
typedef OLNode *PNode;
struct OLNode{
    int data;
    int row, col;
    PNode Right, Down;
};

struct CrossLink{
    int m, n, t;
    PNode *Row, *Column;
};

void Insert(CrossLink &a, int data, int r, int c)
{
    PNode p;
    p = (PNode)malloc(sizeof(OLNode));
    p->col = c;
    p->row = r;
    p->data = data;
    p->Right = p->Down = NULL;
    if (a.Row[r] == NULL || a.Row[r]->col > c) {
        p->Right = a.Row[r];
        a.Row[r] = p;
    } else {
        PNode p1 = a.Row[r];
        while (p1->Right != NULL && p1->Right->col < c) p1 = p1->Right;
            p->Right = p1->Right;
            p1->Right = p;
    }
    if (a.Column[c] == NULL || a.Column[c]->row > r) {
        p->Down = a.Column[c];
        a.Column[c] = p;
    } else {
        PNode p1 = a.Column[c];
        while (p1->Down != NULL && p1->Down->row < r) p1 = p1->Down;
            p->Down = p1->Down;
            p1->Down = p;
    }
}

void CreatCrossLink(CrossLink &a, int t, int m, int n)
{
    a.Row = (PNode*)malloc(sizeof(PNode) * (m + 1));
    a.Column = (PNode*)malloc(sizeof(PNode) * (n + 1));
    for (int i = 0; i <= m; i++) {
        a.Row[i] = NULL;
    }
    for (int i = 0; i <= n; i++) {
        a.Column[i] = NULL;
    }
    a.m = m;
    a.n = n;
    a.t = t;
    for (int i = 0; i < t; i++) {
        int r, c, data;
        cin >> r >> c >> data;
        Insert (a, data, r, c);
    }
}

void Print(CrossLink a)
{
    int m = a.m;
    for (int i = 1; i <= m; i++) {
        if (a.Row[i] != NULL) {
            PNode p = a.Row[i];
            while (p != NULL) {
                cout << p->row << " " << p->col << " " << p->data << endl;
                p = p->Right;
            }
        }
    }
}

void Add(CrossLink a, CrossLink b, CrossLink &c)
{
    int m = a.m, n = a.n;
    c.Row = (PNode*)malloc(sizeof(PNode) * (m + 1));
    c.Column = (PNode*)malloc(sizeof(PNode) * (n + 1));
    for (int i = 0; i <= m; i++) {
        c.Row[i] = NULL;
    }
    for (int i = 0; i <= n; i++) {
        c.Column[i] = NULL;
    }
    c.m = m;
    c.n = n;
    int cnt = 0;
    for (int i = 1; i <= m; i++) {
        PNode p1 = a.Row[i], p2 = b.Row[i];
        while (p1 != NULL && p2 != NULL) {
            if (p1->col < p2->col) {
                Insert (c, p1->data, p1->row, p1->col);
                cnt++;
                p1 = p1->Right;
            } else if (p1->col > p2->col) {
                Insert (c, p2->data, p2->row, p2->col);
                p2 = p2->Right;
                cnt++;
            } else {
                int data = p1->data + p2->data;
                if (data != 0) {
                    Insert (c, data, p1->row, p1->data);
                    cnt++;
                }
                p1 = p1->Right;
                p2 = p2->Right;
            }
        }
        while (p1 != NULL) {
            Insert (c, p1->data, p1->row, p1->col);
            cnt++;
            p1 = p1->Right;
        }
        while (p2 != NULL) {
            Insert (c, p2->data, p2->row, p2->col);
            cnt++;
            p2 = p2->Right;
        }
    }
    c.t = cnt;
}

int main()
{
    int m, n, t1, t2;
    cin >> m >> n >> t1 >> t2;
    CrossLink a, b, c;
    CreatCrossLink (a, t1, m, n);
    CreatCrossLink (b, t2, m, n);
    Add (a, b, c);
    Print (c);
    return 0;
}

/*
3 4 3 2
1 1 1
1 3 1
2 2 2
1 2 1
2 2 3
*/

你可能感兴趣的:(杂)