苏嵌 暑假实训之第一天之数据结构单链双链表的初始化创建插入中间删除表之篇章。。。。

    带着对项目班的  向往和担忧  我开始了这个被我称之为闭关修炼的学习。。。。诚然 我基础不好,尤其是 c 但是正如老师所说,都知道苦但又有几个吃下去这个苦呢,,,梅花香之苦寒来。。。人生的意义就是在于挑战自己的缺陷弥补自己的不足的 。。。。

      废话不说了,,,开始今天的学习,首先上午老师让我们进行了c的阶段性测试,真心感觉那题目啊  很多不会做呢,,,概念题还可以 那编程题啊 ,,,压力山大。。。。

         下午讲的是数据结构, 所以 我谈谈我的数据结构的 收获吧:

         首先是单链表:

1、初始化节点

struct node

{

    int  num;     /*数域*/

    struct node *next;    /*链域*/

};/*这里尤为注意这个冒号,创建节点后需要加冒号*/

2、初始化链表

void cre_list()

{

   head = (struct node *)malloc(sizeof(structnode));/*分配内存空间*/

   head->next = NULL;

   head = NULL;

}

3、添加数据

void   add_node(int num)

{

  struct node *ptr = (struct node *)malloc(sizeof(struct node));

  ptr->num = num;

  ptr->next = head;

  head = ptr;

 }

4、遍历

void display_node()

{

  struct node *ptr = head;

  while(ptr!=NULL)/*单链表尾为空*/

  {

     printf("%d\t",ptr->num);

     ptr = ptr->next;

  }

     printf("\n");

}

练习1:创建一个单链表添加10个数据,然后将其逆序,再在5后面添加11,12,13,14,15,16。

代码如下:

