- 主题
- 68
- 帖子
- 348
- 精华
- 0
- 积分
- 635
- C币
- 578 枚
- 在线时间
- 57 小时
- 注册时间
- 2010-7-9
- 最后登录
- 2012-1-28
- 性别
- 保密
 
- 主题
- 68
- 帖子
- 348
- C币
- 578 枚
- 在线时间
- 57 小时
|
发表于 2010-7-12 09:03:52
|显示全部楼层
看了本论坛一个求助帖后, 写的一段代码, 因为原帖已有回答,因此就贴在这里了, 有兴趣的可以比较一下这里贴出来的代码,与原帖贴出来的代码的异同。 原帖链接:http://bbs.cyuyan.com.cn/thread-691-1-1.html。
下面为我写的代码:
/**************************************************************************
C语言链表操作代码
1、先建立一若干结点(结点数据域的值由键盘输入)构成的单链表,
再实现下列操作:(每一功能用一函数实现)
2 从链表的第i个结点开始连删len个结点,若不够len个结点,
则从第i个结点开始删到表尾。
3、在第i个结点之后插入一值为x的结点,并返回成功与否的标志。
**************************************************************************/
#include <stdio.h>
struct link
{
int x;
struct link *next;
};
// int creat(struct link *add);
int insert(struct link *head, int i,int data);
int delete(struct link *head,int i,int len);
int main(int argc, char **argv)
{
int flag; //flag 为标志位
int i=0;
int len=0;
struct link *data;
struct link *head;
head=data=(struct link *)malloc(sizeof(struct link)); //记录链表的head
head->next=NULL;
data->next=NULL;
printf("Input the data what you wanna to insert!");
printf("\nInput the character : to complete you data.\n")
scanf("%d",&flag);
while(':'!=(char)flag)
{
data.x=flag;
struct link *temp;
temp=(struct link *)malloc(sizeof(struct link));
data.next=temp;
data=temp;
data->next=NULL;
free(temp);
printf("Input the data what you wanna to insert!\n");
printf("Input the character : to complete.\n");
scanf("%d",&flag);
}
printf("\nInput the location where you want to insert a new data.");
scanf("%d",&i);
printf("\nInput the data you wanna to insert into the database.")
scanf("%d",&flag);
flag=insert(head,i,flag);
if(!flag) //flag=1 表示插入成功
{
printf("can not insert a new data into the database\n");
}
else
printf("You have succeed insert a new data to the database.\n");
printf("Input the location you want delete the node of the database.\n");
scanf("%d",&i);
printf("Pelease input how much node do you wanna delete.\n");
scanf("%d",&len);
if(!len) return 1;
if(!i)
{
printf("Input a wrong number,then will quit.\n");
getch();
return 1;
}
flag=delete(head,i,len); //flag=1表示删除成功。
(1==flag)?{printf("Have delete successfully.\n");}:{printf("\nCan not delete, then will quit.\n");}
free(data);
free(head);
getch();
return 1;
}
/*************************************************************
int insert(struct link *head,int i,int data)函数中
head为链表表头指针
i表示要插入的位置
data表示要插入的数据
*************************************************************/
int insert(struct link *head,int i,int data)
{
struct link *temp;
int j;
if(NULL==head->next )
if(1<i) return 0; //当为空链表,且要插入的地方不是第1个位置的时候
if(i==1) //原链表为非空,并且要插入到表头的位置的时候
{
temp=(struct link *)malloc(sizeof(struct link));
(NULL==temp) ? {return 0;}:{temp->next=head; head=temp; free(temp); return 1;}
}
for(j=0;j<i,j++) //原链表非空,且不是要插入到表头位置。
{
temp=(struct link *)malloc(sizeof(struct link));
(NULL==temp) ? {return 0;}:{temp->x=data;temp->next=head-next;head->next=temp;head=temp->next;free(temp); return 1;}
}
}
/******************************************************************
int delete(struct link *head, int i,int len)
head表示原链表表头
i表示删除开始的位置
len表示要删除的元素的个数
******************************************************************/
int delete(struct link *head, int i,int len)
{
struct link *temp,
*lenhead;
int j;
int k;
int length;
whie(!head->next)
{
length++;
temp=head->next;
head=temp;
head->next=temp->next;
}
if(1==i && 1==len)
{
temp=head->next;
head->next=temp->next;
head=temp;
return 1;
}
if(length>=i+len) //当链表长度足够长时
{
for(j=0;j<i;j++)
{
temp=head->next;
head->next=temp->next;
head=temp;
}
lenhead=head;
for(k=0;k<len;K++)
{
temp=head->next;
head->next=temp->next;
head=temp;
}
lenhead->next=head;
}
else //当要删除的节点超过原链表长度时
{
for(j=0;j<i;j++)
{
temp=head->next;
head->next=temp->next;
head=temp;
}
head->next=NULL;
}
return 1;
} |
-
1
查看全部评分
-
|