#include "math.h"
#include "stdlib.h" //exit()
typedef int DataType;
#include "malloc.h" //malloc()
typedef struct Node
{
DataType data;
struct Node *next;
} SLNode;
void ListInitiate(SLNode **head)
{
if ((*head = (SLNode *)malloc(sizeof(SLNode))) == NULL) exit(1);
(*head)->next = NULL;
}
//在带头节点的单链表head的第i(0<=i<=size)个结点前插入
int ListInsert(SLNode * head, int i, DataType x)
{
SLNode *p, *q;
int j;
p = head;
j = -1;
//其实就是i-1-(-1)=i次,把p指针移到第i-1个结点上,因为结点从0开始
while (p->next != NULL && j < i - 1)
{
p = p->next;
j++;
}
if (j != i - 1)
{
printf("insert error !/n");
return 0;
}
if ((q = (SLNode *)malloc(sizeof(SLNode))) == NULL) exit(1);
q->data = x;
q->next = p->next;
p->next = q;
return 1;
}
//释放动态申请的内存空间
void Destroy(SLNode * *head)
{
SLNode *p, *p1;
p = *head;
while (p != NULL)
{
p1 = p;
p = p->next;
free(p1);
}
*head = NULL;
}
void Buildloop(SLNode *head, SLNode *head2, DataType m, DataType n)
{
int flag1 = 0, flag2 = 0;
SLNode *p, *q, *r, *s;
p = head->next;
q = head2;
while (p != NULL)
{
if (p->data ==m ) {
flag1++;
if (flag1 == 1) {
r = (SLNode *)malloc(sizeof(SLNode));
r->data = m;
r->next = p->next;
}
}
if (p->data == n) {
flag2++;
if (flag2 == 1) {
s = (SLNode *)malloc(sizeof(SLNode));
s->data = n;
s->next = p->next;
//s = p;
}
if (flag1 == 1 && flag2 == 1)
break;
}
/*if (s->next = NULL) {
s->next = r;
}*/
head2->next = s->next;
s->next = r;
while (q->next != NULL) {
q = q->next;
}
q->next = r;
}
}
int FindLoopNode(SLNode *head, int j)
{
int i = j, begin = 1;
SLNode *fast = head, *slow = head;
for (i; i > 0; i--) {
fast = fast->next;
}
slow = head;
while (slow != fast) {
begin++;
slow = slow->next;
fast = fast->next;
}
return begin;
}
int IsExitsloop(SLNode *head)
{
SLNode *slow = head;
SLNode *fast = head;
int i, j = 1;
while (fast&&fast->next)
{
slow = slow->next;
fast = fast->next->next;
if (slow == fast)break;
}
if (fast == NULL || fast->next == NULL) {
printf("该链表不存在环!\n");
}
else {
printf("该链表存在环!\n");
while (fast&&fast->next) {
slow = slow->next;//fast与slow相遇后,slow并没有遍历完链表,继续遍历
j++;
if (slow->next == fast)break;
}
}
i = FindLoopNode(head, j);
printf("环的起始位置为:%d\n", i);
printf("环的结束位置为:%d\n", j + i - 1);
return 0;
}
int ListLength(SLNode *head)
{
SLNode *slow = head->next, *fast = head->next;
int length = 1;
while (fast&&fast->next) {
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
{
while (fast&&fast->next)
{
slow = slow->next;
length++;
if (slow->next == fast)break;
}
break;
}
}
return length;
}
void ReList(SLNode *head)
{
SLNode *p = head, *q = head;
int length = ListLength(head);
for (; length > 0; length--)p = p->next;
while (p != q)
{
q = q->next;
if (p->next == q)
{
p->next = NULL;
break;
}
p = p->next;
}
}
void IsIntersect(SLNode *head, SLNode *head2)
{
SLNode *p, *q;
p = head;
q = head2;
int i = 0, flag = 0;
while (p->next != NULL)
{
i++;
p = p->next;
while (q->next != NULL)
{
q = q->next;
if (p == q) {
flag = 1;
printf("链表存在相交位置,位置为%d\n",i);
break;
}
}
if (flag == 1)break;
}
if (flag != 1)
printf("链表不相交!");
}
#pragma once
8 个解决方案
#1
你的main函数呢?
#2
#include "stdio.h"
#include "stdlib.h" //exit()
#include "malloc.h" //malloc()
typedef int DataType;
#include "NLinlist.h"
int main()
{
char a = 'Y';
SLNode *head, *head2, *p,*q;
int i,j, number, ii, m, n;
//int ninsertx, ninserty, ndeletex, npreviousx, ninsertm, ninsertn;
//初始化链表
ListInitiate(&head);
ListInitiate(&head2);
printf("请输入链表的数据元素个数:\n");
scanf("%d", &number);
printf("\n请一个一个的输入元素值:\n");
for (i = 0; i < number; i++)
{
scanf("%d", &ii);
ListInsert(head, i, ii);
}
printf("你输入的链表是:\n");
p = head->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n请输入要成环的两个元素的值:\n");
scanf("%d%d", &m,&n);
//printf("%d%d", m, n);
//scanf("%d", &n);
Buildloop(head, head2, m, n);
/*p = head->next;
q = head2->next;
while (q != NULL)
{
printf("%d ", p->data);
p = p->next;
printf("%d ", q->data);
q = q->next;
}*/
FindLoopNode(head,j);
IsExitsloop(head);
ListLength(head);
ReList(head);
IsIntersect(head, head2);
}
这里
#include "stdlib.h" //exit()
#include "malloc.h" //malloc()
typedef int DataType;
#include "NLinlist.h"
int main()
{
char a = 'Y';
SLNode *head, *head2, *p,*q;
int i,j, number, ii, m, n;
//int ninsertx, ninserty, ndeletex, npreviousx, ninsertm, ninsertn;
//初始化链表
ListInitiate(&head);
ListInitiate(&head2);
printf("请输入链表的数据元素个数:\n");
scanf("%d", &number);
printf("\n请一个一个的输入元素值:\n");
for (i = 0; i < number; i++)
{
scanf("%d", &ii);
ListInsert(head, i, ii);
}
printf("你输入的链表是:\n");
p = head->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n请输入要成环的两个元素的值:\n");
scanf("%d%d", &m,&n);
//printf("%d%d", m, n);
//scanf("%d", &n);
Buildloop(head, head2, m, n);
/*p = head->next;
q = head2->next;
while (q != NULL)
{
printf("%d ", p->data);
p = p->next;
printf("%d ", q->data);
q = q->next;
}*/
FindLoopNode(head,j);
IsExitsloop(head);
ListLength(head);
ReList(head);
IsIntersect(head, head2);
}
这里
#3
head2->next = s->next;
s->next = r;
这里,如果if (p->data == n)条件不满足,那么s->next肯定是有问题的,所以这儿有潜在的bug.
做如下修改,试试:
void Buildloop(SLNode *head, SLNode *head2, DataType m, DataType n)
{
int flag1 = 0, flag2 = 0;
SLNode *p, *q, *r = NULL, *s = NULL;
p = head->next;
q = head2;
while (p != NULL)
{
if (p->data ==m ) {
flag1++;
if (flag1 == 1) {
r = (SLNode *)malloc(sizeof(SLNode));
r->data = m;
r->next = p->next;
}
}
if (p->data == n) {
flag2++;
if (flag2 == 1) {
s = (SLNode *)malloc(sizeof(SLNode));
s->data = n;
s->next = p->next;
//s = p;
}
if (flag1 == 1 && flag2 == 1)
break;
}
/*if (s->next = NULL) {
s->next = r;
}*/
if (s) { /*判断一下s*/
head2->next = s->next;
s->next = r;
}
while (q->next != NULL) {
q = q->next;
}
q->next = r;
}
}
#4
#5
s->next = r;这里的s指针可能还是NULL,赋值前最后判定下,因前面s = (SLNode *)malloc(sizeof(SLNode));这句不一定执行到。
#6
cc 分配了但是未被初始化的内存区域
#7
仅供参考:
//不带表头结点的单向链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <locale.h>
struct NODE {
int data;
struct NODE *next;
} *head,*p,*q,*s,*p1,*p2,*q1,**ta;
int i,k,n,t,m,v,N=10;
int main() {
setlocale(LC_ALL,"chs");
srand(time(NULL));
head=NULL;
printf("创建%d个节点的单链表:",N);//创建N个节点的单链表
p=head;
for (i=0;i<N;i++) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) exit(1);
q->data=rand()%100;//填写0..99的随机值
q->next=NULL;
if (NULL==p) {
head=q;
p=head;
} else {
p->next=q;
p=q;
}
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
k=3;
v=5;
printf("将值为%d的结点插入到单链表的第%d个结点前:",v,k);//将值为v的结点插入到单链表的第k个结点前
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==1) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) exit(1);
q->data=v;
q->next=head;
head=q;
break;
} else {
if (k-1==n) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) exit(1);
q->data=v;
q->next=p->next;
p->next=q;
break;
}
}
p=p->next;
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
k=5;
printf("删除第%d个节点:",k);//删除第k个节点
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==1) {
q=head;
head=head->next;
free(q);
break;
} else {
if (k-1==n) {
q=p->next;
if (q) {
p->next=q->next;
free(q);
}
break;
}
}
p=p->next;
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
printf("从小到大排序:");//从小到大排序
for (p=head,p1=NULL;p!=NULL;p1=p,p=p->next) {
for (q=p->next,q1=p;q!=NULL;q1=q,q=q->next) {
if (p->data > q->data) {
//交换data
// printf("swap %02d %02d\n",p->data,q->data);
// t=p->data;p->data=q->data;q->data=t;
//或者
//交换next
// printf("swap %02d %02d\n",p->data,q->data);
if (p==head) {//p是头
if (p->next==q) {//pq挨着
head=q;
p->next=q->next;
q->next=p;
q=p;
p=head;
} else {//pq不挨着
head=q;
p2=p->next;
p->next=q->next;
q->next=p2;
q1->next=p;
q=p;
p=head;
}
} else {//p不是头
if (p->next==q) {//pq挨着
p1->next=q;
p->next=q->next;
q->next=p;
q=p;
p=p1->next;
} else {//pq不挨着
p1->next=q;
p2=p->next;
p->next=q->next;
q->next=p2;
q1->next=p;
q=p;
p=p1->next;
}
}
//输出整个单链表
// s=head;
// while (1) {
// if (NULL==s) {
// printf("\n");
// break;
// }
// printf("%02d->",s->data);
// s=s->next;
// }
// getchar();
}
}
}
//输出整个单链表并计算链表长度n
n=0;
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
n++;
s=s->next;
}
printf("将整个链表逆序:");//将整个链表逆序
if (n>=2) {
p=head;
q=p->next;
p->next=NULL;
while (1) {
q1=q->next;
q->next=p;
p=q;
q=q1;
if (NULL==q) break;
}
head=p;
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
m=4;
n=6;
printf("将单链表中前%d个结点和后%d个结点进行互换:",m,n);//将单链表中前m个结点和后n个结点进行互换,m+n为链表总长
k=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
k++;
if (m==k) {
q=p;
}
s=p;
p=p->next;
}
q1=head;
head=q->next;
s->next=q1;
q->next=NULL;
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//释放所有节点
p=head;
while (1) {
if (NULL==p) {
break;
}
q=p->next;
free(p);
p=q;
}
return 0;
}
//创建10个节点的单链表:08->74->07->23->03->99->31->56->88->16->
//将值为5的结点插入到单链表的第3个结点前:08->74->05->07->23->03->99->31->56->88->16->
//删除第5个节点:08->74->05->07->03->99->31->56->88->16->
//从小到大排序:03->05->07->08->16->31->56->74->88->99->
//将整个链表逆序:99->88->74->56->31->16->08->07->05->03->
//将单链表中前4个结点和后6个结点进行互换:31->16->08->07->05->03->99->88->74->56->
//
#8
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,
看不懂时双击下一行,直到能看懂为止。
#1
你的main函数呢?
#2
#include "stdio.h"
#include "stdlib.h" //exit()
#include "malloc.h" //malloc()
typedef int DataType;
#include "NLinlist.h"
int main()
{
char a = 'Y';
SLNode *head, *head2, *p,*q;
int i,j, number, ii, m, n;
//int ninsertx, ninserty, ndeletex, npreviousx, ninsertm, ninsertn;
//初始化链表
ListInitiate(&head);
ListInitiate(&head2);
printf("请输入链表的数据元素个数:\n");
scanf("%d", &number);
printf("\n请一个一个的输入元素值:\n");
for (i = 0; i < number; i++)
{
scanf("%d", &ii);
ListInsert(head, i, ii);
}
printf("你输入的链表是:\n");
p = head->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n请输入要成环的两个元素的值:\n");
scanf("%d%d", &m,&n);
//printf("%d%d", m, n);
//scanf("%d", &n);
Buildloop(head, head2, m, n);
/*p = head->next;
q = head2->next;
while (q != NULL)
{
printf("%d ", p->data);
p = p->next;
printf("%d ", q->data);
q = q->next;
}*/
FindLoopNode(head,j);
IsExitsloop(head);
ListLength(head);
ReList(head);
IsIntersect(head, head2);
}
这里
#include "stdlib.h" //exit()
#include "malloc.h" //malloc()
typedef int DataType;
#include "NLinlist.h"
int main()
{
char a = 'Y';
SLNode *head, *head2, *p,*q;
int i,j, number, ii, m, n;
//int ninsertx, ninserty, ndeletex, npreviousx, ninsertm, ninsertn;
//初始化链表
ListInitiate(&head);
ListInitiate(&head2);
printf("请输入链表的数据元素个数:\n");
scanf("%d", &number);
printf("\n请一个一个的输入元素值:\n");
for (i = 0; i < number; i++)
{
scanf("%d", &ii);
ListInsert(head, i, ii);
}
printf("你输入的链表是:\n");
p = head->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n请输入要成环的两个元素的值:\n");
scanf("%d%d", &m,&n);
//printf("%d%d", m, n);
//scanf("%d", &n);
Buildloop(head, head2, m, n);
/*p = head->next;
q = head2->next;
while (q != NULL)
{
printf("%d ", p->data);
p = p->next;
printf("%d ", q->data);
q = q->next;
}*/
FindLoopNode(head,j);
IsExitsloop(head);
ListLength(head);
ReList(head);
IsIntersect(head, head2);
}
这里
#3
head2->next = s->next;
s->next = r;
这里,如果if (p->data == n)条件不满足,那么s->next肯定是有问题的,所以这儿有潜在的bug.
做如下修改,试试:
void Buildloop(SLNode *head, SLNode *head2, DataType m, DataType n)
{
int flag1 = 0, flag2 = 0;
SLNode *p, *q, *r = NULL, *s = NULL;
p = head->next;
q = head2;
while (p != NULL)
{
if (p->data ==m ) {
flag1++;
if (flag1 == 1) {
r = (SLNode *)malloc(sizeof(SLNode));
r->data = m;
r->next = p->next;
}
}
if (p->data == n) {
flag2++;
if (flag2 == 1) {
s = (SLNode *)malloc(sizeof(SLNode));
s->data = n;
s->next = p->next;
//s = p;
}
if (flag1 == 1 && flag2 == 1)
break;
}
/*if (s->next = NULL) {
s->next = r;
}*/
if (s) { /*判断一下s*/
head2->next = s->next;
s->next = r;
}
while (q->next != NULL) {
q = q->next;
}
q->next = r;
}
}
#4
#5
s->next = r;这里的s指针可能还是NULL,赋值前最后判定下,因前面s = (SLNode *)malloc(sizeof(SLNode));这句不一定执行到。
#6
cc 分配了但是未被初始化的内存区域
#7
仅供参考:
//不带表头结点的单向链表
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include <locale.h>
struct NODE {
int data;
struct NODE *next;
} *head,*p,*q,*s,*p1,*p2,*q1,**ta;
int i,k,n,t,m,v,N=10;
int main() {
setlocale(LC_ALL,"chs");
srand(time(NULL));
head=NULL;
printf("创建%d个节点的单链表:",N);//创建N个节点的单链表
p=head;
for (i=0;i<N;i++) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) exit(1);
q->data=rand()%100;//填写0..99的随机值
q->next=NULL;
if (NULL==p) {
head=q;
p=head;
} else {
p->next=q;
p=q;
}
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
k=3;
v=5;
printf("将值为%d的结点插入到单链表的第%d个结点前:",v,k);//将值为v的结点插入到单链表的第k个结点前
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==1) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) exit(1);
q->data=v;
q->next=head;
head=q;
break;
} else {
if (k-1==n) {
q=(struct NODE *)malloc(sizeof(struct NODE));
if (NULL==q) exit(1);
q->data=v;
q->next=p->next;
p->next=q;
break;
}
}
p=p->next;
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
k=5;
printf("删除第%d个节点:",k);//删除第k个节点
n=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
n++;
if (k==1) {
q=head;
head=head->next;
free(q);
break;
} else {
if (k-1==n) {
q=p->next;
if (q) {
p->next=q->next;
free(q);
}
break;
}
}
p=p->next;
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
printf("从小到大排序:");//从小到大排序
for (p=head,p1=NULL;p!=NULL;p1=p,p=p->next) {
for (q=p->next,q1=p;q!=NULL;q1=q,q=q->next) {
if (p->data > q->data) {
//交换data
// printf("swap %02d %02d\n",p->data,q->data);
// t=p->data;p->data=q->data;q->data=t;
//或者
//交换next
// printf("swap %02d %02d\n",p->data,q->data);
if (p==head) {//p是头
if (p->next==q) {//pq挨着
head=q;
p->next=q->next;
q->next=p;
q=p;
p=head;
} else {//pq不挨着
head=q;
p2=p->next;
p->next=q->next;
q->next=p2;
q1->next=p;
q=p;
p=head;
}
} else {//p不是头
if (p->next==q) {//pq挨着
p1->next=q;
p->next=q->next;
q->next=p;
q=p;
p=p1->next;
} else {//pq不挨着
p1->next=q;
p2=p->next;
p->next=q->next;
q->next=p2;
q1->next=p;
q=p;
p=p1->next;
}
}
//输出整个单链表
// s=head;
// while (1) {
// if (NULL==s) {
// printf("\n");
// break;
// }
// printf("%02d->",s->data);
// s=s->next;
// }
// getchar();
}
}
}
//输出整个单链表并计算链表长度n
n=0;
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
n++;
s=s->next;
}
printf("将整个链表逆序:");//将整个链表逆序
if (n>=2) {
p=head;
q=p->next;
p->next=NULL;
while (1) {
q1=q->next;
q->next=p;
p=q;
q=q1;
if (NULL==q) break;
}
head=p;
}
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
m=4;
n=6;
printf("将单链表中前%d个结点和后%d个结点进行互换:",m,n);//将单链表中前m个结点和后n个结点进行互换,m+n为链表总长
k=0;
p=head;
while (1) {
if (NULL==p) {
break;
}
k++;
if (m==k) {
q=p;
}
s=p;
p=p->next;
}
q1=head;
head=q->next;
s->next=q1;
q->next=NULL;
//输出整个单链表
s=head;
while (1) {
if (NULL==s) {
printf("\n");
break;
}
printf("%02d->",s->data);
s=s->next;
}
//释放所有节点
p=head;
while (1) {
if (NULL==p) {
break;
}
q=p->next;
free(p);
p=q;
}
return 0;
}
//创建10个节点的单链表:08->74->07->23->03->99->31->56->88->16->
//将值为5的结点插入到单链表的第3个结点前:08->74->05->07->23->03->99->31->56->88->16->
//删除第5个节点:08->74->05->07->03->99->31->56->88->16->
//从小到大排序:03->05->07->08->16->31->56->74->88->99->
//将整个链表逆序:99->88->74->56->31->16->08->07->05->03->
//将单链表中前4个结点和后6个结点进行互换:31->16->08->07->05->03->99->88->74->56->
//
#8
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,
看不懂时双击下一行,直到能看懂为止。