0% found this document useful (0 votes)
70 views

Experiment 1: Write A C Programme To Perform The Following Operation On A Single Linked List (I) Creation (Ii) Insertion (Iii) Traverse Program

The document describes two C programs to perform operations on linked lists. The first program creates a single linked list, and includes functions for insertion at the beginning, end, and any position in the list. It also includes a function to traverse and display the list. The second program creates a doubly linked list, and includes functions for insertion at the head and tail of the list. It includes functions to traverse the list in the forward and backward directions and print the elements.

Uploaded by

raghu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Experiment 1: Write A C Programme To Perform The Following Operation On A Single Linked List (I) Creation (Ii) Insertion (Iii) Traverse Program

The document describes two C programs to perform operations on linked lists. The first program creates a single linked list, and includes functions for insertion at the beginning, end, and any position in the list. It also includes a function to traverse and display the list. The second program creates a doubly linked list, and includes functions for insertion at the head and tail of the list. It includes functions to traverse the list in the forward and backward directions and print the elements.

Uploaded by

raghu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment 1: WRITE A C PROGRAMME TO PERFORM THE FOLLOWING OPERATION ON A

SINGLE LINKED LIST (I) CREATION (II) INSERTION (III) TRAVERSE

Program:

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *link;
};
struct node *head,*ptr,*temp;
void insert_beg();
void insert_end();
void insert_any();
void display();
void main()
{
int item,ch,c=1;
clrscr();
head->info=NULL;
head->link=NULL;
while(c==1)
{
printf("\nDISPLAY MENUES\n");
printf("\n1.INSERT_BEG\n");
printf("\n2.INSERT_END\n");
printf("\n3.INSERT_ANY\n");
printf("\n4.DISPLAY\n");
printf("\nEnter Your Choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert_beg();
break;
case 2: insert_end();
break;
case 3: insert_any();
break;
case 4: display();
break;
}
printf("\nDo You Want To Continue(1/0)");
scanf("%d",&c);
}
getch();
}
/************INSERT AT BEGING***************/
void insert_beg()
{
int item;
printf("\nEnter The Item\n");
scanf("%d",&item);
temp=(struct node*)malloc(sizeof(struct node));
temp->info=item;
temp->link=head;
head=temp;
}
/************INSERT AT END***************/
void insert_end()
{
int item;
printf("\nEnter The Item\n");
scanf("%d",&item);
temp=(struct node*)malloc(sizeof(struct node));
ptr=head;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
temp->info=item;
temp->link=ptr->link;//sending last node null into new node link i.e null
ptr->link=temp;// sending new node address into previous last node link
}
/************INSERT AT ANY POSITION***************/
void insert_any()
{
int item,key;
printf("\nEnter The Item\n");
scanf("%d",&item);
printf("\nEnter The Key Element:\n");
scanf("%d",&key);
temp=(struct node*)malloc(sizeof(struct node));
ptr=head;
while(ptr->link!=NULL&&ptr->info!=key)
{
ptr=ptr->link;
}
if(ptr->info==key)
{
temp->info=item;
temp->link=ptr->link;//sending address of current node into new node
ptr->link=temp;//sending the address of new node into current node
}
else
{
printf("Key Element Is Not Found:%d",key);
}
}
/************DISPLAY THE ELEMENTS***************/

void display()
{
int item;
ptr=head;
while(ptr!=NULL)
{
printf("%d",ptr->info);

ptr=ptr->link;
}
}
PROGRAM : 2 : Write a C program that uses functions to perform the following operations on
doubly linked list.:
i) Creation ii) Insertion iii) Traversal in both ways

/* Doubly Linked List implementation */


#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
struct Node* head; // global variable - pointer to head node.
//Creates a new Node and returns pointer to it.
struct Node* GetNewNode(int x) {
struct Node* newNode= (struct Node*)malloc(sizeof(struct Node));
newNode->data = x;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
//Inserts a Node at head of doubly linked list
void InsertAtHead(int x) {
struct Node* newNode = GetNewNode(x);
if(head == NULL) {
head = newNode;
return;
}
head->prev = newNode;
newNode->next = head;
head = newNode;
}
//Inserts a Node at tail of Doubly linked list
void InsertAtTail(int x) {
struct Node* temp = head;
struct Node* newNode = GetNewNode(x);
if(head == NULL) {
head = newNode;
return;
}
while(temp->next != NULL)
temp = temp->next; // Go To last Node
temp->next = newNode;
newNode->prev = temp;
}
//Prints all the elements in linked list in forward traversal order
void Print() {
struct Node* temp = head;
printf("Forward: ");
while(temp != NULL) {
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
//Prints all elements in linked list in reverse traversal order.
void ReversePrint() {
struct Node* temp = head;
if(temp == NULL)
return; // empty list, exit
// Going to last Node
while(temp->next != NULL) {
temp = temp->next;
}
// Traversing backward using prev pointer
printf("Reverse: ");
while(temp != NULL) {
printf("%d ",temp->data);
temp = temp->prev;
}
printf("\n");
}
int main()
{
/*Driver code to test the implementation*/
head = NULL; // empty list. set head as NULL.
// Calling an Insert and printing list both in forward as well as reverse direction.
InsertAtTail(2); Print(); ReversePrint();
InsertAtTail(4); Print(); ReversePrint();
InsertAtHead(6); Print(); ReversePrint();
InsertAtTail(8); Print(); ReversePrint();
}

You might also like