Title here
Summary here
A node
data type is defined using a struct
that contains two elements:
int
) to store the actual data of the node.next
) of type struct node*
that holds the memory location of the next node in the list.To manage the list, three pointers of type struct node*
are used:
*new
: Holds the address of a newly created node (allocated via malloc
).*temp
: Holds and links the old node, used to link to the new node.*head
: Points to the first node of the list and is used for iteration. It is never incremented during traversal.*tail
: Points to the Last node of the list, useful for deletion and insertion at end.
Initially, all pointers (head
, temp
, new
, tail
) are set to NULL
.The steps to create a new node using create
function :
new = (struct node*) malloc(sizeof(struct node));
malloc()
to allocate memory for the node with size of `struct node.malloc()
function returns a void*
, which is typecasted into a (struct node*)
and assigned to new
pointernew
now points to the memory location where the new node is stored..
or ->
methodsnew->data
: Assign the value entered by the user.new->next
: Initialize to NULL
since this is the only node at the moment.head == NULL
(i.e., the list is empty), new
is assigned to head
and temp
.if (head == NULL) {
head = temp = new;
}
This makes the newly created node the first node in the list and there is no further pointers inside to update.
Subsequent Insertions:
For other insertions, temp
holds the address of the previous node. The new node’s address is assigned to the next
pointer within the previous node:
Then, temp
is updated to point to the new node to be used for the next creation.
else {
temp->next = new;
temp = new;
}
To traverse and display the elements of the linked list:
te
) to traverse the list, starting from head
:struct node* te;
te = head;
head == NULL
, the list is empty, so nothing is displayed.while
loop is used to traverse the list and display the node data:while (te != NULL) {
printf("%d -> ", te->data);
te = te->next;
}
te
becomes NULL
, indicating the end of the list.#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*new=NULL, *head=NULL, *temp=NULL;
void Create(int a)
{
//struct node *temp;
new = (struct node*) malloc(sizeof(struct node));
new->data = a;
new->next = NULL;
printf("%d \n", new->data);
printf("%d \n", new->next);
if(head == NULL)
{
head = new;
temp = new;
}
else
{
temp->next = new;
temp = new;
}
}
void Display()
{
struct node *te;
te=head;
if(te ==NULL)
{
printf("Currently list is empty\n");
printf("***************************\n");
}
else
{
while(te !=NULL)
{
printf("%d-->",te->data);
te=te->next;
}
printf("\n\n");
}
}
void main()
{
int a;
while(1)
{
int ch;
printf("\n1.Creating Linked List\n");
printf("2.Display\n");
printf("\nEnter choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the element to be inserted \n");
scanf("%d", &a);
Create(a);
break;
case 2:
Display();
break;
default: printf("Enter the choice Correctly");
break;
}
}
}
#include <stdio.h>
#include <stdlib.h>
void create(int x);
void display();
void main()
{
int val;
while(1)
{
int ch;
printf("Make a Choise:\n");
printf("0 = Exit\n1 = Add Value\t2 = Display all Values\n");
scanf("%d", &ch);
switch(ch)
{
case 0:
exit(0);
case 1:
printf("\nEnter the value to be added: \n");
scanf("%d", &val);
create(val);
break;
case 2:
display();
break;
}
}
}
struct node
{
int data;
struct node *next;
} *new=NULL, *temp=NULL, *head=NULL;
void create(int x)
{
new = (struct node*) malloc(sizeof(struct node));
new->data=x;
new->next=NULL;
printf("\tThe Value %d has been inserted\n", x);
if(head == NULL)
{
head = new;
temp = new;
}
else
{
printf("\n\t%p is the previous node, ", temp);
printf("linked with new node %p. \n", new);
temp->next=new;
temp=new;
}
}
void display()
{
struct node *tem;
tem = head;
if (tem == NULL)
{
printf("\n\tThe List is empty\n\n");
}
else
{
printf("\tThe Values in the list are: ");
while(tem != NULL)
{
printf("%d ", tem->data);
tem=tem->next;
}
printf("\n\n");
}
}
#include <stdio.h>
#include <stdlib.h>
void create(int x);
void display();
void main()
{
int val;
while(1)
{
int ch;
printf("Make a Choice:\n");
printf("0 = Exit\n1 = Add Value\t2 = Display all Values\n");
scanf("%d", &ch);
switch(ch)
{
case 0:
exit(0);
case 1:
printf("\nEnter the value to be added: \n");
scanf("%d", &val);
create(val);
break;
case 2:
display();
break;
}
}
}
struct node
{
int data;
struct node *next;
} *head = NULL;
void create(int x)
{
struct node *new = (struct node*) malloc(sizeof(struct node));
new->data = x;
new->next = NULL;
printf("\tThe Value %d has been inserted\n", x);
if (head == NULL)
{
head = new;
printf("New node %p is now the head of the list.\n", new);
}
else
{
struct node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = new;
printf("\t%p is the previous node, linked with new node %p. \n", temp, new);
}
}
void display()
{
struct node *tem = head;
if (tem == NULL)
{
printf("The List is empty\n");
}
else
{
printf("\tThe Values in the list are: ");
while (tem != NULL)
{
printf("%d ", tem->data);
tem = tem->next;
}
printf("\n");
}
}