双链表的建立,插入数据。
分三个部分解读:
头文件:
#ifndef _Linklist1_H
#define _Linklist1_H
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10004
#define FALSE 10003
typedef int ElemType;
struct node
{
ElemType data;
struct node *next; //双向链表要定义头和尾两个指针域
struct node *prior;
};
typedef struct node Node;
int doublelinkinit(Node **s);
int doublelinkinsert(Node *s, int p, ElemType e);
int doublelinktrver(Node *s);
int delete(Node *s, int n, ElemType *e);
#endif
主函数:
#include "Linklist1.h"
#include <stdio.h>
int main()
{
int ret, i;
Node *list;
srand(time(0));
ret = doublelinkinit(&list);
if(ret == SUCCESS)
{
printf("INIT SUCCESS!\n");
}
else
{
printf("INIT FAILURE!\n");
}
for(i = 0; i < 10; i++)
{
ret = doublelinkinsert(list, i + 1, rand() % 20);
if(ret == SUCCESS)
{
printf("INSERT SUCCESS!\n");
}
else
{
printf("INSERT FAILURE!\n");
}
}
ret = doublelinktrver(list);
if(ret == SUCCESS)
{
printf("\nTRVER SUCCESS!\n");
}
else
{
printf("\nTRVER FAAILURE!\n");
}
ElemType e;
int p = 3;
ret = delete(list, p, &e);
if(ret == SUCCESS)
{
printf("delete %d success!\n",e);
}
else
{
printf("delete %d failure!\n",e);
}
return 0;
}
子函数:
#include "Linklist1.h"
#include <stdio.h>
#include <stdlib.h>
int doublelinkinit(Node **s)
{
(*s) = (Node *)malloc(sizeof(Node) * 1);
if(s == NULL)
{
return FAILURE;
}
(*s)->next = NULL; //一个结点的两个指针域都要置空
(*s)->prior = NULL;
return SUCCESS;
}
int doublelinkinsert(Node *s, int n, ElemType e)
{
Node *p = s;
int k = 1;
if(s == NULL)
{
return FAILURE;
}
while(k < n && p != NULL)
{
p = p->next;
k++;
}
if(k > n || p == NULL)
{
return FAILURE;
}
Node *q = (Node *)malloc(sizeof(Node) * 1);
if(q == NULL)
{
return FAILURE;
}
q->data = e; //双向链表的插入,需要判断是否在尾结点后插入
q->next = p->next;
q->prior = p;
p->next = q;
if(q->next != NULL)
{
q->next->prior = q;
}
return SUCCESS;
}
int doublelinktrver(Node *s)
{
Node *p = s;
if(s == NULL)
{
return FAILURE;
}
while(p->next)
{
p = p->next;
printf("%d ",p->data);
}
return SUCCESS;
}
int delete(Node *s, int n, ElemType *e)
{
if(s == NULL)
{
return FAILURE;
}
Node *p = s;
int k = 1;
while(k < n && p != NULL)
{
p = p->next;
k++;
}
Node *q = p->next;
*e = q->data;
p->next = q->next;
if(q->next != NULL) //删除时同样要判断是否删除最后一个结点
{
q->next->prior = p;
}
free(q); //释放结点
return SUCCESS;
}
单链表的头插法和尾插法:
函数主体:
#include "danlianbiao.h"
#include <stdio.h>
int main()
{
Node *list = NULL; //先初始化单链表
int ret, i;
ret = danlianbiaoinit(&list);
if(ret == SUCCESS)
{
printf("INIT SUCCESS!\n");
}
else
{
printf("INIT FAILURE!\n");
}
ElemType e;
for(i = 0; i < 10; i++)
{
scanf("%d", &e);
ret = danlianbiaoinsert(list, e);
if(ret == SUCCESS)
{
printf("insert success!\n");
}
else
{
printf("insert failure!\n");
}
}
ret = danlianbiaotrver(list);
if(ret == SUCCESS)
{
printf("\n");
}
else
{
printf("trver failure!\n");
}
return 0;
}
头文件:
#ifndef _LINKLIST_H
#define _LINKLIST_H
#define SUCCESS 10000
#define FAILURE 10001
#define TRUE 10003
#define FALSE 10004
typedef int ElemType;
struct node
{
ElemType data;
struct node *next;
};
typedef struct node Node;
int danlianbiaoinit(Node **s);
int danlianbiaoinsert(Node *s, ElemType e);
int danlianbiaotrver(Node *s);
#endif
子函数部分:
#include "danlianbiao.h"
#include <stdlib.h>
#include <stdio.h>
int danlianbiaoinit(Node **s)
{
(*s) = (Node *)malloc(sizeof(Node) * 1);
if(NULL == (*s))
{
return FAILURE;
}
(*s)->next = NULL;
return SUCCESS;
}
#if 0 //这是尾插法,先遍历到最后一个结点,然后分配空间插入
int danlianbiaoinsert(Node *s, ElemType e)
{
Node *p = s;
if(s == NULL)
{
return FAILURE;
}
while(p->next)
{
p = p->next;
}
Node *q = (Node *)malloc(sizeof(Node) * 1);
q->data = e;
q->next = p->next;
p->next = q;
return SUCCESS;
}
#endif
int danlianbiaoinsert(Node *s, ElemType e)//头插法直接在头结点后插入
{
Node *p = s;
if(s == NULL)
{
return FAILURE;
}
Node *q = (Node *)malloc(sizeof(Node) * 1);
q->data = e;
q->next = p->next;
p->next = q;
return SUCCESS;
}
int danlianbiaotrver(Node *s)
{
Node *p = s;
if(s == NULL)
{
return FAILURE;
}
while(p->next)
{
p = p->next;
printf("%d ", p->data);
}
return SUCCESS;
}