#include<stdio.h>
#include<stdlib.h>
typedef char datetype;/*定义新的数据类型名*/
typedef struct node
{
datetype date;
struct node *next;
}listnode;
typedef listnode *linklist;
int delete(linklist h,int num)/*删除结点*/
{
linklist p=h;
listnode *q=NULL;
int i=num;
int n=;
while(n<i)/*寻找删除位置*/
{
q=p;/*该结点和前后两个结点*/
p=p->next;
n++;
}
if(p==NULL)/*该结点不存在*/
printf("No Found Node!");
else
{
q->next=p->next;
free(p);
}
} void output(linklist head)/*链表遍历*/
{
linklist p=head;
while(p!=NULL)
{
printf("%c",p->date);
p=p->next;
}
}
int rear_creat(linklist head,int index0,int m)/*插入新结点,链表头,结点位置,结点date*/
{
linklist k,g;
int i=index0,n;
k=head;
n=;
while(n<i)/*寻找结点位置*/
{
n++;
k=k->next;
}
g=(listnode *)malloc(sizeof(listnode));
g->next=k->next;
g->date=m;
k->next=g;
}
int main()
{
char ch;
int index,index0;
char m;
linklist head=NULL;
listnode *p,*r;
ch=getchar();
while(ch!='\n')
{
p=(listnode *)malloc(sizeof(listnode));
p->date=ch;
if(head==NULL)
head=p;
else
r->next=p;
r=p;
ch=getchar();
}
output(head);
printf("\ndelete a node:\n");
scanf("%d",&index);
if(index==)
head=head->next;
else
delete(head,index);
output(head); printf("\ncreat a node:\n");
scanf("%d%c",&index0,&m);
rear_creat(head,index0,m);
output(head);
return ;
}
#include<stdio.h>/*尾插法*/
#include<stdlib.h>
typedef char datetype;
typedef struct node
{
datetype date;
struct node *next;
}listnode; typedef listnode *linklist;
linklist r=NULL,head=NULL; void rear_creat(datetype ch)
{
linklist p=NULL;
p=(listnode *)malloc(sizeof(listnode));
p->date=ch;
if(head==NULL)
{
head=p;
}
else
{
r->next=p;
}
r=p;
}
void output(linklist head)
{
while(head!=NULL)
{
printf("%c ",head->date);
head=head->next;
} }
void insert_rear(int index,int num)
{
listnode *p=NULL,*u;
u=head;
int n=;
p=(listnode *)malloc(sizeof(listnode));
p->date=index;
while(num>n)
{
u=u->next;
n++;
}
p->next=u->next;
u->next=p;
}
void delete(int num)
{
int n=;
linklist p=head,q;
while(num>n)
{
n++;
q=p;
p=p->next;
}
q->next=p->next;
} int main()
{
datetype index,ch;
int num;
ch=getchar();
while(ch!='\n')
{
rear_creat(ch);
ch=getchar();
}
output(head);
scanf("%c%d",&index,&num);
insert_rear(index,num);
output(head);
scanf("%d",&num);
delete(num);
output(head);
}