ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-06-10 23:59:22  阅读:256  来源: 互联网

标签:node head struct da next pnode 干货 双链 数据结构


哈咯~ 今天的最后一题 

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

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

 

结果显示

  

 

标签:node,head,struct,da,next,pnode,干货,双链,数据结构
来源: https://blog.csdn.net/xiexiexiexieqing/article/details/117793241

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有