Linklist S 57
Linklist S 57
h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*header;
void create_list(int n);
void display();
//void insert_beg(int data);
void insert_end(int data);
int main(){
int n;
int data;
printf("Enter number of nodes:");
scanf("%d",&n);
create_list(n);
display();
// insert_beg(data);
//display();
insert_end(data);
display();
return 0;
}
void create_list(int n){
struct node *newnode,*temp;
int data,i;
newnode=(struct node *)malloc(sizeof(struct node));
if(newnode==NULL){
printf("memory not allocated ");
}
else{
printf("Enter your data :");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
header=newnode;
temp=newnode;
for(i=2;i<=n;i++){
newnode=(struct node *)malloc(sizeof(struct node));
if(newnode==NULL){
printf("memory not allocated ");
break;
}
else{
printf("Enter your data :");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}
}
}
}
/*void insert_beg(int data){
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data at begninning\n ");
scanf("%d",&data);
newnode->data=data;
newnode->next=header;
header=newnode;
*/
void insert_end(int data){
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data at end\n ");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
temp=header;
while(temp!=NULL)
temp->next=newnode;
temp=temp->next;
printf("data inserted successfully:\n");
}
void display(){
struct node *temp;
int count=0;
if(header==NULL){
printf("ERROR");
}
else{
temp=header;
while(temp!=NULL){
printf("Data is %d\n",temp->data);
temp=temp->next;
count++;
}
}
printf("total nodes are %d/n",count);
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*header;
void create_list(int n);
void insert_at_anypost(int data,int pos);
void display();
int main(){
int n,i;
int data;
int pos;
printf("Enter number of nodes:");
scanf("%d",&n);
create_list(n);
display();
insert_at_anypost(data, pos);
display();
return 0;
}
void create_list(int n){
struct node *newnode,*temp;
int data,i;
newnode=(struct node *)malloc(sizeof(struct node));
if(newnode==NULL){
printf("memory not allocated ");
}
else{
printf("Enter your data :");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
header=newnode;
temp=newnode;
for(i=2;i<=n;i++){
newnode=(struct node *)malloc(sizeof(struct node));
if(newnode==NULL){
printf("memory not allocated ");
break;
}
else{
printf("Enter your data :");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}
}
}
}
void insert_at_anypost(int data,int pos){
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(newnode));
int i;
if(newnode==NULL){
printf("Unable to locate memory:");
}
else{
printf("Enter your data :");
scanf("%d",&data);
printf("Enter the position:");
scanf("%d",&pos);
newnode->data=data;
newnode->next=NULL;
temp=header;
for(i=2;i<=(pos-1);i++){
temp=temp->next;
if(temp==NULL)
break;
}
if(temp!=NULL){
newnode->next=temp->next;
temp->next=newnode;
printf("inserted at pos");
}
else{
printf("@@##@#");
}
}
}
void display(){
struct node *temp;
int count=0;
if(header==NULL){
printf("ERROR");
}
else{
temp=header;
while(temp!=NULL){
printf("Data is %d\n",temp->data);
temp=temp->next;
count++;
}
}
printf("total nodes are %d\n",count);
}