这个是我用双链表写的.
链表结构采用了<HEAD> - <unit1> - <unit2> - ... - <unitn> - <END> 的结构
神vlinux飘飘
*/
#include <stdio.h>
/*基本单元character*/
struct character
{
char c;
struct character *front;
struct character *next;
};
struct character *head; /*链表表头*/
struct character *end; /*链表表尾*/
struct character *point; /*操作指针*/
void init(); /*链表初始化函数*/
void show(); /*链表全体单元输出函数*/
void del(struct character*); /*链表删除单元函数*/
/*按照字符查找链表单元函数.如果该字符存在返回该字符单元的地址,如果不存在则返回NULL*/
struct character *search(char ch);
int main(void)
{
struct character *temp;
char cmd,x;
init();/*初始化*/
/*程序开始*/
do{
show();
printf("\ninput deleted letter:");
scanf("%c%c",&cmd,&x);
if( cmd=='0' )
break;
if( (temp=search(cmd))==NULL)
printf("\nError, the letter didn't exist!\n");
else
del(temp);
}while(1);
}
void init()
{
struct character *temp;
int i;
/*初始化链表表头和表尾*/
head = (struct character*)malloc(sizeof(struct character));
end = (struct character*)malloc(sizeof(struct character));
head->front = NULL;
end->next = NULL;
head->next = end;
end->front = head;
/*初始化链表26个单元,分配相应字母给各个单元*/
point = head;
for(i=0;i<26;i++)
{
temp = (struct character*)malloc(sizeof(struct character));
temp->c = 'z'-i;
temp->front = point;
temp->next = point->next;
point->next->front = temp;
point->next = temp;
}
}
void show()
{
/*遍历输出所有单元,除了头单元(表头)和尾单元(表尾)*/
point = head;
while(point->next != NULL)
{
printf("%c ",point->c);
point = point->next;
}
}
struct character *search(char ch)
{
/*遍历查询该字符所对应的单元*/
point = head;
while(point->next != NULL)
if(point->c == ch)
return point;
else
point = point->next;
return NULL;
}
void del(struct character *temp)
{
/*删除链表*/
temp->front->next = temp->next;
temp->next->front = temp->front;
free(temp);
}
淘宝杜琨