#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
struct Node* prev;
// Pointer pointing towards next node
};
//function to print the doubly linked list
void display(struct Node* node)
{
struct Node *end;
printf("List in Forward direction: ");
while (node != NULL)
{
printf(" %d ", node->data);
end = node;
node = node->next;
}
printf("\nList in backward direction: ");
while (end != NULL)
{
printf(" %d ", end->data);
end = end->prev;
}
}
// main function
int main()
{
//creating 4 pointers of type struct Node
//So these can point to address of struct type variable
struct Node* head = NULL;
struct Node* node2 = NULL;
struct Node* node3 = NULL;
struct Node* node4 = NULL;
// allocate 4 nodes in the heap
head = (struct Node*)malloc(sizeof(struct Node));
node2 = (struct Node*)malloc(sizeof(struct Node));
node3 = (struct Node*)malloc(sizeof(struct Node));
node4 = (struct Node*)malloc(sizeof(struct Node));
/* Using malloc method we have created 4 memory blocks
Each memory block is of type struct and has int data and Pointer of type Node
So it can point towards a node type struct.
head node2 node3 node4
| | | |
| | | |
+---++---+-----+ +----+----+----+ +----+----+----+ +----+----+----+
|prev|data|next| |prev|data|next| |prev|data|next| |prev|data|next|
+---++---+-----+ +----+----+----+ +----+----+----+ +----+----+----+
# As of now data has garbage value and its pointer
points towards random addresses*/
head->data = 5; // data set for head node
head->next = node2; // next pointer assigned to address of node2
head->prev = NULL;
node2->data = 10;
node2->next = node3;
node2->prev = head;
node3->data = 12;
node3->next = node4;
node3->prev = node2;
node4->data = 3;
node4->next = NULL;
node4->prev = node3;
//last node assigned to Null as linked list ends here
/*
head node2 node3 node4
| | | |
| | | |
+---++---+-----+ +----+----+----+ +----+----+----+ +----+----+----+
|prev| 5 |next|--->|prev| 10 |next|--->|prev| 12 |next|--->|prev| 3 |next|
+---++---+-----+ +----+----+----+ +----+----+----+ +----+----+----+
*/
display(head);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
C数据结构与算法测试例子
共34个文件
makefile:6个
c:6个
o:6个
需积分: 5 0 下载量 172 浏览量
2023-04-06
13:57:39
上传
评论
收藏 133KB ZIP 举报
温馨提示
C数据结构与算法测试
资源推荐
资源详情
资源评论
收起资源包目录
CDataStruct.zip (34个子文件)
CDataStruct
SingleLinkedLists
SingleLinkedLists 16KB
Linked-List-in-C-Addressses.png 36KB
SingleLinkedLists.pro 96B
Makefile 44KB
Linked-List-in-C-Node.png 32KB
main.o 10KB
main.c 3KB
CDataStruct.pro 148B
Makefile 61KB
Stack
Makefile 44KB
Stack 12KB
Stack.pro 96B
main.o 8KB
main.c 1KB
DoubleLinkedLists
DoubleLinkedLists.pro 96B
Makefile 44KB
DoubleLinkedLists 11KB
main.o 7KB
main.c 3KB
QueueArray
QueueArray.pro 96B
Makefile 44KB
QueueArray 11KB
main.o 8KB
main.c 1KB
.qmake.stash 739B
CDataStruct.pro.user 42KB
QueueLinkedLists
QueueLinkedLists.pro 140B
queue.h 588B
queue.c 1KB
Makefile 44KB
main.o 7KB
main.c 404B
queue.o 12KB
QueueLinkedLists 18KB
共 34 条
- 1
资源评论
bugmaker01
- 粉丝: 11
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功