单链表的建立测长打印

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<conio.h>
using namespace std;
typedef struct student
{
    int data;
    struct student *next;

}node;
node*creat()
{
    node*head, *p, *s;
    int x, cycle = 1;
    head = (node*)malloc(sizeof(node));
    p = head;
    while (cycle)
    {
        printf("\nplease input the data:");
        scanf("%d", &x);
        if (x != 0)
        {
            s = (node*)malloc(sizeof(node));
            s->data = x;
            printf("\n%d", s->data);
            p->next = s;
            p = s;
        }
        else cycle = 0;
    }
    head = head->next;
    p->next = NULL;
    printf("\n yyy  %d", head->data);
    return (head);
}
//单链表测长
int length(node*head)
{
    int n = 0;
    node*p;
    p = head;
    while (p != NULL)
    {
        p = p->next;
        n++;
    }
    return (n);

}
//单链表打印
void print(node*head)
{
    node*p;
    int n;
    n = length(head);
    printf("\nNow,These %d records are:\n");
    p = head;
    if (head!=NULL)
        while (p != NULL)
        {
            printf("\n uuu%d ", p->data);
            p = p->next;
        }
}

单链表的删除节点:

status ListDelete(LinkList *L, int i, ElemType *e)
{
    int j;
    LinkList p, q;
    p = *L;
    j = 1;
    while (p->next&&j > i)
    {
        p = p->next;
        ++j;
    }
    if (!(p->next) || j > i)
        return ERROR;
}
p->next = q->next;
*e = q->data;
free(q);

//单链表的插入
node*insert(node*head, int num)
{
    node *p0, *p1, *p2;
    p1 = head;
    p0 = (node*)malloc(sizeof(node));
    p0->data = num;
    while (p0->data > p1->data&&p1->next != NULL)
    {
        p2 = p1;
        p1 = p1->next;
    }
    if (p0->data <= p1->data)
    {
        if (head == p1)
        {
            p0->next = p1;
            head = p0;
        }
        else{
            p2->next = p0;
            p0->next = p1;
        }
    }
    else{
        p1->next = p0;
        p0->next = NULL;
    }

//编程实现单链表的排序
node *sort(node*head)
{
    node*p, *p2, *p3;
    int n, int temp;
    n = length(head);
    if (head == NULL || head->next == NULL)
        return head;
    for (int j = 1; j < n; ++j)
    {
        p = head;
        for (int i = 0; i < n - j; ++i)
        {
            if (p->data>p->next->data)
            {
                temp = p->data;
                p->data = p->next->data;
                p->next->data = temp;

            }
            p = p->next;
        }
    }
    return head
}

//单链表的逆置

node*reverse(node*head)
{
    node*p1, *p1, *p3;//  定义三个指针,分别代表前一个节点,当前节点,下一个节点;
    if (head == NULL || head->next == NULL)//判断首节点的next是否为NULL,如果为空则不需要逆置
        return head;
    p1 = head, p2 = p1->next;//  前节点指向链表首节点;当前指针指向链表首节点的next域,下一个节点为NULL;
    while (p2)
    {
        p3 = p2->next;
        p2->next = p1;
        p1 = p2;
        p2 = p3;

    }
    head->next = NULL;
    head = p1;
    return head;
}
int main()
{
    node*head, stud;
    int n, del_num, insert_num;
    head = creat();
    print(head);
    cout << "\nInt:";
    cin >> del_num;
    head = del(head, del_name)
        print(head);
    cout << "\nplease input the insert data:";
    cin >> insert_num;
    head = insert(head, insert_num);
    print(head);
    return 0;
}

//链表环的判断及入口结点的查找
Node*findLoopPort(Node*head)
slow = fast = head;
//先判断是否存在环
while (fast != NULL&&fast->next != NULL)
{
	fast = fast->next->next;
	slow = slow->next;
	if (fast == slow)
		break;
}
if (fast != slow)
return NULL;
fast = head;
while (fast != slow)
{
	fast = fast->next;
	slow = slow->next;
}
return fast;
}
//判断两个链表是否相交
bool isIntersect(node*h1, node h2)
{
	if (H1 == NULL || h2 == NULL)return false;
	while (h1->next != NULL)
	{
		h1 = h1->next
	}
	while (h2->next != NULL)
	{
		h2 = h2->next;
	}
	if (h1 == h2)
		return true;;
	else
		return false;
}
//链表有环,如何判断相交
bool isInsertsectWithLoop(Node*h1,Node*h2)
	{
		if (!hascircle(h1, circleNode2))
			return false;
		if (!hascircle(h2, circleNode2))
			return false;
	
		Node*temp = circleNode2->next;
		while (temp != circleNode2->next)
		{
			if (temp == circleNode1)
				return true;
			temp = temp->next;
		}
		return true;
	}
//两个链表相交的第一个公共节点
Node*findInsetersectNode(node*h1, node*h2)
{
	int len1 = ListLength(h1);
	int len2 = ListLength(h2);
	if (len1 > len2)
	{
		for (int i = 0; i < len1 - len2; i++)
			h1 = h1->next;
	}
	else{
		for (int i = 0; i < len2 - len1; i++)
			h2 = h2->next;
	}
	while (h1 != NULL)
	{
		if (h1 == h2)
			return h1;
		h1 = h1->next;
		h2 = h2->next;
	}
	return NULL;
}

猜你喜欢

转载自blog.csdn.net/jingeche/article/details/81093019
今日推荐