#include <stdio.h>
#include <stdlib.h>
/************************************************************************/
/* 以下为C语言实现单链表,双链表,循环链表
/************************************************************************/
//单链表定义
typedef char Elemplment;
#define LEN sizeof(Lnode)
typedef struct Node
{
Elemplment data;
struct Node* next;
}Lnode;
// 带头节点的创建在头部插入
Lnode* creatHeader()
{
Lnode *p,*head;
head = (Lnode *)malloc(LEN);
head->next = NULL;
while(1)
{
p = (Lnode *)malloc(LEN);
scanf("%c",&p->data);
p->next = head->next;
head->next = p;
printf("");
if (getchar()=='n')
{
break;
}
}
return head;
}
// 带头节点的在尾部插入
Lnode* creatTail()
{
Lnode *p,*tail,*head;
tail = head =(Lnode*)malloc(LEN);
head->next = NULL;
while (1)
{
p = (Lnode *)malloc(LEN);
scanf("%c",&p->data);
tail->next = p;
tail = p;
p->next = NULL;
printf("");
if (getchar() == 'n')
{
break;
}
}
return head;
}
// 得到长度
int getLenth(Lnode *head)
{
int j= 0;
Lnode *p = head;
while(p!=NULL)
{
j++;
p= p->next;
}
return j;
}
// 在一个位置插入 一个元素
int insertLocation(Lnode*head,int i,Elemplment x)
{
int j=1;
Lnode *p,*s;
p=head->next;
while(p!=NULL&& j<i)
{
j++;
p = p->next;
}
if(j==i)
{
s = (Lnode *)malloc(LEN);
s->data = x;
s->next = p->next;
p->next = s;
return 1;
}
else
return 0;
}
//通过位置删除一个元素
int deleteLocation(Lnode* head,int i)
{
Lnode *p,*q;
int j=1;
p = head->next;
while(p!=NULL&& j<i-1)
{
p++;
p = p->next;
}
if (j<i-1)
{
return 0;
}
else if (p->next =NULL)
{
return 0;
}
q = p->next;
p->next = q->next;
free(q);
return 1;
}
// 查找元素返回节点指针 用数据
Lnode * Locate(Lnode* head,Elemplment x)
{
Lnode* p;
p = head;
while (p!=NULL&&p->data ==x)
{
p = p->next;
}
return p;
}
//查找元素返回节点指针 用位置
Lnode * LocateWithLocation(Lnode* head,int i)
{
int j = 1;
Lnode *p;
p = head->next;
while(p!=NULL&& j<i)
{
j++;
p = p->next;
}
if (i==j)
{
return p;
}
else
return NULL;
}
// 单循环链表初始化空表
Lnode* initCirculate(Lnode *head)
{
head= (Lnode *)malloc(LEN);
head->next = head;
return head;
}
// 单链表初始化空表
int isEmpty(Lnode *head)
{
if (head->next = head)
{
return 0;
}
else
return 1;
}
//遍历单链表
void view (Lnode* head)
{
Lnode *p;
p = head->next;
while(p!=head)
{
putchar(p->data);
p = p->next;
}
}
//循环双链表定义
#define LENDOUBLE sizeof(DouCirList)
typedef struct DouCirList
{
Elemplment data;
DouCirList *next,*prior;
}LDouCirList;
//初始化一个空双循环链表
LDouCirList* initLDouCirList(LDouCirList* head)
{
head = (LDouCirList*)malloc(LENDOUBLE);
head->next = head;
head->prior =head;
return head;
}
//双循环链表建立
LDouCirList* initLDouCirListWithData(LDouCirList* head)
{
LDouCirList *p;
head = (LDouCirList*)malloc(LENDOUBLE);
head->next =head;
head->prior =head;
while(1)
{
p =(LDouCirList*)malloc(LENDOUBLE);
scanf("%c",&p->data);
p->next=head->next;
head->next= p;
p->prior =head;
p->next->prior = p;
printf("");
if (getchar()=='n')
{
break;
}
}
return head;
}
void main ()
{
printf("hello world");
}