
#include <stdlib.h>
#include <string.h>//系统自带的头文件,最好使用<>
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")
}
}
参考一下这个#include<iostream.h>
#include<malloc.h>
#include<conio.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define ERROR 0
#define OK 1
typedef int ElemType
struct LNODE
{
ElemType data
struct LNODE *next
}
typedef struct LNODE LNode
typedef struct LNODE *LinkList
//初始化单链表
int init(LinkList &L)
{
L=(LNode *)malloc(sizeof(LNode))
if(!L) return(ERROR)
L->next=NULL
return OK
}/*init */
//求表长
int ListLength(LinkList L)
{
int j=0
while (L->next)
{
L=L->next
j++
}
return j
}
//获取表中第i个元素的值
int GetElem(LinkList L,int i,ElemType &e)
{
LinkList pint j
p=L->nextj=1
while(p&&j<i)
{
p=p->next++j
}
if(!p||j>i) return ERROR
e=p->data
return OK
}
//判断元素e是否在该链表中
int LocateElem(LinkList La,ElemType e)
{
LinkList p
p=La
int i=0
while(p->next)
{
p=p->next
i++
if(p->data==e)
return i
}
return 0
}
//打印表中元素值
void PrintList(LinkList L)
{
LinkList p
p=L
while(p->next)
{
p=p->next
cout<<p->data<<ends
}
cout<<endl
}
//插入 *** 作
int ListInsert(LinkList &L,int i,ElemType e)
{
LinkList p,s
int j
p=Lj=0
while(p&&j<i)
{
p=p->next
++j
}
if(!p||j>i-1) return ERROR
s=(LinkList)malloc(sizeof(LNode))
s->data=e
s->next=p->next
p->next=s
return OK
}/*ListInsert Before i */
//删除 *** 作
int ListDelete(LinkList &L,int i,ElemType &e)
{
LinkList p,q
int j
p=Lj=0
while(p&&j<i-1)
{
p=p->next
++j
}
if(!p||j>i) return ERROR
q=p->next
p->next=p->next->next
e=q->data
free(q)
return OK
}
// 头插法建表
int CreateList(LinkList &L,int n)
{
LinkList p
L=(LinkList)malloc(sizeof(LNode))
if(!L) return ERROR
L->next=NULL
for(int i=ni>0i--)
{
p=(LinkList)malloc(sizeof(LNode))
if(!p) return ERROR
cin>>p->data
p->next=L->nextL->next=p
}
return OK
}
//合并两个有序表
void MergeList(LinkList La,LinkList Lb,LinkList &Lc)
{
LinkList pa,pb,pc
pa=La->nextpb=Lb->next
Lc=pc=La
while(pa &&pb)
{
if(pa->data<=pb->data)
{
pc->next=papc=papa=pa->next
}
else
{
pc->next=pbpc=pbpb=pb->next
}
}
pc->next=pa?pa:pb
free(Lb)
}
void main()
{
LinkList La,Lb,Lc
ElemType e
cout<<"\n\n-------------------List Demo is running...----------------\n\n"
cout<<"First is InsertList function.\n"
init(La)
int n
cout<<"请输入La表中元素个数:"
cin>>n
cout<<endl<<"请输入"<<n<<"个元素值:"
CreateList(La,n)
PrintList(La)
cout<<"La的表长为:"<<ListLength(La)<<endl
//取值
cout<<"取第i位元素的值:\n请输入位序:"
cin>>n
GetElem(La,n,e)
cout<<endl<<"第"<<n<<"位元素的值为:"<<e<<endl
//定位
cout<<"定位 *** 作:\n请输入要查找的元素:"
cin>>e
cout<<endl<<" 所要查找的元素在表中第"<<LocateElem(La,e)<<"位\n"
//删除
cout<<"删除 *** 作:\n输入要删除元素的位序:"
cin>>n
ListDelete(La,n,e)
cout<<"\n要删除的元素值为:"<<e<<endl
cout<<"删除后表中值有:"
PrintList(La)
init(Lb)
cout<<"请输入Lb表中元素个数:"
cin>>n
cout<<endl<<"请输入"<<n<<"个元素值:"
CreateList(Lb,n)
PrintList(Lb)
cout<<"Lb的表长为:"<<ListLength(Lb)<<endl
cout<<"合并两个有序表:\n"
MergeList(La,Lb,Lc)
PrintList(Lc)
getch()
}
我试着用浅显的例子给你解释一下:比如, *** 场上站了很多人(元素),现在规定每个人记住他后边的人(指针)
这样就形成了链表。你只要知道链表头(第一个人),然后让他指出他后边的
人,逐个指下去,就可以遍历链表。
现在,加入了一个新人。要插到第五个人和第六个人之间。
*** 作方法是,让他跟第五个人问一下,第五个人指的是谁,
然后新人也指向这个人(实际就是第六个人)
然后让第五个人指向新人。这样,就完成了链表的插入运算。
你可以发现,整个过程,只是指针的赋值,而跟每个人的站位是无关的。
大家可以随便站。
这就是说,链表中插入一个元素,各个元素的位置是不需要移动的。
补充一句,如果是数组的话,插入一个值,就需要把插入位置后边的
所有值移动一位,给它让位置了。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)