Experiment 1: the basic operation of the list

Preliminaries

Every time you browse all I insist on writing down the power, if one day you do not, I will not write a

Structure

C language data types include the basic data types, data type structure type Type and void pointer; basic data types integer, real, string, enumeration; structure data types include arrays, structures, public body; structure you can have elementary data types from a combination of the pointer type, primarily used to represent the list, stack, tree, and other more complex data objects.

  • The basic format

    Student information as an example

    struct student
    {
      char sex;               //数据域
      int age;
      struct student *next;   //指针域
    };                       //结构体要以分号结束

    A node on the list represents a structure object pointer field may include a data field; next pointer point to the address of the next node;

  • typedef operator

    Simple to understand typedef be a new name for an existing data type

    //给int起个新名字叫INTER
    typedef int INTER;    
    int e=10;    INTER e=10;  //等价

    Similarly, a new name may be given to a structure, to facilitate the writing of code, and the code can be made simple and easy to read

    typedef struct student
    {
      char sex;             
      int age;
      struct student *next; 
    }STUDENT;

Global Variables

Simply speaking, called local variables, including the main function, in vitro defined function is the function body global variables defined above; global variable characteristics has a lower points

  • In the C language structure in static memory storage area
  • All functions can access and change its value
  • Local variables can be defined by the main function in place of the global variables to the custom function parameter passing mode
int e=10;     //全局变量
int main()
{
  int m=20;   //局部变量
  printf("%d  %d",e,m);
}
// 10  20

pointer

  • A pointer to a structure

    typedef struct student
    {
      char sex;              
      int age;
      struct student *next;
    }STUDENT;
    STUDENT *p;  //指针p指向结构体STUDENT
  • Pointer to a variable structure

    typedef struct student
    {
      char sex;              
      int age;
    }STUDENT;
    STUDENT stu1,stu2,stu3;   //定义三个对象,分别代表学生1、2、3
    STUDENT *p;               //此时p未初始化,指向一个随机值,因此需要对指针初始化 
    p=&stu1;                  //把学生1的地址赋给p

    Since the structure is provided with a linked list pointer field, a pointer field points to the next node, in fact, does not need pointer to a structure of the variable

Code analysis

  • Create a list

    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct LNode
    {
        char date;
        struct LNode *next;
    }LNode;
    
    LNode *head; //定义链表头指针  
    int n;       //n用来存放链表结点个数

    In the above code, the head pointer is set to the node number of global variables, the list is not random because the storage structure, the other must be accessed through the head node pointer of the linked list at the time of insertion, removal, and each function requires function Get head pointer;

  • Insert

    //插入元素e到表尾
    void Insert_end()
    {
        char e;
        LNode *p,*p_new = NULL;
        p=head;
        printf("请输入要插入的元素:\n");
        scanf("%c",&e);
        for(int i=0;i<n;i++)    //通过for循环,是p指向最后一个结点
        {
            p=p->next;
        }
        p_new=(LNode *)malloc(sizeof(LNode)); //为新添的节点分配空间
        if(p_new==NULL)
        {
            printf("内存分配失败!\n");
            exit(0);
        }
        p_new=p;       
        p_new->date=e;
        p_new->next=NULL;
        n++;
    }
    
    //插入元素e到表头
    void Insert_head()
    {
    
        char e;
        printf("请输入元素:\n");
        getchar();  //吃掉缓冲区的回车键,一定要加这一句,不然很惨的
        scanf("%c",&e);
        LNode *p,*p_new;
        p=head;
        p_new=(LNode *)malloc(sizeof(LNode));
        if(p_new==NULL)
        {
            printf("内存分配失败!\n");
            exit(0);
        }
        p_new->next=p;
        p_new->date=e;
        p=p_new;
        head=p;     //使head重新指向头指针
        n++;
    }
  • The insertion element can be inserted into the header, footer, or after a certain element, if a function written in each case too cumbersome, thus following the complete code, to integrate into three cases a function

The complete code

The program's interface is relatively simple, and I think focusing algorithm understanding

#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
    char date;
    struct LNode *next;
}LNode;

