#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node
{
int data
struct node *next
}slink
int n
slink *creat() //创建链表
int insert(slink *head,int i,int x)//在结点i之前播放一个数x
int del(slink *head,int i,int *x) //删除第i个结点并返回该结点上的数值
slink *get(slink *head,int i) //查找第i个结点,并返回
int length(slink *head) //求链表的长度
void print(slink *head) //输出链表
int main()
{
slink *head
int de,num=0,ins,len //de为要删除的结点,ins为要插入的结点
//这里num要用整形数,不用指针
printf("input records:\n")
head=creat()
print(head)
printf("\ninput the delete number:(the number<%d)",n)
scanf("%d",&de) //这里少了一个&符
if(del(head,de,&num)==1)
{
print(head)
printf("%d",num)
}
else
printf("delete failure!")
printf("\ninput the inset record:")
scanf("%d",&ins)
if(insert(head,2,ins)==1)
print(head)
else
printf("the insert is failure!")
len=length(head)
printf("the length is %d\n",len)
return 0
}
slink *creat() /*定义函数,返回带头结点的链表*/ //兄弟,你建的是不带头结点的链表,带头结点的链表的头结点是不存数据的!!!
{
slink *head
slink *p1,*p2
n=0
head=NULL
p1=p2=(slink*)malloc(sizeof(slink))
scanf("%d",&p1->data)
while(p1->data!=0)
{
n=n+1
if(n==1) head=p1
else p2->next=p1
p2=p1
p1=(slink*)malloc(sizeof(slink))
scanf("%d",&p1->data)
}
p2->next=NULL
return (head)
}
int insert(slink *head,int i,int x)
{
slink *p,*pre,*q
int j=0
p=(slink*)malloc(sizeof(slink))
p->data=x
pre=head/*pre指向待插入结点的前驱结点*/
q=head->next /*q指向当前比较结点*/
while(q&&j<i-1) /*查找p结点应该插入的位置*/
{pre=q
q=q->next
j++
}
if(j!=i-1||i<1) return 0 /*插入不成功*/
else
{p->next=q /*将p结点插入链表*/
pre->next=p
}
return 1 /*插入成功*/
}
int del(slink *head,int i,int *x)/*删除第i个结点,并通过x返回值*/
{
slink *p,*q
int j=0
p=head
while(p->next&&j<i-1) /*查找第i个结点的前驱位置p*/
{
p=p->next
j++
}
if(!(p->next)||j>i-1) return 0 /*删除位置不合适*/
q=p->next /*删除并释放结点*/
p->next=q->next
*x=q->data
free(q)
n-- //删除了数据,总数n应该减1吧?
return 1
}
slink *get(slink *head,int i) /*查找第i个结点*/
{slink *p
int j=0
p=head
while(p->next&&j<i-1) /*查找第i个结点的前驱*/
{p=p->nextj++}
if(!(p->next)&&j>i-1) return NULL
return p->next
}
int length(slink *head) /*求表的长度*/
{int len=0
slink *p
p=head /*设该表有表头*/
while(p){p=p->nextlen++} //你建的是不带头链表,所以这里少了一个
return len
}
void print(slink *head)
{slink *p
printf("\nNow,these %d records are :\n",n)
p=head
if(head!=NULL)
do
{printf("%d \n",p->data)
p=p->next
}while(p!=NULL)
}
/************************结构体链表******************************************************/struct
student
{
unsigned
int
stu_ID
char
sex
int
age
struct
student
*st
}
struct
student
*p,*h,*s
student*
crate_list(){//建立链表,返回链表的头结点
int
i=20
unsigned
short
c
p
=
new
student
h
=
p
p->stu_ID=1000
p->sex
=
'M'
p->age
=
i
do{
s=new
student
s->stu_ID=i+1000
s->sex
=
'M'
s->age
=
i
p->st
=
s
p
=
p->st
i++
}while(i<31)
p
=
NULL
return
h
}
bool
delete_node(student*
p,int
a){//传入头结点和年龄
if(p){
student
*
temp=p,*p_pre
while(temp->age!=a){
p_pre=temp
temp=temp->st
if(!temp)
return
false
}
p_pre->st=temp->st
delete
temp
temp=NULL
return
true
}
return
false
}
void
display_list(student
*
h){//打印链表中的数据
student*
temp=p
while(temp->st)
cout<<temp->stu_ID<<"
"<<temp->sex<<"
"<<temp->age<<endl
}
前阵子做的用单向链表实现约瑟夫问题:有M个人围一圈玩报数,凡报到N的出退出,输出每次退出的人的编号。
#include
"stdio.h"
struct
game
{
int
ID
game
*pNext
}
void
main()
{
int
i,m=17,n=3
game
*pPrev,*pNode,*pTop
printf("Input
M
N:")
scanf("%d
%d",&m,&n)
if(n>m||n<1)
return
pTop=new
game
pTop->ID=1
pPrev=pTop
for(i=2i<=mi++)
{
pNode=new
game
pNode->ID=i
pPrev->pNext=pNode
pPrev=pNode
}
pNode->pNext=pTop
pPrev=pNode
pNode=pTop
i=0
while(pNode->pNext!=pNode)
{
i++
if(i%n==0)
{
printf("%d
",pNode->ID)
pPrev->pNext=pNode->pNext
delete
pNode
pNode=pPrev->pNext
i=0
}
else
{
pPrev=pNode
pNode=pNode->pNext
}
}
delete
pNode
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)