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

ds

The document contains multiple C programs that demonstrate various operations on binary trees and linked lists, including creating nodes, inserting at different positions (beginning, middle, end), deleting nodes (first, middle, last), and displaying the list. Each program is structured to prompt the user for input and perform the specified operations on the data structures. The code snippets utilize standard libraries and dynamic memory allocation for managing nodes.

Uploaded by

arpitchristian00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ds

The document contains multiple C programs that demonstrate various operations on binary trees and linked lists, including creating nodes, inserting at different positions (beginning, middle, end), deleting nodes (first, middle, last), and displaying the list. Each program is structured to prompt the user for input and perform the specified operations on the data structures. The code snippets utilize standard libraries and dynamic memory allocation for managing nodes.

Uploaded by

arpitchristian00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

1.#include<stdio.

h>

#include<conio.h>

void main()

{ int l1,lc,rc;

char a[16]={' ','+','-','*','A','B','C','/',' ',' ',' ',' ',' ',' ','E','F'};

clrscr();

printf("enter location of parent node");

scanf("%d",&l1);

lc=2*l1;

rc=(2*l1)+1;

printf("\n location of parent node is: %d",l1);

printf("\n parent node of location:%c",a[l1]);

printf("\n left childnode at location2:%c",a[lc]);

printf("\n right child node at location3:%c",a[rc]);

getch(); }

2.#include<stdio.h>

#include<conio.h>

void main()

{ int l1,lc,rc;

char a[16]={' ','+','-','*','A','B','C','/',' ',' ',' ',' ',' ',' ','E','F'};

clrscr();

printf("enter location of child:");

scanf("%d",&lc);

l1=lc/2;

printf("\n location of left child node is: %d",lc);

printf("\n parent node at location:%c",a[l1]);

printf("\n left childnode at location:%c",a[lc]);

getch(); }

3.#include<stdio.h>

#include<stdlib.h>

struct node

{ int data;

struct node *next;

}*head;
void create(int n)

