以下是我几年前写的一个链表程序,很初级,你可以看看,
#include
#include
#include
typedef struct Node
{
char data;
struct Node * next;
}node;
void Insert(node* );//插入
void Find(node* );//查找
int Count(node*);//链表长度
void Update(node* );//修改
void Delete(node* );//删除
void Show(node* );//输出
int main()
{
int a;
node head;
head.next = NULL;
printf("***********链表的操作************\n\n");
while(1)
{
a = 0;
printf("***********请选择您的操作***********\n\n");
printf("1 链表的插入\t 2 链表的查找\t 3 链表的修改\n4 链表的删除\t 5 链表的输出\t 6 退出系统\n");
scanf("%d",&a);
switch(a)
{
case 1:
Insert(&head);
break;
case 2:
Find(&head);
break;
case 3:
Update(&head);
break;
case 4:
Delete(&head);
break;
case 5:
Show(&head);
break;
case 6:
exit(-1);
break;
default :
printf("输入错误!");
break;
}
}
return 0;
}
int Count(node* head)
{
node* pH = head;
int count = 0;
while (pH->next != NULL )
{
pH = pH->next;
count++;
}
return count;
}
void Insert(node* head )
{
int which = 0;
int i = 0;
int j = 1;
char ch;
node * pH = head;
printf("\n1.首插入 2.未插入 3.插入到位置i\n");
printf("请选择:");
scanf("%d",&which);
ch = getchar();
if (which == 1)
{
printf("请输入值:");
scanf("%c",&ch);
node * q = (node *)malloc(sizeof(Node));
q->data = ch;
q->next = pH->next;
pH->next = q;
}
else if (2 == which)
{
while (pH->next != NULL)
{
pH = pH->next;
}
printf("请输入值:");
scanf("%c",&ch);
node * q = (node *)malloc(sizeof(Node));
q->data = ch;
q->next = pH->next;
pH->next = q;
}
else if ( 3 == which )
{
printf("请输入i的值:");
scanf("%d",&i);
ch = getchar();
if ( (i > 0) && (i <= Count(head) + 1) )
{
printf("i = %d",i);
while (j < i)
{
pH = pH->next;
j++;
}
printf("请输入值:");
scanf("%c",&ch);
node * q = (node *)malloc(sizeof(Node));
q->data = ch;
q->next = pH->next;
pH->next = q;
}
else
{
printf("i输入错误!\n");
}
}
else
{
printf("选择错误!\n");
}
return;
}
void Show(node* pH)
{
printf("链表输出:\n");
if ( pH->next == NULL)
{
printf("链表为空!\n");
return;
}
else
{
while ( pH->next != NULL )
{
pH = pH->next;
printf("%3c",pH->data);
}
printf("\n输出结束!\n");
}
}
void Find(node* head)
{
int which = 0;
int j = 0;
int i = 0;
char ch;
bool is_have = false;
node * q = head->next;
if ( Count(head) == 0 )
{
printf("链表为空!无法查找.\n");
return;
}
printf(" 1.查找内容的位置 2.查找位置的内容\n");
scanf("%d",&which);
ch = getchar();
if (1 == which)
{
printf("请输入要查找的内容:");
scanf("%c",&ch);
while ( q != NULL)
{
j++;
if ( q->data == ch)
{
printf("%c是第%d个。\n",ch,j);
is_have = true;
}
q = q->next;
}
if ( is_have == false )
{
printf("所查找的内容在链表中不存在!");
}
}
else if ( 2 == which )
{
j = 0;
printf("请输入要查找的位置:");
scanf("%d",&i);
if ( i > Count(head) || i < 1 )
{
printf("位置错误!无法查找。\n");
return;
}
while ( q != NULL && j < i-1 )
{
q = q->next;
j++;
}
printf("内容为:%c",q->data);
}
else
{
printf("选择错误!\n");
}
return;
}
void Update(node* head)
{
node * q = head->next;
int i = 0;
int j = 0;
char ch;
if ( Count(head) == 0 )
{
printf("链表为空!无法查找.\n");
return;
}
printf("请输入要修改的位置:");
scanf("%d",&i);
ch = getchar();
if ( i > Count(head) || i < 1 )
{
printf("位置错误!无法修改。\n");
return;
}
printf("请输入修该的值:");
scanf("%c",&ch);
while ( q != NULL && j < i-1 )
{
q = q->next;
j++;
}
q->data = ch;
printf("修改成功!\n");
return;
}
void Delete(node* head)
{
node * q = head->next;
node * p = head;
int i = 0;
int j = 0;
char ch;
if ( Count(head) == 0 )
{
printf("链表为空!无法删除.\n");
return;
}
printf(" 1.全部删除 2.删除单个\n");
scanf("%d",&i);
ch = getchar();
if ( 1 == i)
{
while( q != NULL )
{
p = p->next;
q = q->next;
free(p);
}
head->next = NULL;
printf("释放成功!\n");
}
else if ( 2 == i )
{
printf("请输入要删除的位置:");
scanf("%d",&i);
ch = getchar();
if ( i > Count(head) || i < 1 )
{
printf("位置错误!无法删除。\n");
return;
}
while ( q != NULL && j < i-1 )
{
p = p->next;
q = q->next;
j++;
}
p->next = q->next;
free(q);
printf("删除成功!\n");
}
else
{
printf("选择错误!\n");
}
}