#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
typedef int Elemtype;
typedef struct CNode {
Elemtype data;
struct CNode* next;
}CNode,*LinkList;
bool Empty(LinkList L)
{
return (L->next == L);
}
bool List_TailInsert(LinkList& L)
{
L = (CNode*)malloc(sizeof(CNode));
if (NULL == L) return false;
L->next = L;//指向自己
CNode* p = L;
Elemtype e;
printf("INPUT number:\n");
scanf("%d", &e);
while (e != -9999)
{
CNode* s = (CNode*)malloc(sizeof(CNode));
if (NULL == s) return false;
s->data = e;
s->next = p->next;
p->next = s;
p = s;
scanf("%d",&e);
}
p->next = L;//!!!!!!!!!!!指向头节点而不是第一个数据元素
return true;
}
CNode* GetElem(LinkList L, int i)
{
if (i < 0)
{
return NULL;
}
int j = 0;
CNode* p = L;
while (p && j < i)
{
p = p->next;
j++;
}
return p;
}
bool InsertNextNode(CNode* p, Elemtype e)//当前节点后插
{
if (NULL == p) return false;
CNode* q = (CNode*)malloc(sizeof(CNode));
if (NULL == q)
{
return false;
}
q->data = e;
q->next = p->next;
p->next = q;
return true;
}
bool List_IndexInsert(LinkList L, int i, Elemtype e)
{
CNode* p = GetElem(L, i - 1);
InsertNextNode(p, e);
return true;
}
bool printList(LinkList L)
{
if (NULL == L->next) return false;
CNode* p = L->next;
while (p!=L)//
{
printf("%4d", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
LinkList L;
List_TailInsert(L);
List_IndexInsert(L, 5, 60);
printList(L);
return 0;
}
循环单链表-头节点
于 2022-03-24 15:44:05 首次发布