싱글 체인 시계 (표 두 결점)
4058 단어 데이터 구조
#include
#include
#define OK 0
#define ERROR -1
#define MALLOC_ERROR -2
typedef int ElementType; //c ElementType , ,
typedef struct node
{
ElementType data; //
struct node *next; //
}Node;
typedef Node *PNode; //
//
int Create_List_Head(PNode h, ElementType data)
{
if (h == NULL)
{
return ERROR;
}
PNode node = (PNode)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
return MALLOC_ERROR;
}
node->data = data;
node->next = h->next;
h->next = node;
return OK;
}
//
int Create_List_Tail(PNode h, ElementType data)
{
if (h == NULL)
{
return ERROR;
}
PNode node = (PNode)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
return MALLOC_ERROR;
}
node->data = data;
node->next = NULL;
//
PNode temp = h;
while (temp->next)
{
temp = temp->next;
}
temp->next = node;
return OK;
}
// pos
int Insert_Node(PNode h, int pos, ElementType data)
{
PNode temp = h;
int k = 0;
// pos-1
while (temp && k < pos-1)
{
temp = temp->next;
k++;
}
if (temp == NULL) //
{
printf ("
");
return ERROR;
}
PNode node = (PNode)malloc(sizeof(Node)/sizeof(char));
if (node == NULL)
{
return MALLOC_ERROR;
}
node->data = data;
node->next = temp->next;
temp->next = node;
return OK;
}
// pos
int Delete_Node(PNode h, int pos)
{
PNode temp = h;
int k = 0;
// , temp
while (temp && k < pos-1)
{
temp = temp->next;
k++;
}
if (temp == NULL || temp->next == NULL) //
{
printf ("
");
return ERROR;
}
PNode p = temp->next;
temp->next = p->next;
free(p);
p = NULL;
return OK;
}
//
int Inverse_List(PNode h)
{
//h == NULL ,
//h->next == NULL
//h->next->next == NULL
if (h->next == NULL || h->next->next == NULL)
{
return OK;
}
PNode pre = h->next;
PNode cur = pre->next;
PNode next = NULL;
while(cur)
{
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
/* h->next->next == NULL; // !!!,
h->next = pre; // , next
return OK; */
h->next->next = NULL;
h->next = pre; // , next
return OK;
}
// ,
PNode Search(PNode h, ElementType data)
{
if (h == NULL)
{
return NULL;
}
PNode temp = h->next;
while(temp)
{
if (temp->data == data)
{
return temp;
}
temp = temp->next;
}
return NULL;
}
//
int ListLen(PNode h)
{
int len = 0;
PNode temp = h->next;
while(temp)
{
temp = temp->next;
len++;
}
return len;
}
//
void Display(PNode h)
{
if (h == NULL)
{
return;
}
PNode temp = h->next; //
while (temp)
{
printf ("%4d",temp->data);
temp = temp->next;
}
printf ("
");
}
int main()
{
PNode head_node = (PNode)malloc(sizeof(Node)/sizeof(char));
if (head_node == NULL)
{
return MALLOC_ERROR;
}
head_node->next = NULL; //
int i = 0;
for (i = 0; i < 10; i++)
{
/* //
if (Create_List_Head(head_node,i) != OK)
{
return ERROR;
} */
//
if (Create_List_Tail(head_node,i) != OK)
{
return ERROR;
}
}
/* // pos
if (Insert_Node(head_node, 6, 7) != OK)
{
return ERROR;
}
// pos
if (Delete_Node(head_node,4) != OK)
{
return ERROR;
} */
//
if (Inverse_List(head_node) != OK)
{
return ERROR;
}
Display(head_node);
//
PNode p = Search(head_node, 5);
if(p == NULL)
{
printf("
");
}
else
{
printf("%d
",p->data);
}
//
int len = ListLen(head_node);
printf("%d
",len);
return 0;
}