带头结点和不带头结点的尾插法建立双链表
一.带头结点的
先介绍存储数据类型为字符型的
//编程建立双链表(data为字符型数据)13-2-1ex3.cpp
//带头结点的双链表
#include<stdio.h>
#include<stdlib.h>
//#include<string.h>
//#include<conio.h>
//#define E_DEBUG
#ifdef E_DEBUG
#define PRINT printf
#else
#define PRINT(...) do{}while(0)
#endif
typedef char datatype;
typedef struct student
{
datatype data;
struct student *next;
struct student *pre;
}DNode;
DNode *create()
{
DNode *head, *p, *s;
datatype x, cycle=1;
head=(DNode *)malloc(sizeof(DNode));
p=head; //如果换成带头节点的链表。这里也不能变,切记,不能变成p=head->next;那就错了
printf("\nplease input the data:\n");
while(cycle)
{
scanf("%c",&x); //见后面注意2)
if(x!='#')
{
s=(DNode *)malloc(sizeof(DNode));
s->data=x;
PRINT("\n %c", s->data);