Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d8caa4f

Browse files
doubly linkedlist efficient version
1 parent 0d0adad commit d8caa4f

File tree

2 files changed

+126
-143
lines changed

2 files changed

+126
-143
lines changed
Lines changed: 125 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,144 +1,126 @@
1-
// A complete working C program to demonstrate all insertion methods
2-
#include <stdio.h>
3-
#include <stdlib.h>
4-
5-
// A linked list node
6-
struct Node {
7-
int data;
8-
struct Node* next;
9-
struct Node* prev;
10-
};
11-
12-
/* Given a reference (pointer to pointer) to the head of a list
13-
and an int, inserts a new node on the front of the list. */
14-
void push(struct Node** head_ref, int new_data)
15-
{
16-
/* 1. allocate node */
17-
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
18-
19-
/* 2. put in the data */
20-
new_node->data = new_data;
21-
22-
/* 3. Make next of new node as head and previous as NULL */
23-
new_node->next = (*head_ref);
24-
new_node->prev = NULL;
25-
26-
/* 4. change prev of head node to new node */
27-
if ((*head_ref) != NULL)
28-
(*head_ref)->prev = new_node;
29-
30-
/* 5. move the head to point to the new node */
31-
(*head_ref) = new_node;
32-
}
33-
34-
/* Given a node as prev_node, insert a new node after the given node */
35-
void insertAfter(struct Node* prev_node, int new_data)
36-
{
37-
/*1. check if the given prev_node is NULL */
38-
if (prev_node == NULL) {
39-
printf("the given previous node cannot be NULL");
40-
return;
41-
}
42-
43-
/* 2. allocate new node */
44-
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
45-
46-
/* 3. put in the data */
47-
new_node->data = new_data;
48-
49-
/* 4. Make next of new node as next of prev_node */
50-
new_node->next = prev_node->next;
51-
52-
/* 5. Make the next of prev_node as new_node */
53-
prev_node->next = new_node;
54-
55-
/* 6. Make prev_node as previous of new_node */
56-
new_node->prev = prev_node;
57-
58-
/* 7. Change previous of new_node's next node */
59-
if (new_node->next != NULL)
60-
new_node->next->prev = new_node;
61-
}
62-
63-
/* Given a reference (pointer to pointer) to the head
64-
of a DLL and an int, appends a new node at the end */
65-
void append(struct Node** head_ref, int new_data)
66-
{
67-
/* 1. allocate node */
68-
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
69-
70-
struct Node* last = *head_ref; /* used in step 5*/
71-
72-
/* 2. put in the data */
73-
new_node->data = new_data;
74-
75-
/* 3. This new node is going to be the last node, so
76-
make next of it as NULL*/
77-
new_node->next = NULL;
78-
79-
/* 4. If the Linked List is empty, then make the new
80-
node as head */
81-
if (*head_ref == NULL) {
82-
new_node->prev = NULL;
83-
*head_ref = new_node;
84-
return;
85-
}
86-
87-
/* 5. Else traverse till the last node */
88-
while (last->next != NULL)
89-
last = last->next;
90-
91-
/* 6. Change the next of last node */
92-
last->next = new_node;
93-
94-
/* 7. Make last node as previous of new node */
95-
new_node->prev = last;
96-
97-
return;
98-
}
99-
100-
// This function prints contents of linked list starting from the given node
101-
void printList(struct Node* node)
102-
{
103-
struct Node* last;
104-
printf("\nTraversal in forward direction \n");
105-
while (node != NULL) {
106-
printf(" %d ", node->data);
107-
last = node;
108-
node = node->next;
109-
}
110-
111-
printf("\nTraversal in reverse direction \n");
112-
while (last != NULL) {
113-
printf(" %d ", last->data);
114-
last = last->prev;
115-
}
116-
}
117-
118-
/* Drier program to test above functions*/
119-
int main()
120-
{
121-
/* Start with the empty list */
122-
struct Node* head = NULL;
123-
124-
// Insert 6. So linked list becomes 6->NULL
125-
append(&head, 6);
126-
127-
// Insert 7 at the beginning. So linked list becomes 7->6->NULL
128-
push(&head, 7);
129-
130-
// Insert 1 at the beginning. So linked list becomes 1->7->6->NULL
131-
push(&head, 1);
132-
133-
// Insert 4 at the end. So linked list becomes 1->7->6->4->NULL
134-
append(&head, 4);
135-
136-
// Insert 8, after 7. So linked list becomes 1->7->8->6->4->NULL
137-
insertAfter(head->next, 8);
138-
139-
printf("Created DLL is: ");
140-
printList(head);
141-
142-
getchar();
143-
return 0;
1+
2+
#include<stdio.h>
3+
struct node
4+
{
5+
int data;
6+
struct node *link;
7+
};
8+
struct node *start=NULL;
9+
main()
10+
{
11+
int choice;
12+
struct node *q;
13+
while(1)
14+
{
15+
q=(struct node*)malloc(sizeof(struct node));
16+
q->link=NULL;
17+
printf("\n1.insertatend\n2.insertatbeg\n3.delfront\n4.delrear\n5.display\n6.free\n");
18+
printf("enter the choice\n");
19+
scanf("%d",&choice);
20+
switch(choice)
21+
{
22+
case 1:insertatend(q);
23+
printf("\n INSERTION SUCCESSFUL\n");
24+
break;
25+
case 2:insertatbeg(q);
26+
printf("\n INSERTION SUCCESSFUL\n");
27+
break;
28+
case 3:delfront();
29+
break;
30+
case 4:delrear();
31+
break;
32+
case 5:display();
33+
break;
34+
case 6:freed();
35+
break;
36+
default:printf("\n invalid choice\n");
37+
}
38+
}
39+
}
40+
void insertatend(struct node *q)
41+
{
42+
struct node *r;
43+
printf("\nenter the data which you want to store\n");
44+
scanf("%d",&q->data);
45+
if(start==NULL)
46+
{
47+
start=q;
48+
}
49+
else
50+
{
51+
r=start;
52+
while(r->link!=NULL)
53+
r=r->link;
54+
r->link=q;
55+
}
56+
57+
}
58+
void insertatbeg(struct node *q)
59+
{
60+
printf("\nenter the data which you want to store\n");
61+
scanf("%d",&q->data);
62+
if(start==NULL)
63+
start=q;
64+
else
65+
{
66+
q->link=start;
67+
start=q;
68+
}
69+
70+
}
71+
void delfront()
72+
{
73+
struct node *r;
74+
if(start==NULL)
75+
printf("\nthe list is empty\n");
76+
else
77+
{
78+
r=start;
79+
start=start->link;
80+
printf("the deleted element is %d",r->data);
81+
free(r);
82+
}
83+
}
84+
void delrear()
85+
{
86+
struct node *r,*s;
87+
int count=0,i;
88+
r=start;
89+
if(start==NULL)
90+
printf("\nthe list is empty\n");
91+
else
92+
{
93+
while(r->link!=NULL)
94+
{
95+
r=r->link;
96+
count++;
97+
}
98+
printf("the deleted element is %d",r->data);
99+
free(r);
100+
s=start;
101+
for(i=1;i<=count-1;i++)
102+
{
103+
s=s->link;
104+
}
105+
s->link=NULL;
106+
}
107+
}
108+
void display()
109+
{
110+
struct node *r;
111+
if(start==NULL)
112+
printf("\nthe list is empty\n");
113+
else
114+
{
115+
r=start;
116+
while(r!=NULL)
117+
{
118+
printf("\t%d\t",r->data);
119+
r=r->link;
120+
}
121+
}
122+
}
123+
freed()
124+
{
125+
free(start);
144126
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a doubly linkedlist program made in c language, it uses the concept of structure,pointer,dynamic memory allocation etc.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /