#include<stdio.h>
#include<stdlib.h>
typedef struct node* PNode;
struct node
{
char data;
struct node*next;
};
typedef struct node *LinkList;
LinkList createmptynull()//创建空链表
{
LinkList list=(LinkList)malloc(sizeof(struct node));
if(list==NULL)
{printf("创建失败!");
return NULL;
}
else
list->next=NULL;
return list;
}
/*输出链表*/
LinkList Print(LinkList L)
{
LinkList P;
int i,j;
PNode q;
P=L;
j=2;
printf("请输入数据\n");
q=(PNode)malloc(sizeof(struct node));
if(q!=NULL)
{ {printf("请输入第一节点数据\n");/*该节点数据不为1 */
scanf("%c",&q->data);
}
P->next=q;
for(P=q;q->data!=-1;)
{q=(PNode)malloc(sizeof(struct node));
if(q!=NULL)
{
scanf("%c",&q->data);
printf("第%d个结点数据为%c\n",j++,q->data );
P->next=q;
}
}
}
return L;
}
/*删除P结点*/
/*在p所指结点后插入x*/
LinkList insertLink(LinkList L,PNode p,int x)
{
PNode q=(PNode)malloc(sizeof(struct node));
if(q!=NULL)
{q->data=x;
p->next=q;
}
else
printf("插入失败\n");
return L;
}
/*删除结点p*/
LinkList deletlink(LinkList L,PNode p)
{LinkList q=L;
while(q->next!=NULL)
q=q->next;
q->next=p->next;
free(p);
return L;
}
/*在单恋表中求p所指节点的前去节点*/
PNode find(LinkList list,PNode P)
{
LinkList q;
q=list;
if(q==NULL)
return NULL;
else
while(q!=NULL&&q->next!=P)
q=q->next;
return q;
}
/*求单链表中p的后驱结点*/
PNode qiuhouqujiedian(LinkList L,PNode P)
{
LinkList q;
q=L;
while(q!=NULL&&q!=P)
q=q->next;
return q->next;
}
/*删除值为x的结点*/
LinkList detennode(LinkList L)
{
char x;
LinkList p=L;
printf("请输入想要删除的x的值\n");
scanf("%c",&x);
while(p!=NULL)
{
if(p->next->data=x)
p->next=p->next->next;
p=p->next;
}
return L;
}
/*在单链表中找到值为X的在链表,并返回该结点*/
PNode locate_link(LinkList List,int x)
{
PNode p;
LinkList q=List;
printf("请输入想要查找的x的值!\n");
scanf("%c",&x);
if(List==NULL)
return NULL;
while(q!=NULL&&q->data!=x)
q=q->next;
return List;
}
/*求链表的长度并返回该链表的长度*/
int length(LinkList L)
{
int i=0;
LinkList p=L;
if(p==NULL)
return -1;
while(p)
{
i++;
p=p->next;
}
return i;
}
/*设计算法从表中删除第i个节点开始后的k个元素,i>0*/
void detemanypnode(LinkList L,int i,int k)
{
PNode q;
int j=1;
LinkList p=L;
if(i<1||i>length(L)+k)
printf("删除失败\n");
else
{
if(i+k>length(L))
k=length(L)-i;
while(j<i)
{ p=p->next;
j++;
}
while(j<k)
{
p->next=q;
p->next=p->next->next;
free(q);
p=p->next;
j++;
}
}
}
/*创建链表的尾插法*/
LinkList creatlist()
{
PNode list=(PNode)malloc(sizeof(struct node));
if(list==NULL)
{
printf("创建失败!\n");
return NULL;
}
else
{
PNode p=(PNode)malloc(sizeof(struct node));
if(p!=NULL)
{
{
list->next=p;
p->next=NULL;
}
while(p)
{
PNode q=(PNode)malloc(sizeof(struct node));
if(q!=NULL)
{
scanf("%c",&q->data);
q->next=p->next;
list->next=q;
p=q;
}
}
return list;
} }
}
/*创建链表的头插发*/
LinkList creatllist()
{
PNode list=(PNode)malloc(sizeof(struct node));
if(list!=NULL)
{
PNode p=(PNode)malloc(sizeof(struct node));
if(p!=NULL)
{
printf("请输入该结点数据:\n");
scanf("%c",&p->data);
list->next=p;
p->next=NULL;
while(p)
{
PNode q=(PNode)malloc(sizeof(struct node));
if(q!=NULL)
{
scanf("%c",&q->data);
q->next=p->next;
p->next=q;
q=p;
}
}
return list;
}
}
else
return NULL;
}
int main()
{
char x;
PNode q,P;
LinkList L=createmptynull();
L=Print(L);
printf("该链表的长度为%d",length(L));
printf("q请输入需要查找的数x\n");
scanf("%c",&x);
P=locate_link(L,x);
detennode(L);
q=qiuhouqujiedian(L,P);
}