C语言单链表的实现
2013-12-24 10:21
295 查看
//
// main.c
// gfhjhgdf
//
// Created by chenhao on 13-12-23.
// Copyright (c) 2013年 chenhao. All rights reserved.
//
#include"stdio.h"
#include <stdlib.h>
typedefstruct List_Node{
int info;
struct List_Node *next;
}node;
//链表长度
int Count_Node(node *head)
{
node *p;
int num = 0;
if (head == NULL)
{
return 0;
}
p = head->next;
while(p!=NULL)
{
num++;
p = p->next;
}
return num;
}
//按值查找
void FindByValue(node *head, int v)
{
node *p;
int i = 0, num = 0;
if (head == NULL)
{
return;
}
p = head->next;
while(p != NULL)
{
i++;
if (p->info == v)
{
num++;
printf("%d ",i);
}
p = p->next;
}
printf("找到%d个等于%d的值",num,v);
}
//插入元素
void InsertItem(node *head, int loc, int num)
{
node *p, *pre, *s;
int i = 0;
if (loc > Count_Node(head)||loc < 0)
{
printf("插入位置错误\n");
return;
}
if (head == NULL)
{
return;
}
pre = head;
p = head->next;
while(i != loc)
{
i++;
pre = p;
p = p->next;
}
s = (node*)malloc(sizeof(node));
s->info = num;
s->next = p;
pre->next = s;
}
//删除元素
int deleteItem(node *head, int loc)
{
node *p, *pre;
int i=1, num=0;
if (loc>Count_Node(head)||loc<=0)
{
printf("删除位置错误\n");
return0;
}
if (head == NULL)
{
return0;
}
pre = head;
p = pre->next;
while(i!=loc)
{
i++;
pre = p;
p=p->next;
}
num = p->info;
pre->next = p->next;
p->next = NULL;
free(p);
return num;
}
//清空链表
void FlushLink(node *head)
{
node *p;
if (head == NULL)
{
return;
}
p = head->next;
head->next = NULL;
free(p);
}
//销毁链表
void DestoryLink(node *head)
{
free(head);
}
//按序号查找
int FindByNo(node *head, int loc)
{
node *p;
int i=1;
if (loc>Count_Node(head)||loc<=0)
{
printf("位置错误\n");
return0;
}
if (head == NULL)
{
return0;
}
p = head->next;
while(i!=loc)
{
i++;
p=p->next;
}
return p->info;
}
//尾插法建立带头结点的单链表
node* Creat_Node()
{
node *head,*pre,*p;
int x;
head=(node*)malloc(sizeof(node));;
head->next=NULL;
pre=head;
printf("输入各结点的值,以0结束:");
while(EOF!=(scanf("%d",&x))&&x!=0)
{
p=(node*)malloc(sizeof(node));
p->info=x;
p->next=pre->next;
pre->next=p;
pre=pre->next;
}
return head;
}
//头插法建立带头结点的单链表
node* Build_Node()
{
node *head,*p;
int x;
head=(node*)malloc(sizeof(node));;
head->next=NULL;
printf("输入各结点的值,以0结束:");
while(EOF!=(scanf("%d",&x))&&x!=0)
{
p=(node*)malloc(sizeof(node));
p->info=x;
p->next=head->next;
head->next=p;
}
return head;
}
//在Y前插入X
void Before_y_Insert_x(node* head,int y,int x)
{
node *pre,*p,*s;
if (head ==NULL)
{
return;
}
pre=head;
p=pre->next;
while(p&&p->info!=y)
{
pre=p;
p=p->next;
}
if(p==NULL)
{
printf("error!%d不在该链表中\n",y);
}
else
{
s=(node*)malloc(sizeof(node));
s->info=x;
s->next=p;
pre->next=s;
}
}
//判断链表是否有序
int Is_Sort(node *head)
{
node *p,*pre;
int flag;
if (head ==NULL)
{
return0;
}
pre=head->next;
p=pre->next;
flag=pre->info>p->info?1:0;
while(p)
{
pre=p;
p=p->next;
if(p)
{
if(flag!=pre->info>p->info?1:0)
{
return 0;
}
}
}
return1;
}
//链表反序
void convert_Node(node *head)
{
node *pre,*p=head->next;
if (head ==NULL)
{
return;
}
head->next=NULL;
while(p)
{
pre=p;
p=p->next;
pre->next=NULL;
pre->next=head->next;
head->next=pre;
}
}
//打印单链表
void Print_Node(node *head)
{
node *p;
if (head ==NULL)
{
return;
}
p=head->next;
printf("输出该链表:");
while(p)
{
printf("%-5d--->",p->info);
p=p->next;
}
if(p==NULL)
{
printf("^\n\n\n");
}
}
void menu()
{
printf(" 1:尾插法创建带头节点的链表\n");
printf(" 2:头插法创建带头节点的链表\n");
printf(" 3:链表长度\n");
printf(" 4:在Y前插入X \n");
printf(" 5:判断链表是否有序\n");
printf(" 6:链表反序 \n");
printf(" 7:按值查找\n");
printf(" 8:插入元素\n");
printf(" 9:删除元素\n");
printf(" 10:按序号查找\n");
printf(" 11:清空链表\n");
printf(" 12:销毁链表\n");
printf(" 13:打印链表\n");
printf(" 14:退出\n");
}
int main()
{
int goon = 1;
node *head=NULL;//*head1=NULL,*head2=NULL;
int x=0, y=0;
//int flag = 0;
int choice;
menu();
while(goon)
{
printf("请选择:\n");
scanf("%d",&choice);
switch (choice)
{
case 0:
menu();
break;
case 1:
printf("尾插法创建带头节点的链表\n");
head=Creat_Node();
break;
case 2:
printf("头插法创建带头节点的链表\n");
head=Creat_Node();
break;
case 3:
printf("链表长度\n");
printf("链表长度为%d\n",Count_Node(head));
break;
case 4:
printf("在Y前插入X\n请输入X和Y:\n");
scanf("%d%d",&x,&y);
Before_y_Insert_x(head,y,x);
break;
case 5:
printf("判断链表是否有序\n");
if(Is_Sort(head)==1)
{
printf("该链表有序!\n");
}
else
{
printf("该链表无序!\n");
}
break;
case 6:
printf("链表反序\n");
convert_Node(head);
break;
case 7:
printf("按值查找\n请输入要查找的值\n");
scanf("%d",&x);
FindByValue(head,x);
break;
case 8:
printf("插入元素\n请输入插入的位置(0到链表长度之间)和元素");
scanf("%d%d",&x,&y);
InsertItem(head,x,y);
break;
case 9:
printf("删除元素\n请输入删除元素的位置(1到链表长度之间)");
scanf("%d",&x);
printf("删除的元素为%d",deleteItem(head,x));
break;
case 10:
printf("按序号查找\n请输入要查找的序号\n");
scanf("%d",&x);
printf("找到的值为%d",FindByNo(head,x));
break;
case 11:
printf("清空链表\n");
FlushLink(head);
break;
case 12:
printf("销毁链表\n");
DestoryLink(head);
break;
case 13:
Print_Node(head);
break;
case 14:
printf("Exit!");
goon = 0;
break;
default:
break;
}
}
}
// main.c
// gfhjhgdf
//
// Created by chenhao on 13-12-23.
// Copyright (c) 2013年 chenhao. All rights reserved.
//
#include"stdio.h"
#include <stdlib.h>
typedefstruct List_Node{
int info;
struct List_Node *next;
}node;
//链表长度
int Count_Node(node *head)
{
node *p;
int num = 0;
if (head == NULL)
{
return 0;
}
p = head->next;
while(p!=NULL)
{
num++;
p = p->next;
}
return num;
}
//按值查找
void FindByValue(node *head, int v)
{
node *p;
int i = 0, num = 0;
if (head == NULL)
{
return;
}
p = head->next;
while(p != NULL)
{
i++;
if (p->info == v)
{
num++;
printf("%d ",i);
}
p = p->next;
}
printf("找到%d个等于%d的值",num,v);
}
//插入元素
void InsertItem(node *head, int loc, int num)
{
node *p, *pre, *s;
int i = 0;
if (loc > Count_Node(head)||loc < 0)
{
printf("插入位置错误\n");
return;
}
if (head == NULL)
{
return;
}
pre = head;
p = head->next;
while(i != loc)
{
i++;
pre = p;
p = p->next;
}
s = (node*)malloc(sizeof(node));
s->info = num;
s->next = p;
pre->next = s;
}
//删除元素
int deleteItem(node *head, int loc)
{
node *p, *pre;
int i=1, num=0;
if (loc>Count_Node(head)||loc<=0)
{
printf("删除位置错误\n");
return0;
}
if (head == NULL)
{
return0;
}
pre = head;
p = pre->next;
while(i!=loc)
{
i++;
pre = p;
p=p->next;
}
num = p->info;
pre->next = p->next;
p->next = NULL;
free(p);
return num;
}
//清空链表
void FlushLink(node *head)
{
node *p;
if (head == NULL)
{
return;
}
p = head->next;
head->next = NULL;
free(p);
}
//销毁链表
void DestoryLink(node *head)
{
free(head);
}
//按序号查找
int FindByNo(node *head, int loc)
{
node *p;
int i=1;
if (loc>Count_Node(head)||loc<=0)
{
printf("位置错误\n");
return0;
}
if (head == NULL)
{
return0;
}
p = head->next;
while(i!=loc)
{
i++;
p=p->next;
}
return p->info;
}
//尾插法建立带头结点的单链表
node* Creat_Node()
{
node *head,*pre,*p;
int x;
head=(node*)malloc(sizeof(node));;
head->next=NULL;
pre=head;
printf("输入各结点的值,以0结束:");
while(EOF!=(scanf("%d",&x))&&x!=0)
{
p=(node*)malloc(sizeof(node));
p->info=x;
p->next=pre->next;
pre->next=p;
pre=pre->next;
}
return head;
}
//头插法建立带头结点的单链表
node* Build_Node()
{
node *head,*p;
int x;
head=(node*)malloc(sizeof(node));;
head->next=NULL;
printf("输入各结点的值,以0结束:");
while(EOF!=(scanf("%d",&x))&&x!=0)
{
p=(node*)malloc(sizeof(node));
p->info=x;
p->next=head->next;
head->next=p;
}
return head;
}
//在Y前插入X
void Before_y_Insert_x(node* head,int y,int x)
{
node *pre,*p,*s;
if (head ==NULL)
{
return;
}
pre=head;
p=pre->next;
while(p&&p->info!=y)
{
pre=p;
p=p->next;
}
if(p==NULL)
{
printf("error!%d不在该链表中\n",y);
}
else
{
s=(node*)malloc(sizeof(node));
s->info=x;
s->next=p;
pre->next=s;
}
}
//判断链表是否有序
int Is_Sort(node *head)
{
node *p,*pre;
int flag;
if (head ==NULL)
{
return0;
}
pre=head->next;
p=pre->next;
flag=pre->info>p->info?1:0;
while(p)
{
pre=p;
p=p->next;
if(p)
{
if(flag!=pre->info>p->info?1:0)
{
return 0;
}
}
}
return1;
}
//链表反序
void convert_Node(node *head)
{
node *pre,*p=head->next;
if (head ==NULL)
{
return;
}
head->next=NULL;
while(p)
{
pre=p;
p=p->next;
pre->next=NULL;
pre->next=head->next;
head->next=pre;
}
}
//打印单链表
void Print_Node(node *head)
{
node *p;
if (head ==NULL)
{
return;
}
p=head->next;
printf("输出该链表:");
while(p)
{
printf("%-5d--->",p->info);
p=p->next;
}
if(p==NULL)
{
printf("^\n\n\n");
}
}
void menu()
{
printf(" 1:尾插法创建带头节点的链表\n");
printf(" 2:头插法创建带头节点的链表\n");
printf(" 3:链表长度\n");
printf(" 4:在Y前插入X \n");
printf(" 5:判断链表是否有序\n");
printf(" 6:链表反序 \n");
printf(" 7:按值查找\n");
printf(" 8:插入元素\n");
printf(" 9:删除元素\n");
printf(" 10:按序号查找\n");
printf(" 11:清空链表\n");
printf(" 12:销毁链表\n");
printf(" 13:打印链表\n");
printf(" 14:退出\n");
}
int main()
{
int goon = 1;
node *head=NULL;//*head1=NULL,*head2=NULL;
int x=0, y=0;
//int flag = 0;
int choice;
menu();
while(goon)
{
printf("请选择:\n");
scanf("%d",&choice);
switch (choice)
{
case 0:
menu();
break;
case 1:
printf("尾插法创建带头节点的链表\n");
head=Creat_Node();
break;
case 2:
printf("头插法创建带头节点的链表\n");
head=Creat_Node();
break;
case 3:
printf("链表长度\n");
printf("链表长度为%d\n",Count_Node(head));
break;
case 4:
printf("在Y前插入X\n请输入X和Y:\n");
scanf("%d%d",&x,&y);
Before_y_Insert_x(head,y,x);
break;
case 5:
printf("判断链表是否有序\n");
if(Is_Sort(head)==1)
{
printf("该链表有序!\n");
}
else
{
printf("该链表无序!\n");
}
break;
case 6:
printf("链表反序\n");
convert_Node(head);
break;
case 7:
printf("按值查找\n请输入要查找的值\n");
scanf("%d",&x);
FindByValue(head,x);
break;
case 8:
printf("插入元素\n请输入插入的位置(0到链表长度之间)和元素");
scanf("%d%d",&x,&y);
InsertItem(head,x,y);
break;
case 9:
printf("删除元素\n请输入删除元素的位置(1到链表长度之间)");
scanf("%d",&x);
printf("删除的元素为%d",deleteItem(head,x));
break;
case 10:
printf("按序号查找\n请输入要查找的序号\n");
scanf("%d",&x);
printf("找到的值为%d",FindByNo(head,x));
break;
case 11:
printf("清空链表\n");
FlushLink(head);
break;
case 12:
printf("销毁链表\n");
DestoryLink(head);
break;
case 13:
Print_Node(head);
break;
case 14:
printf("Exit!");
goon = 0;
break;
default:
break;
}
}
}
相关文章推荐
- 单链表的C语言简单实现
- 单链表相关操作--C语言实现
- C语言实现单链表
- C语言实现单链表的逆序打印(带头结点)
- C语言实现单链表的简单操作【完整版】
- 有序单链表的插入 C语言实现
- 【C语言】用递归和循环两种方法实现单链表倒置
- 数据结构中单链表的实现+单链表的C语言实现源代码
- C语言单链表的建立,查找,添加,删除,修改功能实现
- 链表的C语言实现之单链表的实现
- C语言实现单链表面试题--基础篇
- C语言实现单链表面试题——基础篇(下)
- 关于单链表的一些面试题-基础篇(C语言实现)
- C语言实现单链表的创建、插入、删除
- 用C语言实现单链表的各种操作(二)
- C语言实现单链表面试题——基础篇(上)
- C语言实现单链表面试题——基础篇(中)
- c语言实现单链表的及各项基本功能
- 数据结构c语言实现之单链表的应用
- C语言实现单链表面试题--进阶(带环问题)