数据结构 双链表存储(c程序 干货满满)

2021/6/11 1:20:55

本文主要是介绍数据结构 双链表存储(c程序 干货满满),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

哈咯~ 今天的最后一题 

还是 大家先做一做  看看和我一不一样 

1.添加数据 (345)(cds)(5)(9.1)(0.4)(age)(name

2.删除第二个数据

3.将 (考试)插入在(5)后面

 

目录

粗略分析

        1.添加数据

        2.删除 第二个数据

       3.插入(考试)

细节分析

         1.遍历数据的类型   

         2.数字类型的打印

           3.字符串的比较

代码实现

操作演示

                 

 

结果显示



粗略分析

        1.添加数据

                这道题的数据有两类 数字 和 字符串 

                所有要定义一个结构体数组 题里的数据有 整型浮点型  为了兼容

                结构体就定义 flaot型 char型

                在进行双链表的操作

        2.删除 第二个数据

                这个题比较人性化 数据很少 第二个 一眼看过去就找到是(cds)   

                所有在删除前 先找删除

       3.插入(考试)

             (考试)插入在(5)后面还是

               先遍历再到(5)在插入     

细节分析

         1.遍历数据的类型   

                  这个数据有两个 一种是 数字 一种是 字符串 

                  它是什么类型打印什么类型 所有

                   第一个(345)让 float=345  char=NULL

                   第二个 (cds)让 float=0   char=cds

                   ...............

                  遍历的时候只要 float=0 就是字符串 打印char                                                                                                          float!=0   就是数字    打印float

                   这样就可以把 数字 和字符串分开打印 

         2.数字类型的打印

                  数字有(345)int型 和(9.1)flaot型

                  打印的时候还是要把他们分开 

                  这里给大家分析一个小技巧

                  定义一个和结构体数据里面一样的 float型的   让T等于结构体里的

                  在定义一个 int 型  让L=T  就把数据强制转化为int型 

                  然后 L/T 如果他们=1 就是int型 打印%d

                                如果等于1 就是float型 打印%f

           3.字符串的比较

                     字符串的比较和数字的比较不同 是用函数比较的 比如

                    char a[20]="xie"      char b[20]="qing"  if(a[20]==b[20])   是

                    我们用函数 strcmp 来比较  if(strcmp(a[20],b[20])==0)这才是判断字符串大小

代码实现

   

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct data   //结构体数据 
{
	float a;  //浮点型 
	char a1[20]; //字符串 
};

struct node   //结构体节点 
{
	struct data da;  //结构体数据 
	struct node*next; //后指针 
	struct node*front;//前指针 
};

struct node*head()
{
	struct node*headnode=(struct node*)malloc(sizeof(struct node)); //开辟空间 
    headnode->da.a=0;      //初始化 
    headnode->da.a1[20]="null";//初始化 
    headnode->next=NULL;
    headnode->front=NULL;
    return headnode;
}

struct node*newnode(struct data da)   //创建节点 
{
	struct node*newnode=(struct node*)malloc(sizeof(struct node));//开辟空间 
	newnode->da=da;    //等于 传过来的数据 
	newnode->next=NULL;
	newnode->front=NULL;
	return newnode;
}

void in(struct node*head,int i)  //添加 
{
   struct node*pnode=head;  
   struct node*a[i];//结构体数组 
   int n;
   for(n=0;n<i;n++)
   {
   	  a[n]=(struct node*)malloc(sizeof(struct node)); //每一个数组的元素都要开辟空间 
   	  printf("第%d个节点为:",n+1);
   	  scanf("%f%s",&a[n]->da.a,&a[n]->da.a1);//赋值 
	  a[n]->front=pnode;
	  pnode->next=a[n];
	  pnode=a[n];   	  
   }
   pnode->next=head;
}

void print(struct node*head)
{
	struct node*pnode=head->next;
	while(pnode!=head)
	{
        if(pnode->da.a!=0)   //float不为0 打印数字 
        {
        	float i=pnode->da.a;  //int 和 float 分类操作 
        	int t=i;
        	if(pnode->da.a/t==1)
        	printf("%d\t",t);
        	else
        	printf("%.1f\t",i);
		}
		//if(pnode->da.a!=0)
        //printf("%.1f\t",pnode->da.a);
        else
        printf("%s\t",pnode->da.a1); //float为0 打印字符串 
        pnode=pnode->next;
	}
}

void del(struct node*head) //删除  
{
	struct node*pnode=head->next;
	while(pnode!=head)
	{
		if(strcmp(pnode->da.a1,"cds")==0)//字符串的比较 
		{  
             pnode->front->next=pnode->next;
             pnode->next=pnode->front;
             return ;
		}
		pnode=pnode->next;
	}
	printf("失败"); //当循环结束 说明没有这个值 所有打印 失败 
}

void show(struct node*head,struct data x) //插入 
{
	struct node*pnode=head->next;
	struct node*newnode1=newnode(x);  
	while(pnode!=head)
	{
		if(pnode->da.a==5)  //找 (5)这个节点 
		{
			newnode1->next=pnode->next;
			newnode1->front=pnode;
			pnode->next->front=newnode1;
			pnode->next=newnode1;
		}
		pnode=pnode->next;
	}
}

int main(int argc, char *argv[])
 {
	struct node*headnode=head();
	in(headnode,7);
	print(headnode);
		
///删除///
printf("\n"); printf("\n");printf("\n");
     printf("删除\n");
     del(headnode);
     print(headnode);
  
插入//
printf("\n"); printf("\n");printf("\n");
     printf("插入\n");
     struct data x={0,"考试"};   
	 show(headnode,x);  
     print(headnode);
	return 0;
}

                  

操作演示

                 

<iframe allowfullscreen="true" data-mediaembed="bilibili" id="PO73mWix-1623340372934" src="https://player.bilibili.com/player.html?aid=376034558"></iframe>

2t

 

结果显示

  

 



这篇关于数据结构 双链表存储(c程序 干货满满)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程