Data structure in C programming - c

i was asked to create employee record using linked list to insert five different employee .then,display all employee record and lastly search by employee number.i made a mistake to write write search function by employee name.another thing is even in search by employee by name when it found it it some time at below it say employee not found.this two problem anyone can help.any to change search function by employee name to by employee number.Please help.
this is what i have so far.tell me what i need to change
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node* createnode(struct node*);
void display(struct node*);
void search(struct node*);
struct node
{
int emp_num;
char name[10];
int Year_service;
float salary;
struct node* ptr;
};
int main()
{
struct node* head;
int b,i;
head=NULL;
while(1)
{
printf("\n1-> to enter the employee details\n2->to display All employee details\n3->to search an element\nEnter the value\n");
scanf("%d",&b);
switch(b)
{
case (1): printf("\nEnter the Number of Employee's Detail you would like to enter\n");
scanf("%d",&i);
while(i>0)
{
head=createnode(head);
i--;
}
break;
case (2): display(head);
break;
case (3): search(head);
break;
default :
printf("\nEnter Valid Choice \n");
}
}
}
void display(struct node* head)
{
if(head==NULL)
{
printf("\nThe list is empty \n");
}
else
{
while(head!=NULL)
{
printf("*****************************************************");
printf("\nThe employee Number :%d\n",head->emp_num);
printf("\nThe Employee Name : %s\n",head->name);
printf("\nThe Salary : %f\n",head->salary);
printf("\n Years serviced : %d\n",head->Year_service);
head=head->ptr;
}
}
}
struct node* createnode(struct node* head)
{
struct node* newnode;
newnode=(struct node*)malloc(sizeof (struct node));
printf("\nEnter the employee number \n");
scanf("%d",&newnode->emp_num);
printf("\nEnter the employee name \n");
scanf("%s",newnode->name);
printf("\nEnter the salary of the employee \n");
scanf("%f",&newnode->salary);
printf("\nEnter the Employee Years of service \n");
scanf("%d",&newnode->Year_service);
if(newnode == NULL)
{
newnode->ptr=NULL;
}
else
{
newnode->ptr=head;
}
return newnode;
}
void search(struct node* head)
{
char ch[10];
printf("\nEnter Employee Name\n");
scanf("%s",ch);
while(head!=NULL)
{
if(strcmp (ch,head->name)==0)
{
printf("\nThe element is matched\n");
printf("\nThe employee Number : %d\n",head->emp_num);
printf("\nThe employee name : %s\n",head->name);
printf("\nSalary : %f\n",head->salary);
printf("\nYears serviced : %d\n",head->Year_service);
}
else {
printf("\nThe element is not matched\n");
}
head=head->ptr;
}
}

Use a variable that indicates the item was found. It will then be used to display if the element has not been found.
void search(struct node* head)
{
char ch[10];
printf("\nEnter Employee Name\n");
scanf("%s", ch);
int element_found = 0;
while(head!=NULL)
{
if(strcmp(ch,head->name)==0)
{
printf("\nThe element is matched\n");
printf("\nThe employee Number : %d\n",head->emp_num);
printf("\nThe employee name : %s\n",head->name);
printf("\nSalary : %f\n",head->salary);
printf("\nYears serviced : %d\n",head->Year_service);
element_found=1;
}
head=head->ptr;
}
if(!element_found)
{
printf("\nThe element is not matched\n");
}
}

Related

Infinitely printing upon inserting before an existing element in linked list using C

If I insert an element before an existing element then the last two elements keeps printing infinitely. For example, if 10 20 30 are the existing elements and now if I want to insert 40 before 30(so that new list becomes 10 20 40 30) then 40 & 30 prints for an infinite number of times.
Thanks in advance.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}list;
list *start=NULL;
list *create(list *);
list *display(list *);
list *insert_before(list *);
int main()
{
int n;
printf("1: Create list\n");
printf("2: Display\n");
printf("3: Insert before an existing element\n);
for(;;)
{
printf("Enter your choice: ");
scanf("%d",&n);
switch(n)
{
case 1:start=create(start);
printf("\nList created successfully\n");
break;
case 2:start=display(start);
break;
case 3:start=insert_before(start);
break;
default:printf("Wrong input!!!");
exit(0);
}
}
}
list *create(list *start)
{
list *new_node, *ptr;
int num;
printf("Enter data: ");
scanf("%d",&num);
new_node=(list *)malloc(sizeof(list));
new_node->data=num;
if(start==NULL)
{
start=new_node;
new_node->next=NULL;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=new_node;
new_node->next=NULL;
}
return start;
}
list *display(list *start)
{
list *ptr;
if(start==NULL)
{
printf("Empty list");
return start;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
return start;
}
list *insert_before(list *start)
{
int num,search,flag=0;
list *new_node, *ptr, *prev;
ptr=start;
printf("Enter the number before whom new data is to be inserted: ");
scanf("%d",&search);
while(ptr!=NULL)
{
prev=ptr;
if(ptr->data==search)
{
flag=1;
break;
}
ptr=ptr->next;
}
if(flag==1)
{
printf("\nEnter the new data to be entered");
scanf("%d",&num);
new_node=(list *)malloc(sizeof(list));
new_node->data=num;
prev->next=new_node;
new_node->next=ptr;
}
else
{
printf("Entered data cannot be found");
}
return start;
}
In the function insert_before you should be checked if the first item is the searched and then analyse the other case. Well i did some change and this is the modificated code for in the funtion:
list *insert_before(list *start){
int num,search,flag=0;
list *new_node, *ptr, *prev;
ptr=start;
printf("Enter the number before whom new data is to be inserted: ");
scanf("%d",&search);
if(start != NULL && start->data == search){
printf("Enter the new data to be entered: ");
scanf("%d",&num);
new_node=(list *)malloc(sizeof(list));
new_node->data=num;
new_node->next = start;
if(start->next == start)
start->next=new_node;
else{
ptr=start;
while(ptr->next!=start) ptr=ptr->next;
ptr->next=new_node;
}
start=new_node;
}
else{
while(!flag && ptr->next!=start){
prev=ptr;
if(ptr->next->data==search) flag=1;
ptr=ptr->next;
}
if(flag==1){
printf("Enter the new data to be entered: ");
scanf("%d",&num);
new_node=(list *)malloc(sizeof(list));
new_node->data=num;
prev->next=new_node;
new_node->next=ptr;
}
else
printf("Entered data cannot be found\n");
}
return start;
}

making a phonebook system using linked list

i'm creating a phonebook system in c using single linked list it seems that everything is working well but the delete option always giving me an error and i don't know how to fix it so i hope someone can tell what is problem in the code and show me a code that can modify the name or the number in this code
#include<stdio.h>
#include<stdlib.h>
struct node
{
char firstname[20];
char lastname[20];
long int number;
struct node *next;
};
struct node *head=NULL;
struct node *getnode()
{
return((struct node *)malloc(sizeof(struct node)));
}
void display(struct node *head)
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%d\n",temp->number);
temp=temp->next;
}
}
void insert()
{
struct node *temp,*newnode;
newnode=getnode();
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
printf("Enter First name:\n");
scanf("%s",&newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",&newnode->lastname);
printf("Enter number:\n");
scanf("%d",&newnode->number);
temp->next=newnode;
newnode->next=NULL;
display(head);
}
struct node *create()
{
struct node *temp,*newnode;
if(head!=NULL)
insert();
else
{
newnode=getnode();
head=newnode;
temp=head;
printf("Enter First name:\n");
scanf("%s",&newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",&newnode->lastname);
printf("Enter number:\n");
scanf("%d",&newnode->number);
newnode->next=NULL;
display(head);
}
}
void search()
{
struct node *temp;
char *first,*last;
temp=head;
printf("Enter name to be searched:\n");
scanf("%s",&first);
scanf("%s",&last);
while((temp->firstname==first)&&(temp->lastname==last))
{
temp=temp->next;
}
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%d\n",temp->number);
}
void del()
{
struct node *pretemp,*temp;
char *f,*l;
temp=head;
pretemp=head->next;
printf("Enter name :");
scanf("%s",&f);
scanf("%s",&l);
while(temp!=NULL){
if((pretemp->firstname==f)&&(pretemp->lastname==l))
{
printf("%s ",temp->firstname);
printf("%s ",temp->lastname);
printf("%s ",temp->number);
temp=pretemp->next;
delete pretemp;
break;
}
else
{
temp=temp->next;
pretemp=pretemp->next;
}
}
int main()
{
int op,ch;
do{
printf("-------Welcome--------\n");
printf("1.Create\n2.Display\n3.Delete\n4.Search\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display(head);
break;
case 3: del();
break;
case 4:search();
break;
}
printf("Do you want to quit ? 1 for no / 0 for yes:");
scanf("%d",&op);
}while(op);
return 0;
}
this is the error
I had made the following changes in your search and delete function
The first and last buffer in both search and delete function were not allocated memory before using them in scanf. This will cause run-time error
The way you were catching user input for first and last name in scanf was also improper
In search and delete function I modified the string comparison. To compare to string you need use strncmp function. Using == will check the address of first byte.
In search function you were not checking end of list.
In del function I have changed printf("%s ", temp->number) to printf("%d ", temp->number)
void search()
{
struct node *temp;
char first[20], last[20];
temp = head;
printf("Enter name to be searched:\n");
scanf("%s", first);
scanf("%s", last);
while (temp != NULL && ((strncmp(temp->firstname, first, 20) != 0) && (strncmp(temp->lastname, last, 20) != 0)))
{
temp = temp->next;
}
if (temp != NULL) {
printf("%s\n", temp->firstname);
printf("%s\n", temp->lastname);
printf("%d\n", temp->number);
}
}
void del()
{
struct node *pretemp, *temp;
char first[20], last[20];
temp = head;
pretemp = head->next;
printf("Enter name :");
scanf("%s", first);
printf("Enter Last name:");
scanf("%s", last);
while (temp != NULL) {
if((strncmp(temp->firstname, first, 20) != 0) && (strncmp(temp->lastname, last, 20) != 0))
{
printf("%s ", temp->firstname);
printf("%s ", temp->lastname);
printf("%d ", temp->number);
temp = pretemp->next;
delete pretemp;
break;
}
else
{
temp = temp->next;
pretemp = pretemp->next;
}
}
}
I made some changes in your code, you can find comment in code bellow.
It is compiled and likend under linux ubuntu 18.04 and it works now.
Basicaly, when you use scanf(" %s ", astr), 'astr' should have enough space to accept input.
#include<stdio.h>
#include<stdlib.h>
struct node
{
char firstname[20];
char lastname[20];
long int number;
struct node *next;
};
struct node *head=NULL;
struct node *getnode()
{
return((struct node *)malloc(sizeof(struct node)));
}
void display(struct node *head)
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%ld\n",temp->number); /* number is long int */
temp=temp->next;
}
}
void insert()
{
struct node *temp,*newnode;
newnode=getnode();
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
printf("Enter First name:\n");
scanf("%s",newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",newnode->lastname);
printf("Enter number:\n");
scanf("%ld",&newnode->number);
temp->next=newnode;
newnode->next=NULL;
display(head);
}
struct node *create()
{
struct node *temp,*newnode;
if(head!=NULL)
insert();
else
{
newnode=getnode();
head=newnode;
temp=head;
printf("Enter First name:\n");
scanf("%s",newnode->firstname);
printf("Enter Last name:\n");
scanf("%s",newnode->lastname);
printf("Enter number:\n");
scanf("%ld",&newnode->number);
newnode->next=NULL;
display(head);
}
}
void search()
{
struct node *temp;
char first[20], last[20]; /* space for input */
temp=head;
printf("Enter name to be searched:\n");
scanf("%s",first); /* you dont need '&' operator for string*/
scanf("%s",last);
while((temp->firstname==first)&&(temp->lastname==last))
{
temp=temp->next;
}
printf("%s\n",temp->firstname);
printf("%s\n",temp->lastname);
printf("%ld\n",temp->number); /* number is long int */
}
void del()
{
struct node *pretemp,*temp;
char f[20],l[20]; /* you need a space to store input */
temp=head;
pretemp=head->next;
printf("Enter name :");
scanf("%s",f); /* you dont need '&' operator to access a string */
scanf("%s",l);
while(temp!=NULL){
if((pretemp->firstname==f)&&(pretemp->lastname==l))
{
printf("%s ",temp->firstname);
printf("%s ",temp->lastname);
printf("%ld ",temp->number); /* 'number' is long int */
temp=pretemp->next;
free(pretemp); /* 'delete' is c++ operator, not C */
break;
}
else
{
temp=temp->next;
pretemp=pretemp->next;
}
} /* missing curly bracket */
}
int main()
{
int op,ch;
do{
printf("-------Welcome--------\n");
printf("1.Create\n2.Display\n3.Delete\n4.Search\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: display(head);
break;
case 3: del();
break;
case 4:search();
break;
}
printf("Do you want to quit ? 1 for no / 0 for yes:");
scanf("%d",&op);
}while(op);
return 0;
}

add function is not any new node or display function is showing none but the first node info only

enter code hereIn the main function I am calling the add function as well as the display function to see all the present nodes and their stored data into the linked list. But every time it's only showing me the first node value not the other nodes. I can't find any bug into my code. Can any body help...
This is my code:-
struct student* add(struct student*);
struct student* search(struct student*);
struct student* modify(struct student*);
void display(struct student* head);
struct student
{
int roll;
char name[50];
float percentage;
struct student *address;
};
int nodes=0;//nodes variable keeps the data of the total no of nodes present in to the inked list.
int main()
{
struct student *head;
int choice;
char mychoice='y';
head=NULL;
do
{
printf("Enter 1 to add a node.\nEnter 2 to display all existing record.\nEnter 3 to search a record.\nEnter 4 to modify an existing record.\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
head=add(head);
display(head);
//break;
case 2:
display(head);
break;
case 3:
head=search(head);
break;
case 4:
head=modify(head);
break;
default:
printf("Enter any of the correct input.\n");
break;
}
printf("\nDo you want to continue? [Y/N]");
scanf(" %c",&mychoice);
}while(mychoice=='y'||mychoice=='Y');
return 0;
}
struct student* add(struct student* head)
{
struct student *node,*temp;
node=(struct student*)malloc(sizeof(struct student));
temp=head;
printf("Enter the name of the student: ");
scanf("%s",&(node->name));
printf("Enter the roll of the student: ");
scanf("%d",&(node->roll));
printf("Enter the percentage: ");
scanf("%f",&(node->percentage));
node->address=NULL;
if(head==NULL) // Implies an empty list.
head=node;
else
{
temp=head;
while(temp!=NULL)//Traversing to the last node.
temp=temp->address;
temp=node;
}
nodes++;
return head;
}
struct student* search(struct student* head)
{
int m;
printf("Enter the no of node you wanna search: ");
scanf("%d",&m);
if(m>nodes)
printf("%dth node does not exist.",m);
else
{
for(i=0;i<m;i++)// Traversing node to node to go to the mth node.
temp=temp->address;
printf("\nThe name of the student is: %s\nThe roll no of the student is: %d \nThe percentage of the student is: %5.2f \n\n",temp->name,temp->roll,temp->percentage);
}
return head;
}
struct student* modify(struct student* head)
{
int m,i;
struct student *temp;
temp=head;
printf("Enter the index no of node you wanna change: ");
scanf("%d",&m);
if(m>nodes)
printf("%dth node does not exist.",m);
else
{
for(i=0;i<m;i++)// Traversing node to node to go to the mth node.
temp=temp->address;
printf("Enter the new name of the student: ");
scanf("%s",&(temp->name));
printf("Enter the new roll of the student: ");
scanf("%d",&(temp->roll));
printf("Enter the new percentage: ");
scanf("%f",&(temp->percentage));
}
return head;
}
void display(struct student* head)
{
struct student *temp;
temp=head;
while(temp!=NULL)
{
printf("\nThe name of the student is: %s\nThe roll no of the student is: %d \nThe percentage of the student is: %5.2f \n\n",temp->name,temp->roll,temp->percentage);
temp=temp->address;
}
}
For the add function your problem is here:
temp=head;
while(temp!=NULL)//Traversing to the last node.
temp=temp->address;
temp=node;
You never make the currently last node point to the new node. What you do is to assign the new node to temp and then return. When you return temp goes out of scope and the new node is lost.
To insert a new node, you'll need to update address of the last node. In pseudo code you need to do:
// Pseudo code to insert a new tail node
current_last_node->address = new_node;
Try something like this:
temp=head;
while(temp->address!=NULL)//Traversing to the last node.
temp=temp->address;
temp->address=node;
BTW: Calling the pointer to the next element of the list for address is not normal. The convention is to use the name next.
Two other comments:
1) Since you want to add to the end of the list it's often a good idea to have a tail pointer for better performance.
2) You should collect head, nodes and tail into a struct.
Something like:
struct student_list
{
struct student *head;
struct student *tail;
int nodes;
};
int main()
{
struct student_list list = {NULL, NULL, 0};
add(&list);
...
...
}
Then the add function would be:
void add(struct student_list* list)
{
struct student *node;
node=malloc(sizeof *node);
// Read data into node
node->address=NULL;
if(list->head==NULL) // Implies an empty list.
{
list->head=node;
}
else
{
list->tail->address=node;
}
list->tail=node;
list->nodes++;
}
Now there is no loop in the function so adding elements are O(1), i.e. better performance.

Node elements are not printing in Single Linked List Program

I have created here a C Program in TurboC++. This simple program goal is to just create a linked list, insert every element at the end of the list and then print the value of the nodes.(Edited The program is somewhat changed to make it easier for me to understand but still the problem of nodes not being priniting exists)
The problem is, some of the node elements are not printing
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<ctype.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
typedef struct node ND;
void main()
{
ND *start,*current,*temp;
start=NULL;
do
{
temp=(ND*)malloc(sizeof(ND));
printf("\n Enter Node Value");
scanf(" %d",&temp->data);
temp->link=NULL;
if(start==NULL)
{
start=current=temp;
}
else
{
current->link=temp;
current=temp;
}
fflush(stdin);
printf("\nDo You Want TO Continue?(Y/N)");
}while(toupper(getchar())!='N');
current=start;
printf("\nThe Elements OF Linked List ARE:");
while(current!=NULL)
{
printf(" %d",current->data);
current=current->link;
}
}
You printing list from wrong element. You should start from head.
like:
temp1 = head;
while(temp1!=NULL)
{
printf(" %d",temp1->data);
temp1=temp1->link;
}
By the way your head element will be always NULL.
Here is correct way to add elements to list:
if (head == NULL)
{
head = temp;
}
else
{
temp1 = head;
while (temp1->link != NULL)
{
temp1 = temp1->link;
}
temp1->link = temp;
}
The program is now working correctly i have made some changes to make this program easier to understand.Here's the code:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *link;
};
typedef struct node ND;
void main()
{
ND *head,*tail,*temp1;
int n;
char ch;
printf("\nEnter Data?(y/n)\n");
scanf(" %c",&ch);
fflush(stdin);
if(ch=='y' || ch=='Y')
{
tail=(ND*)malloc(sizeof(ND));
printf("\n Enter Node Value");
scanf(" %d",&n);
tail->data=n;
tail->link=NULL;
head=tail;
printf("\nDo You Want TO Continue?(Y/N)");
scanf(" %c",&ch);
fflush(stdin);
}
while(ch=='y' || ch=='Y')
{
printf("\n Enter Node Value");
scanf(" %d",&n);
tail->link=(ND*)malloc(sizeof(ND));
tail->link->data=n;
tail->link->link=NULL;
tail=tail->link;
printf("\nEnter More Data?(y/n)\n");
scanf(" %c",&ch);
fflush(stdin);
}
printf("\nElements Of Linked List Are:");
temp1=head;
while(temp1!=NULL)
{
printf(" %d",temp1->data);
temp1=temp1->link;
}
printf("\n");
fflush(stdout);
getch();
}

