Sum C Code
Sum C Code
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
#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