#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
} SqList;
void InitList(SqList *&L) //建立链表
{
L=(SqList*)malloc(sizeof(SqList));
L->next=NULL;
}
void Insert(SqList *&L,ElemType n) //插入n
{
SqList *p=L,*q;
while(p->next!=NULL)
p=p->next;
q=p;
p=(SqList*)malloc(sizeof(SqList));
q->next=p;
p->data=n;
p->next=NULL;
}
void Print(SqList *L) //输出链表
{
SqList *p=L->next;
while(p->next!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
printf("%c\n",p->data);
}
void PrintLength(SqList *L) //输出长度
{
int i=0;
SqList *p=L->next;
while(p!=NULL)
{
i++;
p=p->next;
}
printf("%d\n",i);
}
bool SqNull(SqList *L) //判断是否为空
{
if(L->next!=NULL)return true;
else return false;
}
void PrintData(SqList *L,int n) //输出第n个数据
{
SqList *p=L->next;
int i;
for(i=0; i<n-1&&p->next!=NULL; i++)
p=p->next;
printf("%c\n",p->data);
}
int Find(SqList *L,ElemType a) //找到a所在的位置
{
SqList *p=L->next;
for(int i=0; p!=NULL; i++)
if(p->data==a)return i+1;
return 0;
}
void Insertinto(SqList *&L,int n,ElemType f) //把f插入到n位置
{
SqList *p=L->next,*q;
for(int i=1; p!=NULL; i++)
{
if(i==n-1)
{
q=(SqList*)malloc(sizeof(SqList));
q->data=f;
q->next=p->next;
p->next=q;
}
p=p->next;
}
}
void Delete(SqList *&L,int n) //删除链表
{
SqList *p=L->next,*q;
for(int i=1; p!=NULL; i++)
{
if(i==n-1)
{
q=p->next;
p->next=p->next->next;
free(q);
}
p=p->next;
}
}
#include <stdlib.h>
typedef char ElemType;
typedef struct Node
{
ElemType data;
struct Node *next;
} SqList;
void InitList(SqList *&L) //建立链表
{
L=(SqList*)malloc(sizeof(SqList));
L->next=NULL;
}
void Insert(SqList *&L,ElemType n) //插入n
{
SqList *p=L,*q;
while(p->next!=NULL)
p=p->next;
q=p;
p=(SqList*)malloc(sizeof(SqList));
q->next=p;
p->data=n;
p->next=NULL;
}
void Print(SqList *L) //输出链表
{
SqList *p=L->next;
while(p->next!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
printf("%c\n",p->data);
}
void PrintLength(SqList *L) //输出长度
{
int i=0;
SqList *p=L->next;
while(p!=NULL)
{
i++;
p=p->next;
}
printf("%d\n",i);
}
bool SqNull(SqList *L) //判断是否为空
{
if(L->next!=NULL)return true;
else return false;
}
void PrintData(SqList *L,int n) //输出第n个数据
{
SqList *p=L->next;
int i;
for(i=0; i<n-1&&p->next!=NULL; i++)
p=p->next;
printf("%c\n",p->data);
}
int Find(SqList *L,ElemType a) //找到a所在的位置
{
SqList *p=L->next;
for(int i=0; p!=NULL; i++)
if(p->data==a)return i+1;
return 0;
}
void Insertinto(SqList *&L,int n,ElemType f) //把f插入到n位置
{
SqList *p=L->next,*q;
for(int i=1; p!=NULL; i++)
{
if(i==n-1)
{
q=(SqList*)malloc(sizeof(SqList));
q->data=f;
q->next=p->next;
p->next=q;
}
p=p->next;
}
}
void Delete(SqList *&L,int n) //删除链表
{
SqList *p=L->next,*q;
for(int i=1; p!=NULL; i++)
{
if(i==n-1)
{
q=p->next;
p->next=p->next->next;
free(q);
}
p=p->next;
}
}