链表结构体实现(单链表,循环链表)

徘徊边缘 提交于 2020-03-08 09:38:16
// 无表头节点的单链表
#include <iostream> 
#include <cstdio>
#include <malloc.h>
using namespace std;

typedef char ListData;

typedef struct node{
	ListData data;
	struct node* link;
}ListNode;

typedef ListNode* LinkList;
LinkList first;

int insert(LinkList& first, ListData x, int i)
{
	ListNode* preNode = first;
	int cnt = 0;
	while(preNode != NULL && cnt < i - 1)
	{
		preNode = preNode -> link;
		cnt ++;
	}
	if(preNode == NULL && first != NULL){
		puts("无效的插入!");
		return false;
	}
	ListNode* newnode = listNode* malloc(sizeof(node));
	newnode->data = x;
	if(i == 1 || first == NULL)
		!!!!为什么不是 i == 0!!!
	{
		newnode->link = first;
		first = newnode;
	}
	else
	{
		newnode->link = preNode->link;
		preNode->link = newnode;
	}
	if(newnode->link == NULL) last = newnode;
	return true;
}

int delete(LinkList& first, int i)
{
	newnode* preNode = first, *deleNode;
	ListData res;
	int cnt = 0;
	while(preNode->link != NULL && cnt < i - 1)
	{
		cnt ++;
		preNode = preNode->link;
	}
	if(preNode == NULL || preNode -> link == NULL)
	{
		puts("无效的删除位置!");
		return false;
	}
	if(preNode == first)
	{
		deleNode = first;
		first = first->link;
		free(deleNode);
		res = preNode->data;
	}
	else
	{
		deleNode = preNode->link;
		preNode->link = deleNode->link;
		free(deleNode);
		res = deleNode->data;
	}
	if(preNode->link == NULL) last = preNode;
	return res;
}


// 循环链表实现约瑟夫循环
#include <iostream>
#include <cstdio>
using namespace std;

typedef int ListData;

typedef struct node
{
	ListData data;
	struct node* link;
}ListNode, *ListLink;

void printList(ListLink& head)
{
	ListLink now = head->link;
	puts("LISTELEMENTS:");
	while(now != head)
	{
		printf("%d ", now->data);
		now = now->link;
	}
	puts("");
}

ListLink createList(int n)
{
	ListLink head = (ListLink) malloc(sizeof (ListNode));
	head->data = 1;
	head->link = head;
	ListLink last = head;
	for (int i = 2; i <= n; i ++)
	{
		ListLink newNode = (ListLink) malloc(sizeof (ListNode));
		if(newNode == NULL) return NULL;
		newNode->data = i;
		last->link = newNode;
		last = newNode;
	}
	last->link = head;
	printList(head);
	return head;
}

void johnsiffe(ListLink& head, int k, int m, int n)
{
	ListLink tail = head;
	while(tail->link != head) tail = tail->link;

	ListLink now = head; int cnt = 1;
	while(now->data != k && cnt <= n)
	{
		cnt ++;
		now = now->link;
		tail = tail->link;
	}
	if(cnt > n)
	{
		puts("Invalid k!");
		return;
	}

	while(now->link != now)
	{
		for (int i = 0; i < m - 1; i ++)
		{
			now = now->link;
			tail = tail->link;
		}
		tail->link = now->link;
		printf("delete node: %d\n", now->data);
		free(now);
		now = tail->link;
	}
	printf("delete node: %d\n", now->data);
	free(now);
}

int main()
{
	int n, k, m;
	scanf("%d%d%d", &n, &k, &m);
	ListLink head = createList(n);
	johnsiffe(head, k, m, n);


	return 0;
}

 

// 带头节点的单链表(AC)
#include <iostream>
#include <cstdio>
using namespace std;

typedef int ListData;

typedef struct node
{
    ListData data;
    struct node* link;
}ListNode, *ListLink;

ListLink init(ListLink& headPointer)
{
    headPointer = (ListLink) malloc(sizeof (ListNode));
    if(headPointer == NULL) return NULL;
    headPointer->link = NULL;
    return headPointer;
}

ListLink Locate(ListLink& headPointer, int i)
{
    int cnt = 0;
    ListLink now = headPointer;
    while(cnt < i && now != NULL)
    {
        cnt ++;
        now = now->link;
    }
    if(cnt == i) return now;
    return NULL;
}

int insert(ListLink& headPointer, int i, ListData x)
{
    ListLink preNode = Locate(headPointer, i - 1);
    if(preNode == NULL) return false;
    ListLink addNode = (ListLink) malloc(sizeof (ListNode));
    addNode->data = x;
    addNode->link = preNode->link;
    preNode->link = addNode;
    return true;
}

int remove(ListLink& headPointer, int i)
{
    ListLink preNode = Locate(headPointer, i - 1);
    if(preNode == NULL || preNode->link == NULL) return false;
    ListLink remNode = preNode->link;
    preNode->link = remNode->link;
    free(remNode);
    return true;
}

ListLink find(ListLink& headPointer, ListData x)
{
    ListLink now = headPointer->link;
    while(now != NULL && now->data != x)
    {
        now = now->link;
    }
    if(now == NULL) return NULL;
    return now;
}

ListLink creatListF(void)
{
    ListLink headPointer = init(headPointer);
    ListData n;
    while(~scanf("%d", &n))
    {
        ListLink newNode = (ListLink) malloc(sizeof (ListNode));
        newNode->data = n;
        newNode->link = headPointer->link;
        headPointer->link = newNode;
    }
    return headPointer;
}

ListLink creatListB(void)
{
    ListLink headPointer = init(headPointer);
    ListData n;
    ListLink last = headPointer;
    while(~scanf("%d", &n))
    {
        ListLink newNode = (ListLink) malloc(sizeof (ListNode));
        newNode->data = n;
        last->link = newNode;
        last = newNode;
    }
    last->link = NULL;
    return headPointer;
}

void printList(ListLink& headPointer)
{
    ListLink now = headPointer->link;
    puts("LISTELEMENTS:");
    while(now != NULL)
    {
        printf("%d ", now->data);
        now = now->link;
    }
    puts("");
}

int main()
{
    ListLink head = creatListB();
    printList(head);

    int insertn;
    scanf("%d", &insertn);
    insert(head, 3, insertn);

    printList(head);

    remove(head, 3);
    printList(head);

    ListLink p = find(head, 5);
    printf("find: %d\n", *p);

    printList(head);



    return 0;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!