南昌大学技术交流...吧 关注:89贴子:975
  • 6回复贴,共1

11月12日安卓部与web部上课代码(指针与链表)提问与讨论区

只看楼主收藏回复

一楼不给看


IP属地:浙江1楼2015-11-12 21:43回复
    先是指针
    #include <stdio.h>
    #include <stdlib.h>
    void swap(int *a, int *b);
    int main1()
    {
    /*int a = 10;
    int *p = &a;
    printf("%d, %p, %p\n", *p, p, &a);
    system("pause");
    return 0;*/
    //int a = 10;
    //int b = 5;
    //printf("交换前a = %d, b = %d\n", a, b);
    ////swap(&a, &b);
    //printf("交换后a = %d, b = %d\n", a, b);
    /*int a[] = { 1, 2, 4, 5, 6, 4, 9 };
    int *p = a;
    int size = sizeof(a) / sizeof(int);
    for (; p < a + size; p++)
    {
    printf("%5d", *p);
    }*/
    //printf("%d\n", size);
    /*char ch[] = "abc";
    printf("%s\n", ch);
    char *p = ch;
    printf("%s\n", p);*/
    getchar();
    return 0;
    }
    void swap(int *a, int *b)
    {
    int temp = *a;
    *a = *b;
    *b = temp;
    }


    IP属地:浙江2楼2015-11-12 21:44
    回复
      再是链表的的增删改查操作
      头文件部分:
      #pragma once
      #include <stdio.h>
      #include <stdlib.h>
      struct Student
      {
      int no;
      float score;
      struct Student *next;
      };
      typedef struct Student ST;
      void insert(ST **head, int no, float score);
      void showall(ST *head);
      ST *freeall(ST *head);
      ST *rev(ST *head


      IP属地:浙江3楼2015-11-12 21:47
      回复
        源文件部分:
        咳咳像我这种渣渣的人注意了。。。前方高能
        #include "学生.h"
        void insert(ST **head, int no, float score)
        {
        ST *node = (ST *)malloc(sizeof(ST));
        node->no = no;
        node->score = score;
        node->next = NULL;
        if (*head == NULL) *head = node; // 如果链表为空,那么将新建的节点当作头结点就可以了
        else
        {
        ST *node1 = *head; // 如果链表不为空,那么定义一个零时的结构体来充当头结点
        while (node1->next != NULL) // 一直循环,直到最后一个节点
        {
        node1 = node1->next;
        }
        node1->next = node; // 把最后一个节点的指针域赋值为要插入的节点的地址
        }
        }
        void showall(ST *head)
        {
        while (head != NULL)
        {
        printf("%d, %f, %p, %p\n", head ->no, head ->score, head, head ->next);
        head = head->next;
        }
        }
        ST *freeall(ST *head)
        {
        if (head == NULL) return NULL; // 如果链表为空,那么返回NULL
        else // 否则
        {
        ST *node1 = head, *node2 = NULL;
        while (node1->next != NULL) // 一直循环,删除头结点后的所有节点
        {
        node2 = node1->next;
        node1->next = node2->next; // 要删除的节点的前一个节点与要删除的节点的后一个节点联系起来,
        free(node2); // 删除要删除的节点
        }
        free(head); // 最后删除头结点
        return NULL;
        }
        }
        ST *rev(ST *head)
        {
        ST *p1 = NULL, *p2 = NULL, *p3 = NULL;
        if (head == NULL || head->next == NULL) return head;
        p1 = head;
        p2 = head->next;
        while (p2 != NULL)
        {
        p3 = p2->next;
        p2->next = p1;
        p1 = p2;
        p2 = p3;
        }
        head->next = NULL; // 链表的结束
        head = p1; // 指向最后一个节点
        return head;
        }


        IP属地:浙江4楼2015-11-12 21:48
        回复
          第二个好。。。心好痛


          IP属地:浙江5楼2015-11-12 21:49
          回复
            小吧你好


            IP属地:江西来自Android客户端6楼2015-11-19 11:46
            收起回复