数据结构——链表
来源:互联网 发布:淘宝模特纹身 编辑:程序博客网 时间:2024/05/17 05:13
#include <stdio.h>#include <stdlib.h>#include <malloc.h>typedef struct Node{int data;struct Node *next;}NODE,*PNODE; //定义结构体, NODE相当于 struct Node ,PNODE等价于 struct Node * PNODE create();void list(PNODE head);//遍历只需要一个参数,就是头结点 head(是个指针) bool isempty(PNODE head);int length(PNODE head);void insert(PNODE head,int num,int data); //插入需要三个参数 void del(PNODE head,int num); int main(void){int num,num2,data;PNODE head=NULL;head=create();list(head);if(isempty(head)){printf("链表为空!\n");}else printf("链表不为空\n");printf("链表长度为:%d\n",length(head));printf("请输入插入的顺序和数值:\n");scanf("%d %d",&num,&data);insert(head,num,data);list(head);printf("请输入删除的节点:\n");scanf("%d",&num2);del(head,num2);list(head);return 0;}PNODE create(void){int len;int i; int var;PNODE head=(PNODE)malloc(sizeof(NODE));//创建新链表,需要动态分配存储单元,返回的是个指针 赋给head PNODE tail=head; //需要定义一个尾节点 创建新表时 把head赋给tail tail->next=NULL;if(head==NULL){printf("内存分配失败!");exit(-1); }printf("请输入链表的长度:");scanf("%d",&len);for(i=0;i<len;i++){printf("请输入第%d个节点的值:",i+1);scanf("%d",&var);PNODE p=(PNODE)malloc(sizeof(NODE));//需要新建一个指针p,用来指向每个节点 。 if(p==NULL){printf("内存分配失败!");exit(-1); }p->data=var;tail->next=p;p->next=NULL;tail=p; } return head;} void list(PNODE head){PNODE p=head->next; //遍历不需要动态分配 while(p!=NULL){printf("%d ",p->data);p=p->next;}printf("\n");}bool isempty(PNODE head){if(head->next==NULL)return true;else return false;}int length(PNODE head){int len=0;while(head->next!=NULL){len++;head=head->next;}return len;}void insert(PNODE head,int num,int data){int i=0;PNODE p=(PNODE)malloc(sizeof(NODE));if(p==NULL){printf("内存分配失败!");exit(-1); }PNODE tail=head;PNODE temp=NULL;for(i=0;i<num-1;i++){tail=tail->next;}temp=tail->next;tail->next=p;p->data=data;p->next=temp;}void del(PNODE head,int num){int i=0;PNODE tail=head;PNODE temp;for(i=0;i<num-1;i++){tail=tail->next;}temp=tail->next->next;tail->next=temp;}
运行结果:
阅读全文
0 0
- 数据结构——链表
- 数据结构——链表
- 数据结构——链表
- 数据结构——链表
- 数据结构——链表
- 数据结构——链表
- 数据结构——链表
- 数据结构——链表
- 数据结构——链表
- 数据结构—链表List
- linux数据结构—链表
- 数据结构—链表
- 数据结构—链表-双链表
- 数据结构——什么是数据结构
- 【大话数据结构】——-数据结构
- 数据结构 —— 数据结构概述
- [数据结构]数据结构——链表法
- 内核数据结构——链表
- 丢掉PSEXEC使用wmi来横向渗透
- JavaScript流程控制,关键是如何实现任务的顺序执行。
- [SQL]数据分析人员如何快速入门SQL-SQL学习感悟(一)
- maya win10下的路径脚本
- fastjson 远程反序列化poc的构造和分析
- 数据结构——链表
- lucene笔记-001-新建索引和读取索引
- Java基础--------(8)继承
- 【用VC建立C++程序】
- [SQL]数据分析人员如何快速入门SQL-删除数据(二)
- caffe可视化
- Android主题Theme的了解
- Spring4整合Hibernate4.3
- MyBatis学习总结(一)——MyBatis快速入门