zoukankan      html  css  js  c++  java
  • 再次复习数据结构:c语言链表的简单操作

    最近呢,又要面临多次的数据结构与算法方面的试题了,而我呢,大概也重新温习c语言的基本要点快一个月了,主要是针对指针这货的角度在研究c语言,感觉又学到了不少。

    现在c指针感觉知道点了,也就匆忙开展数据结构部分了。

    首先,是比较简单的链表部分,其实说起来,说链表简单,那也是理解了之后才简单,记得刚开始不理解之前,感觉链表就是神。

    链表:

         创建链表,一般步骤是申明一个结构体,结构体里面再定义一个指向结构体自己的指针,通过这个指针将一块一块的内存区穿起来。

                  如:

                        struct node *next;

                  一块块的内存呢,自然是:malloc(sizeof(node))

                  几个核心的语句:    s=(node *)malloc(sizeof(node));

                                             p->next=s;

                                             p=s;

                                         穿到最后,结束时用 p->next=NULL;

          求链表长度,不用说,从头遍历到尾,定义个变量n计数即可。

          删除节点:这篇文章是单链表,删除的要义就是:

                                     删节的前一个     要删除的节点    删节的后一个

                                          pre               p                    after

                           删之前他们的关系是:

                                         pre->next=p;

                                         p->next=after;

                    显然,删除要做的步骤就是:

                                         pre->next=after   或   pre-next=p->next

                     释放这块内存    free(p);

    直接运行代码:

     1 #include<stdio.h>
     2 #include<malloc.h>
     3 typedef struct student
     4 {
     5  int data;
     6  struct student *next;
     7 }node;
     8 
     9 node *create()//创建链表,返回头指针
    10 {
    11  node *p,*head,*s;
    12  head=(node *)malloc(sizeof(node));
    13  int x,cycle=1;
    14  
    15  p=head;
    16  while(cycle)
    17  {
    18   scanf("%d",&x);
    19   if(x)
    20   {
    21   s=(node *)malloc(sizeof(node));
    22   s->data=x;
    23   p->next=s;
    24   p=s;
    25   }
    26   else
    27    cycle=0;
    28  }
    29  head=head->next;
    30  p->next=NULL;
    31  return head;
    32 }
    33 int length(node *head)//链表长度
    34 {
    35  node *p;
    36  int n=0;
    37  p=head;
    38  while(p)
    39  {
    40   n++;
    41   p=p->next;
    42  }
    43  return  n;
    44 }
    45 void print(node *head)//打印该链表
    46 {
    47  node *p;
    48  p=head;
    49  while(p)
    50  {
    51   printf("%d\t",p->data);
    52   p=p->next;
    53  }
    54 }
    55 node *del(node *head,int num)
    56 {
    57  node *p1,*p2;
    58  p1=head;
    59  while(num!=p1->data&&p1->next!=NULL)
    60  {
    61   p2=p1;
    62   p1=p1->next;
    63  }
    64  if(num==p1->data)
    65  {
    66   if(p1==head)
    67   {
    68    head=p1->next;
    69    free(p1);
    70   }
    71   else
    72    p2->next=p1->next;
    73  }
    74  else
    75   printf("Not have this number\n");
    76  return head;
    77 }
    78 int main()
    79 {
    80  node *head;
    81  head=create();
    82  printf("the List's length is %d\n",length(head));
    83  print(head);
    84 
    85  del(head,2);
    86     printf("\nthe List's length is %d\n",length(head));
    87  print(head);
    88 
    89  return 0;
    90 }

    截图:

  • 相关阅读:
    ubuntu下安装maven
    159.Longest Substring with At Most Two Distinct Characters
    156.Binary Tree Upside Down
    155.Min Stack
    154.Find Minimum in Rotated Sorted Array II
    153.Find Minimum in Rotated Sorted Array
    152.Maximum Product Subarray
    151.Reverse Words in a String
    150.Evaluate Reverse Polish Notation
    149.Max Points on a Line
  • 原文地址:https://www.cnblogs.com/dftencent/p/3325497.html
Copyright © 2011-2022 走看看