#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
int label;
char oper;
int address;
struct node *next;
};
//shorting the name
typedef struct node node;
node *head;
//Display function
void display(){
if(head==NULL)
{
printf("There is no node \n");
}
//Create temporary node t
node *t=head;
while(t){
printf("%d\t",t->label);
printf("%c\t",t->oper);
printf("%d\t",t->address);
t=t->next;
}
}
//Insert function
void insert(){
int a,c;
char b;
//create new node
node *nnode=(node*) malloc(sizeof(node));
printf("Enter Label,Operator,Address\n\n");
scanf("%d",&a);//Label
getchar();
scanf("%c",&b);//oper
getchar();
scanf("%d",&c);//address
getchar();
printf("\n");
nnode->label=a;
nnode->oper=b;
nnode->address=c;
if(head==NULL){
nnode->next=NULL;
head=nnode;
}
else{
//insert at the end
node *t=head;
while(t->next){
//traverse until t->next=NULL
t=t->next;
}
t->next=nnode;//New node will be inserted at the end
nnode->next=NULL;
}
}
void remove(int l){
node *t=head;//store head so it do not change
node *p;
p=t;//Take head address to temp pointer to remove it
if(head==NULL){
printf("There is nothing to delete\n\n");
return;
}
if (t!=NULL && t->label==l){
//This is single or first node to delete t->NULL
free(t);
head=NULL;
return;
}
while(t->next!=NULL && t->label!=l){
p=t;
t=t->next;
}
if(t==NULL){
printf("Item is not available\n\n");
return;
}
/* if(t->label==l)
{
head=t->next;
free(t);
return;
}*/
p->next=t->next;
free(t);
return;
}
int main(){
int l,ch;
do{
printf("*Enter Your choice*\n1.Insert Node\t2.Display List\t3.Delete Node\t4.Exit\n");
printf("Choice:\t");scanf("%d",&ch);printf("\n");
switch(ch){
case 1:
insert();
break;
case 2:
printf("Display:-");
display();
printf("\n\n");
break;
case 3:
printf("Enter label to remove:-\t");
scanf("%d",&l);
remove(l);
break;
case 4:
exit(0);
default:
printf("Wrong choice try again\n");
}
}while(ch<=3);
}
#include<stdlib.h>
#include<malloc.h>
struct node{
int label;
char oper;
int address;
struct node *next;
};
//shorting the name
typedef struct node node;
node *head;
//Display function
void display(){
if(head==NULL)
{
printf("There is no node \n");
}
//Create temporary node t
node *t=head;
while(t){
printf("%d\t",t->label);
printf("%c\t",t->oper);
printf("%d\t",t->address);
t=t->next;
}
}
//Insert function
void insert(){
int a,c;
char b;
//create new node
node *nnode=(node*) malloc(sizeof(node));
printf("Enter Label,Operator,Address\n\n");
scanf("%d",&a);//Label
getchar();
scanf("%c",&b);//oper
getchar();
scanf("%d",&c);//address
getchar();
printf("\n");
nnode->label=a;
nnode->oper=b;
nnode->address=c;
if(head==NULL){
nnode->next=NULL;
head=nnode;
}
else{
//insert at the end
node *t=head;
while(t->next){
//traverse until t->next=NULL
t=t->next;
}
t->next=nnode;//New node will be inserted at the end
nnode->next=NULL;
}
}
void remove(int l){
node *t=head;//store head so it do not change
node *p;
p=t;//Take head address to temp pointer to remove it
if(head==NULL){
printf("There is nothing to delete\n\n");
return;
}
if (t!=NULL && t->label==l){
//This is single or first node to delete t->NULL
free(t);
head=NULL;
return;
}
while(t->next!=NULL && t->label!=l){
p=t;
t=t->next;
}
if(t==NULL){
printf("Item is not available\n\n");
return;
}
/* if(t->label==l)
{
head=t->next;
free(t);
return;
}*/
p->next=t->next;
free(t);
return;
}
int main(){
int l,ch;
do{
printf("*Enter Your choice*\n1.Insert Node\t2.Display List\t3.Delete Node\t4.Exit\n");
printf("Choice:\t");scanf("%d",&ch);printf("\n");
switch(ch){
case 1:
insert();
break;
case 2:
printf("Display:-");
display();
printf("\n\n");
break;
case 3:
printf("Enter label to remove:-\t");
scanf("%d",&l);
remove(l);
break;
case 4:
exit(0);
default:
printf("Wrong choice try again\n");
}
}while(ch<=3);
}
No comments:
Post a Comment