Doubly Linked List Delete Function Error

I have written the following program but the problem is in Delete function.Whenever i am trying to delete the value at first position.The whole list is lost don't know why.If i try to display list after that then some garbage values are printed.The function is working perfectly for other positions.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
struct Student
{
int rno;
char name[20];
struct Student *next;
struct Student *prev;
};
void Display(struct Student *head)
{
assert(head!=NULL);
while(head!=NULL)
{
printf("%d\t%s\t",head->rno,head->name);
head=head->next;
}
}
struct Student *Insert(struct Student *head,const int position,const int rno,const char name[])
{
//printf("%s\n",__FUNCTION__);
struct Student *temp=(struct Student *)malloc(sizeof(struct Student));
struct Student *traverse=head;
int pos=position;
if(temp==NULL)
exit(-1);
temp->rno=rno;
strcpy(temp->name,name);
temp->next=NULL;
temp->prev=NULL;
// printf("%s\n",__FUNCTION__);
if(pos==1)
{
if(head==NULL)
{
head=temp;
}
else
{
temp->next=head;
head->prev=temp;
head=temp;
}
}
else
{
for(traverse=head,pos=position;traverse->next!=NULL&&pos-2!=0;traverse=traverse->next,pos--);
if(traverse==NULL || pos-2!=0)
{
printf("Invalid Position");
}
else
{
temp->next=traverse->next;
if(temp->next!=NULL)
temp->next->prev=temp;
temp->prev=traverse;
traverse->next=temp;
}
}
return head;
}
void DeleteAll(struct Student *head)
{
struct Student *temp=head;
while(temp->next!=NULL)
{
head=head->next;
free(temp);
temp=head;
}
free(temp);
}
void Delete(struct Student *head,int pos)
{
assert(head!=NULL);
struct Student *temp=head;
struct Student *traverse=head;
int position=pos;
if(position==1)
{
if(head->next!=NULL)
head=head->next;
head->prev=NULL;
temp->next=NULL;
free(temp);
}
else
{
while(traverse->next!=NULL&&position-1!=0)
{
traverse=traverse->next;
position--;
}
if(traverse==NULL || position-1!=0)
{
printf(".............Invalid position..........\n");
}
else
{
traverse->prev->next=traverse->next;
if(traverse->next)
traverse->next->prev=traverse->prev;
}
}
}
struct Student *CreateStudentList(const int no_of_students)
{
struct Student *head=NULL;
int i;
int rno;
char name[20];
for(i=0;i<no_of_students;i++)
{
printf("Enter roll number and name:");
scanf("%d%s",&rno,name);
head=Insert(head,i+1,rno,name);
}
return head;
}
void SimulateDoublyLinkedList()
{
struct Student *cdscpp2013=NULL;
int no_of_students;
int choice,rno,position;
char name[20];
while(choice!=5)
{
if(NULL==cdscpp2013)
{
printf("Enter number of students:");
scanf("%d",&no_of_students);
cdscpp2013=CreateStudentList(no_of_students);
}
else
{
printf("\nMenu Operations\nPress 1 for Insert\nPress 2 for Delete\nPress 3 for DeleteAll\nPress 4 for Display\nPress 5 for Exit\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter roll number and name to ininsert:");
scanf("%d%s",&rno,name);
printf("Enter position to insert:");
scanf("%d",&position);
cdscpp2013=Insert(cdscpp2013,position,rno,name);
break;
case 2:
printf("Enter position to delete:");
scanf("%d",&position);
Delete(cdscpp2013,position);
break;
case 3:
DeleteAll(cdscpp2013);
break;
case 4:
Display(cdscpp2013);
break;
case 5:
exit(1);
default:
printf("Invalid choice....Please Enter proper option.....");
}
}
}
}
int main()
{
SimulateDoublyLinkedList();
}
When you remove the first element of a list, the caller must update its reference to the list. So your Delete function must return the pointer to the first element of the resulting list.
In this kind of operation I also find useful to use pointers to pointers... this would simplify a lot your code.
try this for delete this should work -
as temp = head , the temp->next =null will make the whole list disappear
//just removed temp->next = null;
if(position==1) {
if(head->next!=NULL)
head=head->next;
head->prev=NULL;
free(temp);
}
Edited --
make delete function to return the head pointer and in the switch case use this
cdscpp2013=Delete(cdscpp2013,position);
instead of this
Delete(cdscpp2013,position);
then change the same for deleteall function - it should work :)

Resources