LNode *head;
int n;

//创建链表并输入元素
void create()
{
    char e;             //用来暂时存放你从键盘输入的元素
    LNode *p,*p_new;    //p指向当前最后一个结点,p_new指向新插入的结点
    head=(LNode *)malloc(sizeof(LNode));  //为头指针指向的头节点开辟空间
    if(head==NULL)
    {
        printf("内存分配失败!\n");
        exit(0);
    }
    p=head;         //将头结点的地址赋给p,用p代替head,因为链表为空时,头结点也是最后一个结点
    printf("请输入元素!\n");
    for(int i = 0;;i++)  
    {
        p_new=(LNode *)malloc(sizeof(LNode));
        if(p_new==NULL)
        {
            printf("内存分配失败!\n");
            exit(0);
        }
        scanf("%c",&e);
        n++;          //更新链表元素个数
        p->next=p_new;//将新结点的地址赋给最后一个结点的指针域
        p->date=e;    //将从键盘输入的元素e存放到上个结点的数据域
        p_new->next=NULL; 
        p=p_new;     //使p重新指向最后一个结点
        if(getchar()=='\n')
        {
            break;
        }   
    }
  free(p);  //上面代码执行完毕后,实际上最后一个结点是空的,因为数据都是存放在倒数第二个结点的数据域(p->date=e),因此应该释放最后一个结点占用的内存空间
}
//显示链表中所有元素
void Output()
{
    LNode *p;
    p=head;
    printf("链表中的元素为:\n");
    for(int i=0;i<n;i++)
    {
        printf("%c ",p->date);
        p=p->next;
    }
    printf("\n");
}
//插入元素e到首、尾、第i个元素后面
void Insert_i()
{
    int t;      //t用来存放插入的位置信息
    char e;
    LNode *p,*p_new;
    p=head;
    p_new=(LNode *)malloc(sizeof(LNode));
    if(p_new==NULL)
    {
        printf("内存分配失败!\n");
        exit(0);
    }
    printf("请输入位置i和元素e:\n");
    scanf("%d",&t);
    getchar();    
    scanf("%c",&e);
    //插入到表头
    if(t==0)
    {
        p_new->next=p;
        p_new->date=e;
        p=p_new;
        head=p;
        n++;
    }
    //插入到表尾
    else if(t==n)
    {
        for(int i=0;i<n;i++)
        {
            p=p->next;
        }
        p_new=p;
        p_new->date=e;
        p_new->next=NULL;
        n++;
        
    }
  //插入到某个元素的后面
    else
    {
        for(int i=0;i<t-1;i++)
        {
            p=p->next;
        }
        p_new->next=p->next;
        p->next=p_new;
        p_new->date=e;
        
        n++;
    }
}

//删除指定元素
void delete()
{
    char e;
    int n;
    LNode *p;
    p=head;
    printf("请输入需要删除的元素的位置: ");
    scanf("%d",&n);
    if(n==1)
    {
        head=p->next;
    }
    else{
       for(int i=0;i<n-2;i++)
       {
           p=p->next;
       }
       e=p->next->date;
       p->next=p->next->next;
       n--;
    }
  free(p);
}

//菜单
void menu()
{
    
    printf("\t\t\t* 1-输入\n\t\t\t* 2-插入\n\t\t\t* 3-删除\n\t\t\t* 4-输出\n\t\t\t* 0-退出\n");
    
}

int main()
{
    head=NULL;
    int choose;
    menu();
   while(1)
   {
       printf("%c亲,请开始你的操作:",0);
       scanf("%d",&choose);
       getchar();     //吃掉你你输入后按下的回车键
       if(choose==0)
           exit(0);
       else
       {
           switch (choose) {
               case 1:
                   create();
                   break;
               case 2:
                   Insert_i();
                   break;
               case 3:
                   delete();
                   break;
               case 4:
                   Output();
                   break;
               default:
                   break;
           }
       }
       
   }
    return 0;
}

Contact the author QQ: 749866529

Guess you like

Origin www.cnblogs.com/zhulmz/p/11620482.html