版权声明:本文为博主原创文章,欢迎转载。转载请注明出处 https://blog.csdn.net/jobbofhe/article/details/85214269
数据结构再复习。本文实现了双向链表的基本操作。
【注】:本文标题DSA含义是(Data struct & algorithm)
/**
* 双向链表
* 初始化链表 : A B C D E F G H I J K L M N O P Q R S T U V W X Y
* 输入正整数4: E F G H I J K L M N O P Q R S T U V W X Y Z A B C D
* 输入负整数-4:W X Y Z A B C D E F G H I J K L M N O P Q R S T U V
*/
#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef int Status;
#define TRUE (1)
#define FALSE (0)
typedef struct DoubleNode
{
ElemType data;
struct DoubleNode *pre;
struct DoubleNode *next;
}DoubleNode, *DoubleList;
Status initList(DoubleList *list)
{
// malloc header node
DoubleList pHead = (DoubleList)malloc(sizeof(DoubleNode));
DoubleNode *p, *q;
(*list) = pHead;
(*list)->pre = (*list)->next = NULL;
if(!(*list))
{
return FALSE;
}
p = (*list);
int i = 0;
for (i = 0; i < 26; i++)
{
q = (DoubleNode*)malloc(sizeof(DoubleNode));
if(!q)
{
return FALSE;
}
q->data = 'A' + i;
q->pre = p;
q->next = p->next;
p->next = q;
printf("%c ", p->data);
p = q;
}
// 循环完毕,为节点指向头结点的 next节点,头结点的next节点指尾节点
p->next = (*list)->next;
(*list)->next->pre = p;
printf("\n");
return TRUE;
}
void Sort(DoubleList *list, int i)
{
if (i > 0)
{
printf("i > 0\n");
do
{
(*list) = (*list)->next;
}while(--i);
}
if (i < 0)
{
i = i+26;
printf("i < 0\n");
do
{
(*list) = (*list)->next;
}while(--i);
}
}
void PrintList(DoubleList *list)
{
if(!list)
{
return;
}
int i = 0;
for ( i = 0; i < 26; ++i)
{
(*list) = (*list)->next;
printf("%c ", (*list)->data);
}
}
int main(int argc, char const *argv[])
{
DoubleList *list = (DoubleList*)malloc(sizeof(DoubleList));
initList(list);
int number;
printf("Please input an int number: ");
scanf("%d", &number);
printf("number [%d]\n", number);
Sort(list, number);
PrintList(list);
return 0;
}