数据结构头插法和尾插法建立单链表
- 格式:doc
- 大小:24.50 KB
- 文档页数:2
单链表头插法代码单链表是一种常见的数据结构,它由多个节点组成,每个节点包括两部分:一个数据存储区和一个指向下一个节点的指针。
插入操作是单链表的基本操作之一,在插入一个节点时,可以采用两种方法:头插法和尾插法。
在本篇文章中,我们将重点讲解单链表的头插法。
头插法是指在单链表的头节点之前插入一个新节点。
这种方法需要先创建一个新节点,并将其指针指向原头节点所指向的节点。
然后再将头节点的指针指向新节点。
相当于是在链表的头部插入新节点。
下面是头插法的代码实现:```struct node {int data;struct node *next;};void insert_node(struct node **head, int value) {struct node *new_node = (struct node*)malloc(sizeof(struct node));new_node->data = value;new_node->next = *head;*head = new_node;}```在上面的代码中,我们首先定义了一个节点结构体node,其中包含一个int类型的数据成员data和一个指向下一个节点的指针成员next。
然后,我们定义一个函数insert_node,这个函数的作用是向单链表中插入新的节点。
其中,head是指向链表头节点的指针,value 是要插入的节点的值。
在insert_node函数体中,我们首先通过malloc函数动态分配内存,创建一个新节点new_node。
然后,将新节点的data成员赋值为value,将新节点的next指针指向原head指针所指向的节点,最后将head指针指向新节点new_node。
这样,新节点就插入到链表的头部了。
总结一下,头插法是单链表中比较常用的一种插入节点的方式,通过这种方法,可以很方便地在链表头部插入新节点。
在实现的过程中需要注意,在创建新节点时要手动分配内存,否则会发生内存错误。
#include "stdio.h"#include "stdlib.h"typedef struct List{int data;struct List *next; //指针域}List;void HeadCreatList (List *L) //头插法建立链表{List *s;L->next=NULL;for (int i=0;i<10;i++){s=(struct List*)malloc(sizeof(struct List));s->data=i;s->next=L->next; //将L指向的地址赋值给S;L->next=s;}}void TailCreatList(List *L) //尾插法建立链表{List *s,*r;r=L;for (int i=0;i<10;i++){s=(struct List*)malloc(sizeof(struct List));s->data=i;r->next=s;r=s;}r->next=NULL;}void DisPlay(List *L){List *p=L->next;while(p!=NULL){printf ("%d ",p->data);p=p->next;}printf("\n");}int main (){List *L1,*L2;L1=(struct List*)malloc(sizeof(struct List));L2=(struct List*)malloc(sizeof(struct List)); HeadCreatList(L1);DisPlay(L1);TailCreatList(L2);DisPlay(L2);}//头插法创建链表#include <stdio.h>#include <stdlib.h>struct node{int data;struct node * next;};//建立只含头结点的空链表struct node * create_list(){struct node * head = NULL;head = (struct node *)malloc(sizeof(struct node));if (NULL == head){printf("memory out of use/n");return NULL;}head->next = NULL;head->data = 0;return head;}//头插法建立链表int insert_form_head(struct node * head, int num){struct node * head_t = head->next;struct node * new_node = NULL;new_node = (struct node *)malloc(sizeof(struct node));if (NULL == new_node){printf("memory out of use/n");return -1;}//将新结点插入到链表的最后new_node->data = num;new_node->next = head_t;head->next = new_node;return 0;}//打印链表int show_list(struct node * head){struct node * temp;temp = head->next;while(temp){printf("%d/n",temp->data);temp = temp->next;}return 0;}// 按值删除结点,头结点不被删除int delete_node(struct node *head, int data){//head_t 保存要删除结点的上一个结点struct node * head_t = head;struct node * temp = NULL;if (head == NULL){printf("delete node from empty list!/n");return -1;}//查找删除的结点的前一个结点//如果此处查找的是删除的结点,则需要另加一个指针保存删除结点的前一个指针while(NULL != head_t->next){if (data == head_t->next->data)break;head_t = head_t->next;}//如果要删除的结点不存在,直接返回if (NULL==head_t->next){printf("node not found/n");return -1;}//删除操作temp = head_t->next;head_t->next = head_t->next->next;free(temp);return 0;}void main(int argc, char* argv[]){struct node * head;head = create_list();if (NULL == head)printf("create_list error/n");insert_form_head(head,123);insert_form_head(head,456);show_list(head);printf("delete once!/n");delete_node(head,123);show_list(head);printf("delete second!/n");delete_node(head,456);show_list(head);delete_node(head,0);show_list(head);}/*//尾插法创建链表#include<stdio.h>#include<stdlib.h>struct Node{int data;struct Node * next;};//建立只含头结点的空链表struct Node * create_list(){struct Node * head = NULL;head = (struct Node *)malloc(sizeof(struct Node));if (NULL == head){printf("memory out of use/n");return NULL;}head->next = NULL;head->data = 0;return head;//尾插法建立链表int insert_form_tail(struct Node * head, int num){struct Node * temp = head;struct Node * new_node = NULL;new_node = (struct Node *)malloc(sizeof(struct Node));if (NULL == new_node){printf("memory out of use/n");return -1;}//寻找尾结点while (temp->next != NULL){temp = temp->next;}//将新结点插入到链表的最后new_node->data = num;new_node->next = NULL;temp->next = new_node;return 0;}//打印链表int show_list(struct Node * head){struct Node * temp;temp = head->next;while(temp){printf("%d/n",temp->data);temp = temp->next;}return 0;}void main(int argc, char* argv[]){struct Node * head;head = create_list();if (NULL == head)printf("create_list error/n");insert_form_tail(head,123);insert_form_tail(head,456);show_list(head);*/。
头插法建⽴单链表学习总结单链表是⼀种链式存储的数据结构,每⼀个节点存放数据和指向下⼀个节点的指针。
头插法是指每次将新节点插⼊头部(head节点之后),最终链表顺序与插⼊顺序相反。
这就好⽐你在⾷堂排队,⼤妈所在窗⼝是头部,每次都有⼈插⼊到队伍最前⾯,结果你是最后买到饭的。
图解:以下代码是新建链表并遍历输出元素#include<stdio.h>#include<stdlib.h>typedef struct _node{int data;struct _node *next;}node;node *headinsert(int n);//创建链表并插⼊新节点void output(node *head);//遍历链表并输出节点数据void destroy(node *head);//清除链表int main(){int n;node *head;scanf("%d",&n);head=headinsert(n);output(head);destroy(head);return0;}node *headinsert(int n){node *head,*q;head=(node *)malloc(sizeof(node));//malloc返回void* 需要强制转换类型head->next=NULL;for(int i=0;i<n;i++){ //申请新节点并插⼊链表q=(node *)malloc(sizeof(node));scanf("%d",&q->data);q->next=head->next;head->next=q;}return head;}void output(node *head){node *q; //新建指针q使其从头节点遍历链表 q=head;while(q->next!=NULL){q=q->next;printf("%d ",q->data);}}void destroy(node *head){node *q,*p;for(p=head;p;p=q){q=p->next;free(p);}}。
#include <stdio.h>#include <stdlib.h>typedef char DataType; //假设节点的数据类型为字符typedef struct node{//节点类型定义DataType data; //节点的数据域struct node *next; //节点的指针域}ListNode;typedef ListNode* LinkList;ListNode *p; //指向某一节点的指针LinkList head; //单链表的头指针//头插法LinkList createListF(void);LinkList createListF(void){//返回单链表的头指针char ch;LinkList head; //头指针ListNode *s; //工作指针head = NULL; //链表开始为空ch = getchar(); //读入第一个字符while (ch != '\n') {s =(ListNode*)malloc(sizeof(ListNode)); //生成新节点s->data = ch; //将读入的数据放入新节点的数据域中s->next = head;head = s;ch = getchar(); //读入下一个字符}return head;}//尾插法LinkList createListR(void);LinkList createListR(void){//返回头指针char ch;LinkList head; //头指针ListNode *s,*r; //工作指针head = NULL; //链表开始为空r = NULL; //尾指针初始值为空ch = getchar(); //读入第一个字符while (ch != '\n') {s =(ListNode*)malloc(sizeof(ListNode)); //生成新节点s->data=ch; //将读入的数据放入新节点的数据域中s->next=NULL;if (head==NULL) {head = s; //新节点插入空表,头节点指向插入的第一个节点r = s; //尾节点指向插入的第一个节点}else{r->next = s; //将新节点查到*r 之后r = s; //尾指针指向新表尾}ch = getchar();} //endwhilereturn head;}int main(int argc, const char * argv[]){LinkList l = createListR();while (l != NULL) {printf("%c",l->data);l = l->next;}printf("\n");return0; }。
不带头结点,头部插⼊法创建链表#include<stdio.h>#include<stdlib.h>#define ListSize 100typedef int DataType;typedef struct{DataType *data;int length;int size;} Sqlist;void initSqlist(Sqlist *L){L->data = (void*)malloc(ListSize * sizeof(int));if(! L->data) exit(0);L->length = 0;L->size = ListSize;}/*在顺序表的i位置插⼊元素*/void insertSqlist(Sqlist *L,int i,DataType e){int j;DataType *base;if(i<1 || i>L->length+1) exit(0);if(L->length >= L->size){base = (void *) realloc(L->data,(L->length+10)*sizeof(int));if(! base) exit(0);L->data = base;L->size = L->length +10;}for(j = L->length-1;j>= i-1;j--){L->data[j+1] = L->data[j];}L->data[i-1] = e;L->length ++;}/*从顺序表中删除元素*/void delSqlist(Sqlist *L,int i){/*位置是否合法*/int j;if(i<1 || i>L->length +1) exit(0);for(j = i;j < L->length;j++)xx{L->data[j-1] = L->data[j];}L->length --;}int main(){Sqlist L;int i;initSqlist(&L);for(i = 0;i < 15;i++){insertSqlist(&L,i+1,i+1);}for(i = 0;i<15;i++)printf("%d ",L.data[i]);putchar(10);''delSqlist(&L,5);for(i = 0;i<15;i++)printf("%d ",L.data[i]);putchar(10);system("pause");return 0;}上⾯代码实现了顺序表的创建。
单向链表单向链表的基本操作,创建一个由6个节点组成的单向链表,显示链表中每个节点的数据,并且做增加、删除、查找节点以及计算单链表的长度等处理。
➢需求分析:1.功能(1)用尾插法创建一带头结点的由6个节点组成的单向链表:从键盘读入一组整数,作为单链表中的元素,输入完第6个结点后结束;将创建好的单链表元素依次输出到屏幕上。
(2)显示链表中每个节点的数据(3)从键盘输入一个数,查找在以上创建的单链表中是否存在该数;如果存在,显示它的位置,即第几个元素;如果不存在,给出相应提示如“No found node!”。
(4)在上述的单链表中的指定位置插入指定数据,并输出单链表中所有数据。
(5)删除上述单链表中指定位置的结点,并输出单链表中所有数据。
(6)求单链表的长度并输出.2.输入要求先输入单链表中结点个数n,再输入单链表中所有数据,在单链表中需查找的数据,需插入的数据元素的位置、值,要删除的数据元素的位置。
3。
测试数据单链表中所有数据:12,23,56,21,8,10在单链表中需查找的数据:56;24插入的数据元素的位置、值:1,28;7,28;0,28要删除的数据元素的位置:6➢概要设计:1.算法思想:由于在操作过程中要进行插入、删除等操作,为运算方便,选用带头结点的单链表作数据元素的存储结构.对每个数据元素,由一个数据域和一个指针域组成,数据域放输入的数据值,指针域指向下一个结点。
2.数据结构:单链表结点类型:typedef struct Liistnode {int data;struct Listnode *next;}NODE;3.模块划分:a)用尾插法建立带头结点的单链表*CreateList函数;b)显示链表中每个结点的数据PrintList函数;c)从键盘输入一个数,查找单链表中是否存在该数FoundList函数;d)在单链表中指定位置插入指定数据并输出单链表中所有数据InsertList函数;e)删除单链表中指定位置的结点并输出单链表中所有数据DeleteList函数;f)计算单链表的长度并在屏幕上输出LengthList函数;g)主函数main(),功能是给出测试数据值,建立测试数据值的带头结点的单链表,调用PrintList函数、FoundList函数、InsertList函数、DeleteList函数、LengthList函数实现问题要求。
数据结构(头插法建立单链表)
假设线性表中结点的数据类型是字符型,逐个输入这些字符,并以换行符‘\n’作为输入结束的条件,使用头插法动态地建立单链表(不带头结点)。
算法思路:
从一个空表开始,重复读入数据,生成新结点,将读入数据存放在新结点的数据域中,然后将新结点插入到当前链表的表头上,直到读入换行符'\n'为止。
具体算法:
LinkList CreatListF(void)
{//头插法建立单链表
DataType ch;
LinkList head;
ListNode *s;
head=NULL;
printf("请输入链表各结点的数据(字符型):\n");
while((ch=getchar())!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode ));
if(s==NULL)
{
printf("申请存储空间失败!");return head;
}
s->data=ch; //输入的数据写入结点数据域s->next=head; //将新结点*s插入链表的前面
head=s; //头指针指向新结点
}
return head;。
链式存储(头插法、尾插法)#include "stdio.h"#include "string.h"#include "ctype.h"#include "stdlib.h"#include "io.h"#include "math.h"#include "time.h"#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define MAXSIZE 20 // 存储空间初始分配量typedef int Status;// Status是函数的类型,其值是函数结果状态代码。
如OK等typedef int ElemType;//ElemType类型依据实际情况⽽定,这⾥如果为intStatus visit(ElemType c){printf("%d ",c);return OK;}typedef struct Node{ElemType data;struct Node *next;}Node;typedef struct Node *LinkList; // 定义LinkList// 初始化顺序线性表Status InitList(LinkList *L){*L=(LinkList)malloc(sizeof(Node)); // 产⽣头结点,并使L指向此头结点if(!(*L)) // 存储分配失败return ERROR;(*L)->next=NULL; //指针域为空return OK;}//初始条件:顺序线性表L已存在。
操作结果:若L为空表,则返回TRUE,否则返回FALSEStatus ListEmpty(LinkList L){if(L->next)return FALSE;elsereturn TRUE;}// 初始条件:顺序线性表L已存在。