单链表增删改查代码实现/约瑟夫环代码实现
2014-11-18 15:52
323 查看
#include <stdio.h>
#include <stdlib.h>
//定义结点
struct node
{
int data;
struct node *next;
};
/***********************实现函数********************************/
//找单链表中值为key的结点
struct node *search(struct node *head,int key)
{
struct node *p; //游标
p = head->next;
while (p!=NULL) {
if (p->data==key) {
return (p);
}else
{
p=p->next;
}
}
return NULL;
}
//单链表结点的插入,p结点后插入值为key的结点
void insert (struct node *p,int key)
{
struct node *q;
q = (struct node *)malloc(sizeof(struct node));
if (!q) {
printf("不能分配内存空间!");
exit(0);
}
q->data = key;
q->next = NULL;
q->next = p->next;
p->next = q;
}
//单链表结点的删除
int del(struct node *head,int key)
{
struct node *p,*q;
int flag = 0;
p = head;
while (p->next!=NULL) {
if (p->next->data==key) {
flag = 1;
break;
}else
p = p->next;
}//如果结点中存在值为key的结点,找到其前驱结点p,flag变量置1;
if (flag==1) {
q = p->next;
p->next = q->next;
free(q);
return 1;
}else
return 0;
}
//逆序建链表
struct node *creat1(int n)
{
struct node * head, * p;
int i;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
for (i=0; i<n; i++) {
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = head->next;
head->next = p;
}
return (head);
}
//打印链表
void display(struct node *head, int n)
{
int i;
head=head->next;
for (i=0; i<n; i++) {
printf("%d ",head->data);
head=head->next;
}
printf("\n");
}
//顺序建链表
struct node *creat2(int n)
{
struct node * head,* tail,* p;
int i;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
tail = head;
for (i=0; i<n; i++) {
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = NULL;
tail->next = p;
tail = p;
}
return (head);
}
//单链表的逆置(带头结点)
void reverse(struct node * head)
{
struct node * p,* q;
p = head->next;
head->next = NULL;
q = p->next;
while (p!=NULL) {
p->next = head->next;
head->next = p;
p = q;
if (q!=NULL) {
q = q->next;
}
}
}
//两个带头结点的有序单链表的归并
struct node * merge(struct node *head1,struct node *head2)
{
struct node *p1,*p2,*tail;
p1 = head1->next;
p2 = head2->next;
tail = head1;
free(head2);
while (p1&&p2) {
if (p1->data < p2->data) {
tail->next = p1;
tail = p1;
p1 = p1->next;
}else{
tail->next = p2;
tail = p2;
p2 = p2->next;
}
}
if (p1)
tail->next = p1;
else
tail->next = p2;
return (head1);
}
//单链表的拆分(拆分为负整数和非负整数)
struct node * split(struct node * head1)
{
struct node *head2,*p,*q;
head2 = (struct node *)malloc(sizeof(struct node));
head2->next = NULL;
p = head1->next;
head1->next = NULL;
q = p->next;
while (p!=NULL) {
if (p->data >= 0) {
p->next = head1->next;
head1->next = p;
}else
{
p->next = head2->next;
head2->next = p;
}
p = q;
if (q!=NULL) {
q = q->next;
}
}
return (head2);
}
//循环链表创建,赋初始值
struct node *creat(int n)
{
int i;
struct node *p,*tail,*head;
p = (struct node *)malloc(sizeof(struct node));
p->data = 1;
p->next = NULL;
head = p;
tail = p;
for (i=2; i<=n; i++) {
p = (struct node *)malloc(sizeof(struct node));
p->data = i;
tail->next = p;
tail = p;
p->next = NULL;
}
tail->next = head;//最后一个结点的指针域指向第一个结点
return head;
}
//循环建链表、自定义数据
struct node *creat3(int n)
{
int i;
struct node *p,*tail,*head;
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = NULL;
head = p;
tail = p;
for (i=0; i<n-1; i++) {
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
tail->next = p;
tail = p;
p->next = NULL;
}
tail->next = head;//最后一个结点的指针域指向第一个结点
return head;
}
//约瑟夫环
int sel(struct node *head,int m,int n)//n个数m删除位置
{
int num = 0;
int count = 0;
struct node *p,*q;
q = head;
while (q->next != head) {
q = q->next;
}
printf("被删掉的序号为:");
while (count < n-1) {
p = q->next;
num++;
if (num%m==0) {
q->next = p->next;
printf("%3d",p->data);
free(p);
count++;
}else
q = p;
}
return q->data;//最后一个结点的数据域为所求的结果
}
/**************************主函数********************************/
int main(int argc, const char * argv[])
{
int n,m;
struct node *head;
// scanf("%d %d",&n,&m);
// head = creat3(n);
// printf("\n%d\n",sel(head, m, n));
scanf("%d",&n);
head = creat2(n);
display(head, n);
return 0;
}
#include <stdlib.h>
//定义结点
struct node
{
int data;
struct node *next;
};
/***********************实现函数********************************/
//找单链表中值为key的结点
struct node *search(struct node *head,int key)
{
struct node *p; //游标
p = head->next;
while (p!=NULL) {
if (p->data==key) {
return (p);
}else
{
p=p->next;
}
}
return NULL;
}
//单链表结点的插入,p结点后插入值为key的结点
void insert (struct node *p,int key)
{
struct node *q;
q = (struct node *)malloc(sizeof(struct node));
if (!q) {
printf("不能分配内存空间!");
exit(0);
}
q->data = key;
q->next = NULL;
q->next = p->next;
p->next = q;
}
//单链表结点的删除
int del(struct node *head,int key)
{
struct node *p,*q;
int flag = 0;
p = head;
while (p->next!=NULL) {
if (p->next->data==key) {
flag = 1;
break;
}else
p = p->next;
}//如果结点中存在值为key的结点,找到其前驱结点p,flag变量置1;
if (flag==1) {
q = p->next;
p->next = q->next;
free(q);
return 1;
}else
return 0;
}
//逆序建链表
struct node *creat1(int n)
{
struct node * head, * p;
int i;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
for (i=0; i<n; i++) {
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = head->next;
head->next = p;
}
return (head);
}
//打印链表
void display(struct node *head, int n)
{
int i;
head=head->next;
for (i=0; i<n; i++) {
printf("%d ",head->data);
head=head->next;
}
printf("\n");
}
//顺序建链表
struct node *creat2(int n)
{
struct node * head,* tail,* p;
int i;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
tail = head;
for (i=0; i<n; i++) {
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = NULL;
tail->next = p;
tail = p;
}
return (head);
}
//单链表的逆置(带头结点)
void reverse(struct node * head)
{
struct node * p,* q;
p = head->next;
head->next = NULL;
q = p->next;
while (p!=NULL) {
p->next = head->next;
head->next = p;
p = q;
if (q!=NULL) {
q = q->next;
}
}
}
//两个带头结点的有序单链表的归并
struct node * merge(struct node *head1,struct node *head2)
{
struct node *p1,*p2,*tail;
p1 = head1->next;
p2 = head2->next;
tail = head1;
free(head2);
while (p1&&p2) {
if (p1->data < p2->data) {
tail->next = p1;
tail = p1;
p1 = p1->next;
}else{
tail->next = p2;
tail = p2;
p2 = p2->next;
}
}
if (p1)
tail->next = p1;
else
tail->next = p2;
return (head1);
}
//单链表的拆分(拆分为负整数和非负整数)
struct node * split(struct node * head1)
{
struct node *head2,*p,*q;
head2 = (struct node *)malloc(sizeof(struct node));
head2->next = NULL;
p = head1->next;
head1->next = NULL;
q = p->next;
while (p!=NULL) {
if (p->data >= 0) {
p->next = head1->next;
head1->next = p;
}else
{
p->next = head2->next;
head2->next = p;
}
p = q;
if (q!=NULL) {
q = q->next;
}
}
return (head2);
}
//循环链表创建,赋初始值
struct node *creat(int n)
{
int i;
struct node *p,*tail,*head;
p = (struct node *)malloc(sizeof(struct node));
p->data = 1;
p->next = NULL;
head = p;
tail = p;
for (i=2; i<=n; i++) {
p = (struct node *)malloc(sizeof(struct node));
p->data = i;
tail->next = p;
tail = p;
p->next = NULL;
}
tail->next = head;//最后一个结点的指针域指向第一个结点
return head;
}
//循环建链表、自定义数据
struct node *creat3(int n)
{
int i;
struct node *p,*tail,*head;
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
p->next = NULL;
head = p;
tail = p;
for (i=0; i<n-1; i++) {
p = (struct node *)malloc(sizeof(struct node));
scanf("%d",&p->data);
tail->next = p;
tail = p;
p->next = NULL;
}
tail->next = head;//最后一个结点的指针域指向第一个结点
return head;
}
//约瑟夫环
int sel(struct node *head,int m,int n)//n个数m删除位置
{
int num = 0;
int count = 0;
struct node *p,*q;
q = head;
while (q->next != head) {
q = q->next;
}
printf("被删掉的序号为:");
while (count < n-1) {
p = q->next;
num++;
if (num%m==0) {
q->next = p->next;
printf("%3d",p->data);
free(p);
count++;
}else
q = p;
}
return q->data;//最后一个结点的数据域为所求的结果
}
/**************************主函数********************************/
int main(int argc, const char * argv[])
{
int n,m;
struct node *head;
// scanf("%d %d",&n,&m);
// head = creat3(n);
// printf("\n%d\n",sel(head, m, n));
scanf("%d",&n);
head = creat2(n);
display(head, n);
return 0;
}
相关文章推荐
- 双链表的增删改查的功能实现代码。
- Linux 内核代码赏析与应用(二)-链表之实现
- 代码分析-DataGrid实现增删(带提示)改和分页
- 链表相关操作代码实现
- hibernate中的增删改查实现代码
- C语言算法(代码):假设文件中有许多单词,将单词从文件中读出,放到链表中,按字母升序实现
- 代码分析-DataGrid实现增删(带提示)改和分页
- 单链表的实现:增删改查
- javascript写的一个链表实现代码
- 利用链表实现队列的Java代码
- 线索链表的实现 C++代码实现
- 查找算法集:顺序查找、二分查找、插值查找、动态查找(数组实现、链表实现,附代码)
- 代码分析-DataGrid实现增删(带提示)改和分页
- 代码分析-DataGrid实现增删(带提示)改和分页
- 有序链表 代码实现
- 代码分析-DataGrid实现增删(带提示)改和分页
- 只需一行代码实现增删查改,微软已经让我们很简单。谈AccessDataSource的使用。
- hibernate中的增删改查实现代码
- 不写一行代码,实现单表增删改查(附demo)
- 双向链表代码实现-C语言