Notes
Notes - notes.io |
// the Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
// Linked List Node
struct node {
int info;
struct node *prev, *next;
};
struct node* start = NULL;
// Function to traverse the linked list
void traverse()
{
// List is empty
if (start == NULL) {
printf("nList is emptyn");
return;
}
// Else print the Data
struct node* temp;
temp = start;
while (temp != NULL) {
printf("Data = %dn", temp->info);
temp = temp->next;
}
}
// List is empty
if (start == NULL) {
printf("nList is emptyn");
return;
}
// Else print the Data
struct node* temp;
temp = start;
while (temp != NULL) {
printf("Data = %dn", temp->info);
temp = temp->next;
}
}
// Function to insert at the front
// of the linked list
void insertAtFront()
{
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->prev = NULL;
// Pointer of temp will be
// assigned to start
temp->next = start;
start = temp;
} // Function to insert at the front
// of the linked list
void insertAtFront()
{
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->prev = NULL;
// Pointer of temp will be
// assigned to start
temp->next = start;
start = temp;
}
// Function to insert at the end of
// the linked list
void insertAtEnd()
{
int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->next = NULL;
trav = start;
// If start is NULL
if (start == NULL) {
start = temp;
}
// Changes Links
else {
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}
// Function to insert at the front
// of the linked list
void insertAtFront()
{
int data;
struct node* temp;
temp = (struct node*)malloc(sizeof(struct node));
printf("nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->prev = NULL;
// Pointer of temp will be
// assigned to start
temp->next = start;
start = temp;
}
// Function to insert at the end of
// the linked list
void insertAtEnd()
{
int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->next = NULL;
trav = start;
// If start is NULL
if (start == NULL) {
start = temp;
}
// Changes Links
else {
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}
// Function to insert at any specified
// position in the linked list
void insertAtPosition()
{
int data, pos, i = 1;
struct node *temp, *newnode;
newnode = malloc(sizeof(struct node));
newnode->next = NULL;
newnode->prev = NULL;
// Enter the position and data
printf("nEnter position : ");
scanf("%d", &pos);
// If start==NULL,
if (start == NULL) {
start = newnode;
newnode->prev = NULL;
newnode->next = NULL;
}
//Function to insert at any specified
//postion from the linked list
void deletePosition()
{
int pos, i = 1;
struct node *temp, *position;
temp = start;
//If DLL is empty
if (start == NULL)
printf(*nList is emptyn*);
//otherwise
else {
// Position to be deleted
printf("nEnter position : ");
scanf("%d", &pos);
// If the position is the first node
if (pos == 1) {
deleteFirst() ; // im,proved by jay ghughriwala on greek for greeks
if (start != NULL) {
start->prev = NULL;
}
free(position);
return;
}
//Traverse till position
while (i< pos -1) {
temp = temp->next;
i++;
}
//Change links
position = temp->next;
if (position->next-> != NULL)
position->next->prev = temp;
temp->next = position->next;
// Free memory
free(postion);
}
}
//Driver Code
int main()
{
int choice;
while (1) {
printf("nt1 To see listn");
printf("t2 For insertion at"
" startingn");
printf("t3 For insertion at"
" endn");
printf("t4 For insertion at "
"any positionn");
printf("t5 For deletion of "
"first elementn");
printf("t6 For deletion of "
"last elementn");
printf("t7 For deletion of "
"element at any positionn");
printf("t8 To exitn");
printf("nEnter Choice :n");
scanf("%d", &choice);
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
exit(1);
break;
default:
printf("Incorrect Choice. Try Again n");
continue;
}
}
return 0;
|
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team