#include<stdio.h>
#include<stdlib.h>
struct node
{
  int num;
  struct node*next;
};
 struct node*head;
 void cre_list()
{
  head = (struct node*)malloc(sizeof(struct node));
  head->next = NULL;
  head = NULL;
}
void display_node()
{
  struct node *ptr = head;
    while(ptr!=NULL)
    {
     printf("%d\t",ptr->num);
     ptr = ptr->next;
    
     }
     printf("\n");
}
  void add_node(int num)
{
  struct node *ptr = (struct node *)malloc(sizeof(struct node));
  ptr->num = num;
  ptr->next = head;
  head = ptr;
}
void add_model(n,m)
{
  struct node *ptr =head;
  struct node *p = (struct node *)malloc(sizeof(struct node));
  while (ptr)
  {
    if(ptr->num == n)
    {
      p->num=m;
      p->next=ptr->next;
      ptr->next=p;
     
    }
    ptr = ptr->next;
  }
}
void re_node()
{
  struct node *p1=NULL;
  struct node *p2=NULL;
  while(head)
  {
  p2 = head->next;
  head->next = p1;
  p1 = head;
  head = p2;
  }
  head = p1;
}
void del_node()
{
  struct node *ptr = head;
  struct node *temp=NULL;
  while(ptr)
{
  if((ptr->num) % 2 ==0)
  {
   temp = ptr;
   ptr = ptr->next;
   head = head->next;
   free(temp);
   temp=NULL;
  }
  else
  {
   temp = ptr;
   ptr = ptr->next;
   if((ptr->num)%2 == 0)
    {
      temp->next = ptr->next;
      temp = ptr;
      ptr = ptr->next;
      free(temp);
      temp = NULL;
    }
  }
}

}
int main()
{ int i=0;
  cre_list();
  for(i;i<10;i++)
  {
   add_node(i);
  }
  display_node();
  re_node();
  display_node();
  for(i=0;i<=5;i++)
  {
   if(i==0)
   {
     add_model(5,11);
   }
   else
   {

     add_model(i+10,i+11);
   }
  }

  display_node();
  return 0;

练习2有n个人围成一圈,顺序排号,从第一个开始报数(从1到3报数),凡报到3的退出圈子,问最后最后留下的是原来第几号的那个数(约瑟夫环)。

#include<stdio.h>

#include<stdlib.h>

struct node

{

  int num;

  struct node *next;

};

struct node *head;

struct node *last;

void cre_list()

{

  head = (struct node*)malloc(sizeof(struct node));

  last = head;

  head->next = head;

}

void del_node()

  {

    last->next=head->next;

    struct node *p1 = last->next;

    struct node *p2 ;

    int i=1;

    while(p1->next!=p1)

    {

     p2=p1->next;

     p2->next=p2->next->next;/*利用p1,p2删除后面第三个节点*/

     p1=p2->next;

    }

    printf("%d\t",p1->num);

    printf("\n");

}

void display_node()

{

  struct node *ptr =head;

  while(ptr->next!=head)

  {

    ptr=ptr->next;

    printf("%d\t ",ptr->num);

   }

}

  void add_node(int num)

  {

    struct node *ptr = (struct node *)malloc(sizeof(struct node));

    ptr->num = num;

    last->next = ptr;

    ptr->next = head;

    last = ptr;

  }

 int main()

{  

  int i;

  cre_list();

  for(i=0;i<10;i++)

  {

    add_node(i);

  } 

  display_node();

  del_node();

  return 0;

}

  以上编程思路画个图就全部很清晰了,约瑟夫环利用了p1,p2将其第三个也就是p1,p2后面一个删除,依次循环就可以了。

练习3用双向循环链表进行文件检索。(利用循环链表将一个目录下的文件建成一个循环链表,然后通过输入指令去查看上个文件以及下个文件)

#include<stdio.h>

#include<stdlib.h>

#include"fcntl.h"

#include"dirent.h"

#include<string.h>

struct node

{

  char name[20];

  struct node *prior;

  struct node *next;

};

struct node *head = NULL;

struct node *last = NULL;

void cre_list()

{

  DIR *mydir;

  struct dirent *myitem;

  mydir = opendir(".");

  while (( myitem = readdir(mydir)) !=NULL)

 {

   if(( strcmp(myitem->d_name,".") ==0)||(strcmp(myitem->d_name,"..")==0))/*每个目录下面都有两个隐藏的文件所以这里需要将这两个文件排除*/

   {

     continue;

   }

   struct node *p = (struct node *)malloc(sizeof(struct node));

   if( head == NULL)

   {

     strcpy(p->name,myitem->d_name);

     head = p;

     head->prior = NULL;

     head->next = head;

     last = p;

   }

   else

   {

     strcpy(p->name,myitem->d_name);

     last->next = p;

     p->prior = last;

     last = p;

     last->next = head;

   }

 }

   head->prior = last;

   closedir(mydir);

}

void display_node()

{

  struct node *p = head;

  while(p->next != head)

  {

    printf("%s\t",p->name);

    p = p->next;

  }

  printf("%s\n",p->name);

}

void printfile()

{

   int x;

   printf("请选择功能\n");

   printf("1输出上个文件\n");

   printf("2输出下个文件\n");

   printf("3正常退出\n");

   struct node *p = head;

   printf("此刻第一个文件为: %s\n",p->name);

   while(1)

   {

     printf("input\n");

     scanf("%d",&x);

     switch(x)

     {

       case 1:

       {

         p = p->prior;

         printf("上一个文件为:%s\n",p->name);

         break;

       }

       case 2:

       {

         p=p->next;

         printf("下一个文件是:%s\n",p->name);

         break;

       }

      case 3:

       {

         printf("正常退出\n");

         exit(0);

       }

     }

   }

}

int main()

{

  cre_list();

  display_node();

  printfile();

  return 0;

}

   文件检索主要还是建立在双向循环链表之上的,就是把一个目录下的文件有几个算几个串连起来形成一个双向链表,再通过移动指针来查看上一个或者是下个文件。感觉这个做起来 在目录加进来那个地方还是有点不太理解的。。。

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

         

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值