{ struct node *newNode,*temp;

int data,i;

head=(struct node*)malloc(sizeof(struct node));

if(head==NULL)

{ printf("memory underflow:No insertion"); }

else

{ printf("enter data of node 1:");

scanf("%d",&data);

head->data=data;

head->next=NULL;

temp=head;

for(i=2;i<=n;i++)

{ newNode=(struct node *)malloc(sizeof(struct node));

if(newNode==NULL)

{ printf("memory underflow:No insertion");

break; }

else

{ printf("enter data of node %d:",i);

scanf("%d",&data);

newNode->data=data;

newNode->next=NULL;

temp->next=newNode;

temp=temp->next;

} printf("SINGLE LINKED LIST CREATED\n");

} void insertatbegining(int data)

struct node *newNode;

newNode=(struct node*)malloc(sizeof(struct node));

if(newNode==NULL)

{ printf("memory underflow:No insertion");


}

else

{ newNode->data=data;

newNode->next=head;

head=newNode;

printf("Data Inserted");

void display()

{ struct node *temp;

if(head==NULL)

{ printf("List is empty");

else

{ temp=head;

while(temp!=NULL)

{ printf("Data=%d\n",temp->data);

temp=temp->next;

void main()

{ int n,data;

clrscr();

printf("enter no. of nodes:");

scanf("%d",&n);

create(n);

printf("\nData in list\n");

display();

printf("\nenter data to insert at begining:");

scanf("%d",&data);

insertatbegining(data);

printf("\nData in list\n");

display();
getch();

4.#include<stdio.h>

#include<stdlib.h>

struct node

{ int data;

struct node *next;

}*head;

void create(int n)

{ struct node *newNode,*temp;

int data,i;

head=(struct node*)malloc(sizeof(struct node));

if(head==NULL)

{ printf("memory underflow:No insertion");

else

{ printf("enter data of node 1:");

scanf("%d",&data);

head->data=data;

head->next=NULL;

temp=head;

for(i=2;i<=n;i++)

{ newNode=(struct node *)malloc(sizeof(struct node));

if(newNode==NULL)

{ printf("memory underflow:No insertion");

break;

else

{ printf("enter data of node %d:",i);

scanf("%d",&data);

newNode->data=data;

newNode->next=NULL;
temp->next=newNode;

temp=temp->next;

} printf("SINGLE LINKED LIST CREATED\n");

void insertatmiddle(int data,int position)

{ int i;

struct node *newNode,*temp;

newNode=(struct node*)malloc(sizeof(struct node));

if(newNode==NULL)

{ printf("memory underflow:No insertion");

else

{ newNode->data=data;

newNode->next=NULL;

temp=head;

for(i=2;i<=position-1;i++)

{ temp=temp->next;

if(temp==NULL)

{ break;

if(temp!=NULL)

{ newNode->next=temp->next;

temp->next=newNode;

printf("Data Inserted");

else

{ printf("Unable to insert data ar given position");

}
}

void display()

{ struct node *temp;

if(head==NULL)

printf("List is empty");

else

{ temp=head;

while(temp!=NULL)

{ printf("Data=%d\n",temp->data);

temp=temp->next;

void main()

{ int n,data,position;

clrscr();

printf("enter no. of nodes:");

scanf("%d",&n);

create(n);

printf("\nData in list\n");

display();

printf("\nenter data to insert at middle:");

scanf("%d",&data);

printf("\nenter position to insert new node:");

scanf("%d",&position);

insertatmiddle(data,position);

printf("\nData in list\n");

display();

getch();

5.#include<stdio.h>

#include<stdlib.h>
struct node

{ int data;

struct node *next;

}*head;

void create(int n)

{ struct node *newNode,*temp;

int data,i;

head=(struct node*)malloc(sizeof(struct node));

if(head==NULL)

{ printf("memory underflow:No insertion");

else

{ printf("enter data of node 1:");

scanf("%d",&data);

head->data=data;

head->next=NULL;

temp=head;

for(i=2;i<=n;i++)

{ newNode=(struct node *)malloc(sizeof(struct node));

if(newNode==NULL)

{ printf("memory underflow:No insertion");

break;

else

{ printf("enter data of node %d:",i);

scanf("%d",&data);

newNode->data=data;

newNode->next=NULL;

temp->next=newNode;

temp=temp->next;

} printf("SINGLE LINKED LIST CREATED\n");


}

void insertatend(int data)

{ struct node *newNode,*temp;

newNode=(struct node*)malloc(sizeof(struct node));

if(newNode==NULL)

{ printf("memory underflow:No insertion");

else

{ newNode->data=data;

newNode->next=NULL;

temp=head;

while(temp->next!=NULL)

{ temp=temp->next;

temp->next=newNode;

printf("Data Inserted");

void display()

struct node *temp;

if(head==NULL)

{ printf("List is empty");

else

{ temp=head;

while(temp!=NULL)

{ printf("Data=%d\n",temp->data);

temp=temp->next;

}
}

void main()

{ int n,data;

clrscr();

printf("enter no. of nodes:");

scanf("%d",&n);

create(n);

printf("\nData in list\n");

display();

printf("\nenter data to insert at end:");

scanf("%d",&data);

insertatend(data);

printf("\nData in list\n");

display();

getch();

6.#include<stdio.h>

#include<stdlib.h>

struct node

{ int data;

struct node *next;

}*head;

void create(int n)

{ struct node *newNode,*temp;

int data,i;

head=(struct node*)malloc(sizeof(struct node));

if(head==NULL)

{ printf("memory underflow:No insertion");

else

{ printf("enter data of node 1:");

scanf("%d",&data);

head->data=data;

head->next=NULL;
temp=head;

for(i=2;i<=n;i++)

{ newNode=(struct node *)malloc(sizeof(struct node));

if(newNode==NULL)

{ printf("memory underflow:No insertion");

break;

else

{ printf("enter data of node %d:",i);

scanf("%d",&data);

newNode->data=data;

newNode->next=NULL;

temp->next=newNode;

temp=temp->next;

printf("SINGLE LINKED LIST CREATED\n");

void deletefirstnode()

{ struct node *todelete;

if(head==NULL)

{ printf("List is already empty");

else

{ todelete=head;

head=head->next;

printf("\nData deleted=%d\n",todelete->data);

free(todelete);

printf("First Node Deleted");

}
void display()

{ struct node *temp;

if(head==NULL)

{ printf("List is empty");

else

{ temp=head;

while(temp!=NULL)

{ printf("Data=%d\n",temp->data);

temp=temp->next;

void main()

{ int n,ch;

clrscr();

printf("enter no. of nodes:");

scanf("%d",&n);

create(n);

printf("\nData in list\n");

display();

printf("\nPress 1 to delete first node:");

scanf("%d",&ch);

if(ch==1)

{ deletefirstnode();

printf("\nData in list\n");

display();

getch();

7.#include<stdio.h>

#include<stdlib.h>

struct node

{ int data;
struct node *next;

}*head;

void create(int n)

{ struct node *newNode,*temp;

int data,i;

head=(struct node*)malloc(sizeof(struct node));

if(head==NULL)

{ printf("memory underflow:No insertion");

else

printf("enter data of node1:");

scanf("%d",&data);

head->data=data;

head->next=NULL;

temp=head;

for(i=2;i<=n;i++)

{ newNode=(struct node *)malloc(sizeof(struct node));

if(newNode==NULL)

{ printf("memory underflow:No insertion");

break;

else

{ printf("enter data of node %d:",i);

scanf("%d",&data);

newNode->data=data;

newNode->next=NULL;

temp->next=newNode;

temp=temp->next;

printf("SINGLE LINKED LIST CREATED\n");

}
}

void deletemiddlenode(int position)

{ int i;

struct node *todelete,*prevnode;

if(head==NULL)

{ printf("List is already empty");

else

{ todelete=head;

prevnode=head;

for(i=2;i<=position;i++)

{ prevnode=todelete;

todelete=todelete->next;

if(todelete==NULL)

break;

if(todelete!=NULL)

{ if(todelete==head)

{ head=head->next;

prevnode->next=todelete->next;

todelete->next=NULL;

free(todelete);

printf(" Node Deleted from middle of list\n");

else

{ printf("Invalid position");

void display()
{ struct node *temp;

if(head==NULL)

{ printf("List is empty");

else

temp=head;

while(temp!=NULL)

{ printf("Data=%d\n",temp->data);

temp=temp->next;

void main()

{ int n,position;

clrscr();

printf("enter no. of nodes:");

scanf("%d",&n);

create(n);

printf("\nData in list\n");

display();

printf("\nenter node position to delete:");

scanf("%d",&position);

deletemiddlenode(position);

printf("\nData in list\n");

display();

getch();

8.#include<stdio.h>

#include<stdlib.h>

struct node

{ int data;

struct node *next;

}*head;
void create(int n)

{ struct node *newNode,*temp;

int data,i;

head=(struct node*)malloc(sizeof(struct node));

if(head==NULL)

{ printf("memory underflow:No insertion");

else

{ printf("enter data of node 1:");

scanf("%d",&data);

head->data=data;

head->next=NULL;

temp=head;

for(i=2;i<=n;i++)

{ newNode=(struct node *)malloc(sizeof(struct node));

if(newNode==NULL)

{ printf("memory underflow:No insertion");

break;

else

{ printf("enter data of node %d:",i);

scanf("%d",&data);

newNode->data=data;

newNode->next=NULL;

temp->next=newNode;

temp=temp->next;

printf("SINGLE LINKED LIST CREATED\n");

void deletelastnode()

{ struct node *todelete,*secondlastnode;


if(head==NULL)

{ printf("List is already empty");

else

{ todelete=head;

secondlastnode=head;

while(todelete->next!=NULL)

{ secondlastnode=todelete;

todelete=todelete->next;

if(todelete==NULL)

{ head=NULL;

else

{ secondlastnode->next=NULL;

} free(todelete);

printf("Last Node Deleted");

void display()

{ struct node *temp;

if(head==NULL)

{ printf("List is empty");

else

{ temp=head;

while(temp!=NULL)

{ printf("Data=%d\n",temp->data);

temp=temp->next;

void main()

{ int n,ch;
clrscr();

printf("enter no. of nodes:");

scanf("%d",&n);

create(n);

printf("\nData in list\n");

display();

printf("\nPress 1 to delete last node:");

scanf("%d",&ch);

if(ch==1)

{ deletelastnode();

printf("\nData in list\n");

display();

getch();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy