0% found this document useful (0 votes)
12 views9 pages

Sum C Code

The document contains C code for managing a singly linked list, including functions to create, display, insert at the beginning and end, and delete the first node. Each section of the code is clearly labeled with questions indicating the specific operations being performed. The document also includes prompts for user input and handles memory allocation for nodes.

Uploaded by

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

Sum C Code

The document contains C code for managing a singly linked list, including functions to create, display, insert at the beginning and end, and delete the first node. Each section of the code is clearly labeled with questions indicating the specific operations being performed. The document also includes prompts for user input and handles memory allocation for nodes.

Uploaded by

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

NAME -HIMANI GUPTA

ADMISSION NO -U22EE078

ASSIGNMENT 4
QUES –create and display

#include<stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*stnode;
void create(int n){
struct node *fnnode,*temp;
int num;
stnode=(struct node*)malloc(sizeof(struct node));
if(stnode==NULL){
printf("Memory is not allocated");
}
else{
printf(" Input data for node 1 : ");
scanf("%d",&num);
stnode->data=num;
stnode->next=NULL;
temp=stnode;
for(int i=2;i<=n;i++){
fnnode=(struct node*)malloc(sizeof(struct node));
if(fnnode==NULL){
printf("Memory is not allocated");
}
else{
printf(" Input data for node %d : ", i);
scanf("%d",&num);
fnnode->data=num;
fnnode->next=NULL;
temp->next=fnnode;
temp=temp->next;
}
}
}
}
void display(){
struct node *temp;
if(stnode==NULL){
printf("List is empty");
}
else{
temp=stnode;
while(temp!=NULL){
printf("%d",temp->data);
temp=temp->next;
}
}
}
int main(){
int n,num,num1;
printf(" Input the number of nodes : ");
scanf("%d",&n);
create(n);
display();
}

OUTPUT
QUES 2 insert at beginning

#include<stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*stnode;
void create(int n){
struct node *fnnode,*temp;
int num;
stnode=(struct node*)malloc(sizeof(struct node));
if(stnode==NULL){
printf("Memory is not allocated");
}
else{
printf(" Input data for node 1 : ");
scanf("%d",&num);
stnode->data=num;
stnode->next=NULL;
temp=stnode;
for(int i=2;i<=n;i++){
fnnode=(struct node*)malloc(sizeof(struct node));
if(fnnode==NULL){
printf("Memory is not allocated");
}
else{
printf(" Input data for node %d : ", i);
scanf("%d",&num);
fnnode->data=num;
fnnode->next=NULL;
temp->next=fnnode;
temp=temp->next;
}
}
}
}
void display(){
struct node *temp;
if(stnode==NULL){
printf("List is empty");
}
else{
temp=stnode;
while(temp!=NULL){
printf("%d",temp->data);
temp=temp->next;
}
}
}
void insertatbegin(int num){
struct node *fnnode;
fnnode=(struct node*)malloc(sizeof(struct node));
if(fnnode==NULL){
printf("Memory is not allocated");
}
else{
fnnode->data=num;
fnnode->next=stnode;
stnode=fnnode;
}
}
int main(){
int n,num,num1;
printf(" Input the number of nodes : ");
scanf("%d",&n);
create(n);
display();
printf("\n Input data to insert at the beginning of the list : ");
scanf("%d", &num);
insertatbegin(num);
printf("\n Data after inserted in the list are : \n");
display();
}

OUTPUT
QUES 3 insert at end

#include<stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*stnode;
void create(int n){
struct node *fnnode,*temp;
int num;
stnode=(struct node*)malloc(sizeof(struct node));
if(stnode==NULL){
printf("Memory is not allocated");
}
else{
printf(" Input data for node 1 : ");
scanf("%d",&num);
stnode->data=num;
stnode->next=NULL;
temp=stnode;
for(int i=2;i<=n;i++){
fnnode=(struct node*)malloc(sizeof(struct node));
if(fnnode==NULL){
printf("Memory is not allocated");
}
else{
printf(" Input data for node %d : ", i);
scanf("%d",&num);
fnnode->data=num;
fnnode->next=NULL;
temp->next=fnnode;
temp=temp->next;
}
}
}
}
void display(){
struct node *temp;
if(stnode==NULL){
printf("List is empty");
}
else{
temp=stnode;
while(temp!=NULL){
printf("%d",temp->data);
temp=temp->next;
}
}
}
void insertatend(int num){
struct node*fnnode,*temp;
fnnode=(struct node*)malloc(sizeof(struct node));
if(fnnode==NULL){
printf("Memory is not allocated");
}
else{
fnnode->data=num;
fnnode->next=NULL;
temp=stnode;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=fnnode;
}
}
int main(){
int n,num,num1;
printf(" Input the number of nodes : ");
scanf("%d",&n);
create(n);
display();
printf("\n Input data to insert at the ending of the list : ");
scanf("%d", &num1);
insertatend(num1);
printf("\n Data after inserted in the list are : \n");
display();
}

OUTPUT

QUES 4 FIRST NODE DELETION

#include<stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*stnode;
void create(int n){
struct node *fnnode,*temp;
int num;
stnode=(struct node*)malloc(sizeof(struct node));
if(stnode==NULL){
printf("Memory is not allocated");
}
else{
printf(" Input data for node 1 : ");
scanf("%d",&num);
stnode->data=num;
stnode->next=NULL;
temp=stnode;
for(int i=2;i<=n;i++){
fnnode=(struct node*)malloc(sizeof(struct node));
if(fnnode==NULL){
printf("Memory is not allocated");
}
else{
printf(" Input data for node %d : ", i);
scanf("%d",&num);
fnnode->data=num;
fnnode->next=NULL;
temp->next=fnnode;
temp=temp->next;
}
}
}
}
void display(){
struct node *temp;
if(stnode==NULL){
printf("List is empty");
}
else{
temp=stnode;
while(temp!=NULL){
printf("%d",temp->data);
temp=temp->next;
}
}
}
void FirstNodeDeletion()
{
struct node *temp;
if(stnode == NULL)
{
printf(" There are no node in the list.");
}
else
{
temp = stnode;
stnode = stnode->next;
printf("\n Data of node 1 which is being deleted is : %d\n", temp-
>data);
free(temp);
}
}
int main(){
int n,num,num1;
printf(" Input the number of nodes : ");
scanf("%d",&n);
create(n);
display();
FirstNodeDeletion();
display();
}

OUTPUT

You might also like