c语言双向链表问题
#include <iostream>using namespace std;
typedef struct Node
{
char str;
struct Node *next;
struct Node *front;
}node , *L_node;
int Initiate(L_node *L)
{
node *p = NULL, *q = NULL;
*L = (L_node)malloc(sizeof(node));
(*L)->next = (*L)->front = NULL;
p = (*L);
for (int i = 0; i < 26; ++i)
{
q = (node *)malloc(sizeof(node));
q->str = 'A' + i;
q->front = p;
q->next = p->next;
p->next = q;
p = q;
}
p->next = (*L)->next;
(*L)->next->front = q;
return 1;
}
void Print(L_node *L, int n)
{
if (n > 0)
{
do
{
(*L) = (*L)->next;
} while (--n);
}
if (n < 0)
{
do
{
(*L) = (*L)->front;
} while (++n);
}
}
int main()
{
L_node head;
Initiate(&head);
Print(&head , -10);
for (int i = 0; i < 26; ++i)
{
head = head->next;
cout << head->str << " ";
}
cout << " =========== " << endl;
for (int i = 0; i < 5; ++i)
{
head = head->front;
cout << head->str << " ";
}
system("pause");
return 0;
}