linked list insertion operation and display - c

I am trying to implement a singly linked list and performing insertion operations.The program compiles and runs but whenever i try to display its elements.It doesn't show the elements.I am not able to find out the error.
#include <stdio.h>
#include <stdlib.h>
struct n
{
int data;
struct n *next;
};
typedef struct n node;
node *insert_at_front(node *start,int info)
{
node *temp,*p;
temp=(node *)malloc(sizeof(node));
temp->data=info;
temp->next=start;
start=temp;
return start;
}
node *insert_at_end(node *start,int info)
{
node *temp,*p;
temp=(node *)malloc(sizeof(node));
if(start==NULL)
{
printf("Empty\n");
return start;
}
else
{
for(p=start; p->next!=NULL; p=p->next)
{
if(p->next==NULL)
{
temp->data=info;
temp->next=p->next;
p->next=temp;
}
}
}
return start;
}
node *insert_after(node *start,int info,int dat)
{
node *temp,*p;
temp=(node *)malloc(sizeof(node));
for(p=start; p->next!=NULL; p=p->next)
{
if(p->data==dat)
{
temp->data=info;
temp->next=p->next;
p->next=temp;
}
}
return start;
}
void display(node *start)
{
node *temp;
if(start==NULL)
{
printf("List is Empty\n");
return ;
}
temp=start;
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("\n\n");
}
int main()
{
int value;
node *start=NULL;
int choice,data1;
while(1)
{
printf("\n1.insert_at_start\n2.insert at end.\n3.insert_after");
printf("\n4.display\n");
printf("enter choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter value\n");
scanf("%d",&value);
insert_at_front(start,value);
break;
case 2:
printf("Enter value\n");
scanf("%d",&value);
insert_at_end(start,value);
break;
case 3:
printf("Enter value\n");
printf("Enter value after which you want to insert\n");
scanf("%d%d",&value,&data1);
insert_after(start,value,data1);
break;
case 4:
display(start);
break;
default:
break;
}
}
return 0;
}

One problem with your code is that you're not using the return values from the different insert procedures. This means that if you start with an empty list (NULL) there is no way that main can get a non-empty list back.
At least you need to update start, for example:
start = insert_at_front(start,value);

Related

Why this program of stacking using linked list is not working?

Code compiles just fine but when i try to pop or display the pushed integer values it crashes ! Thanx in advance for helping me out.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
}*top=NULL;
void push(int);
void pop();
void display();
void main()
{
int choice,value;
while(1){
printf("\n-----MENU-----\n");
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter a number to push\n");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
}}
getch();
}
FUNCTION TO PUSH A VALUE
void push (int value)
{
struct node*newnode;
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data=value;
if(top==NULL){
newnode->next=NULL;
}
else
{
newnode->next=top;
top=newnode;
printf("Insertion successful\n");
}
}
FUNCTION TO POP A VALUE FROM LIST
void pop()
{
if (top==NULL)
{
printf("Nothing to delete");
}
else{
struct node *temp=top;
printf("Deleted element %d", temp->data);
top=temp->next;
free(temp);
}}
FUNCTION TO DISPLAY THE STACKED ELEMENTS
void display()
{
if(top==NULL)
{
printf("List is empty\n");
}
else
{
struct node *temp=top;
while(temp->next!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
printf("%d ----->NULL", temp->data);
}
}
You forgot to set top after insertion of the first element, change to (also never forget to verify your allocation status):
void push (int value) {
struct node *newnode = malloc(sizeof(struct node));
if (newnode==NULL) { /* error */ }
newnode->data=value;
newnode->next=top;
top=newnode;
printf("Insertion successful\n");
}

undefined reference to 'clrscr' and ld returned 1 exit status

Im trying to compile the c code free available. The code is relative to implement a Linked List.
But when I try to build and run the code I get this error twice:
undefined reference to 'clrscr'
and this error one time:
error: ld returned 1 exit status
Im a beginner in c and Im with some difficutlties trying to fix this, do you see what can be wrong?
code:
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void create();
void insert_atfirst();
void insert_atlast();
void insert();
void delete_bypos();
void delete_byval();
void display();
int menu();
struct node
{
int data;
struct node *next;
}*first=NULL,*last=NULL,*temp=NULL;
void main()
{
int ch;
clrscr();
do{
ch=menu();
switch(ch)
{
case 1:create();
break;
case 2:insert_atfirst();
break;
case 3:insert_atlast();
break;
case 4:insert();
break;
case 5:delete_bypos();
break;
case 6:delete_byval();
break;
case 7:display();
break;
case 8:exit(0);
case 9:clrscr();
default:printf("\nError-->Enter a valid choice!!");
exit(0);
}
}while(1);
}
void create()
{
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the Data in the Node:");
scanf("%d",&temp->data);
temp->next=NULL;
if(first==NULL)
{
first=temp;
last=temp;
}
else
{
last->next=temp;
last=temp;
}
}
void insert_atfirst()
{
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter Element:");
scanf("%d",&temp->data);
temp->next=first;
first=temp;
}
void insert()
{
struct node *t, *t1;
int pos, count=1,AB;
printf("\nEnter Position to be inserted:");
scanf("%d",&pos);
printf("\nAfter[1] or Before[2]:");
scanf("%d",&AB);
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&temp->data);
temp->next=NULL;
t=t1=first;
while(t!=NULL)
{
if(pos==count)
break;
else
{
t=t1;
t=t->next;
count++;
}
}
if(AB==1)
{
temp->next=t->next;
t->next=temp;
}
else if(AB==2)
{
temp->next=t;
t1->next=temp;
}
else
printf("\nInvalid Input");
}
void insert_atlast()
{
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter value to be inserted at last:");
scanf("%d",&temp->data);
last->next=temp;
last=temp;
}
void delete_bypos()
{
struct node *t,*t1;
int pos, count=1;
printf("\nEnter the Position of the element you would like to delete:");
scanf("%d",&pos);
t=t1=first;
while(t!=NULL)
{
if(pos==count)
t1=t;
t=t->next;
count++;
}
if(pos==1)
{
first=t->next;
printf("\nExceuted-->First Node is deleted!!");
}
else if(t==last)
{
t1->next=NULL;
free(t);
printf("\nExecuted-->Last Node is deleted!!");
}
else
{
t1->next=t->next;
free(t);
printf("\nExecuted-->Node has been deleted!!");
}
}
void delete_byval()
{
int val, count=1;
struct node *t,*t1;
printf("\nEnter value:");
scanf("%d",&val);
t=t1=first;
while(t!=NULL)
{
if(val==t->data)
break;
else
{
t=t->next;
count++;
}
}
if(t==first)
{
first=t->next;
free(t);
}
else if(first==last)
{
t1->next=NULL;
free(t);
last=t1;
}
else
{
t1->next=t->next;
free(t);
}
}
void display()
{
temp=first;
while(temp!=NULL)
{
printf("|%d|%d| --> ",temp->data,temp->next);
temp=temp->next;
}
}
int menu()
{
int ch;
printf("\n------------");
printf("\nSingle Linked List");
printf("\n------------");
printf("\n1.Create\n2.Insert at first\n3.Insert at last\n4.Insert at Middle\n5.Delete by Position\n6.Delete by Value\n7.Display\n8.Exit");
printf("\n\n-->Enter Your Choice:");
scanf("%d",&ch);
return ch;
}
clrscr() is only applicable in Turbo C. Remove clrscr() in Dev C++ or in your online C compiler.
clrscr() function works only in Borland's C compilers - Turbo C etc. Use system("cls") function instead.

AddNew Function using Linked List In C

Question: Create a linked list containing values in the ascending values. Then write functions addNew() which will accept a value from the user and then call addBegin() and addafterValue() functions to add the input value in the appropriate place
e.g. consider the list is like this:
12,15,20,26 then if the user enters values 8, 16 & 30 the list will look like this: 8,12,15,16,20,26,30.
My program:
#include<stdio.h>
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *start=NULL;
void append()
{
NODE *temp,*ptr;
temp=(NODE *)malloc(sizeof(NODE));
printf("Enter data:");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next=temp;
}
}
void display()
{
NODE *ptr=start;
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void addBegin(int val)
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=start;
start=temp;
}
unsigned int addAfterValue(int val,NODE *ptr)
{
NODE *temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=ptr->next;
return temp;
}
void addNew()
{
int val;
unsigned int loc;
NODE *ptr=start;
printf("Enter value to add:");
scanf("%d",&val);
if(val<ptr->data) {
addBegin(val);
ptr=NULL;
}
while(ptr!=NULL) {
if(ptr->next!=NULL)
{
if(val<ptr->next->data)
{
addAfterValue(val,ptr);
ptr->next=loc;
ptr=NULL;
}
else
{
ptr=ptr->next;
}
}
if(ptr->next==NULL)
{
loc=addAfterValue(val,ptr);
ptr=NULL;
}
}
}
int main()
{
int ans;
do
{
printf("Enter [1]To append\n[2]To add new node\n[3]To display\n[0]To exit\n");
printf("Enter your choice:");
scanf("%d",&ans);
switch(ans)
{
case 1:
append();
break;
case 2:
addNew();
break;
case 3:
display();
break;
case 0:
break;
default:
printf("Wrong Input.Try again.");
}
}while(ans);
}
My doubt: The addBegin() function works perfectly. I think there's something wrong with addafterValue(). Can anyone help me by finding out my mistake?
Instead of passing the current pointer address.
Use the previous node pointer and assign to its next node.
void addAfterValue(int val,NODE *ptr)
{
NODE *temp = (NODE *)malloc(sizeof(NODE));
temp->data=val;
temp->next=ptr->next;
ptr->next = temp;
}
Change the addNew function
void addNew()
{
int val;
NODE *ptr=start;
NODE *prev= NULL;
printf("Enter value to add:");
scanf("%d",&val);
if( ptr == NULL || val < ptr->data)
{
addBegin(val);
return;
}
else
{
prev = ptr;
ptr=ptr->next;
}
while( ptr != NULL)
{
if( val <= ptr->data)
{
addAfterValue(val,prev);
return;
}
else
{
prev = ptr;
ptr=ptr->next;
}
}
/* Control comes here if the entire list is scanned.... Now append it to the end using prev pointer, as the new node is greater than all of the existing nodes */
}

How to insert node at begin ,end and selected position in singly linked list?

I am new for c programming ,i have tried myself inserting node in singly linked list program but i didn't get a proper output and i dont have any idea to correct my program if anybody knows please help.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
int loc;
void addbegin(int num)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(head=NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addend(int num)
{
struct node *temp1,*temp2;
temp1=(struct node *)malloc(sizeof(struct node));
temp1->data=num;
temp2=head;
if(head==NULL)
{
head=temp1;
head->next=NULL;
}
else
{
while(temp2->next != NULL)
temp2=temp2->next;
temp1->next=NULL;
temp2->next=temp1;
}
}
void pos(int num,int loc)
{
int length();
struct node *temp,*cur_ptr,*prev_ptr;
int i;
cur_ptr=head;
if(loc > (length()+1) || loc<= 0)
{
printf("it is illegal call:");
}
else
{
if(loc == 1)
{
addbegin(num);
}
else
{
for(i=1;i<loc;i++)
{
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->next;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
prev_ptr->next=temp;
temp->next=cur_ptr;
}
}
}
int length()
{
struct node *cur_ptr;
int count = 0;
cur_ptr=head;
while(cur_ptr!=NULL)
{
cur_ptr=cur_ptr->next;
count++;
}
return(count);
}
void display()
{
struct node *temp=NULL;
if(temp==NULL)
{
printf("list is empty:");
}
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
int main()
{
int num;
head=NULL;
int choice;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert at begin\n");
printf("2.insert at end\n");
printf("3.insert at selected position\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&choice)<=0)
{
printf("Enter only an Integer\n");
}
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the number to insert at begin : ");
scanf("%d",&num);
addbegin(num);
break;
case 2: printf("Enter the number to insert at end: ");
scanf("%d",&num);
addend(num);
break;
case 3: printf("Enter the number to insert at selected position: ");
scanf("%d",&num);
pos(num,loc);
break;
case 4: printf("display the values");
display();
break;
case 5: printf("exit");
display();
}
}
return 0;
}
i think the error is in my main function but am not clear in that please help
In your addbeginmethod there's at least one obvious error:
if(head=NULL)
should be
if (head == NULL)
as you need to compare, not assign.
In your posmethod you have a function declaration: int length(); which shouldn't be there, but rather at the top, before main.
Another issue, this time in the display method:
void display()
{
struct node *temp=NULL;
if(temp==NULL) {
printf("List is empty:");
}
while(temp!=NULL) {
printf("%d",temp->data);
temp=temp->next;
}
}
Here temp will always be NULL, I guess you meant to assign headto the temppointer, otherwise it will never traverse the list.
And finally, in the insert at specific position choice you need to ask for a location value and pass that along too the function call, so add a declaration for int loc;in main, and change the third case to this:
case 3:
printf("Enter the number to insert at selected position: ");
scanf("%d",&num);
printf("Enter the position: ");
scanf("%d",&loc);
pos(num,loc);
break;
to the
Finally I'm going to quote from the C99 standard, section 5.1.2.2.1 Program startup:
The function called at program startup is named main. The
implementation declares no prototype for this function. It shall be
defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as
argc and argv, though any names may be used, as they are local to the
function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
So, please, change your declaration of mainand include a returnline at the end (possibly return 0;indicating successful program exit).
This became rather lengthy. After the suggested changes your program should look something like this:
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void addbegin(int num)
{
struct node *temp;
temp = malloc(sizeof(struct node));
temp->data=num;
if(head==NULL) {
head=temp;
head->next=NULL;
} else {
temp->next=head;
head=temp;
}
}
void addend(int num)
{
struct node *temp1, *temp2;
temp1 = malloc(sizeof(struct node));
temp1->data = num;
temp2 = head;
if(head == NULL) {
head = temp1;
head->next = NULL;
} else {
while(temp2->next != NULL)
temp2=temp2->next;
temp1->next=NULL;
temp2->next=temp1;
}
}
int length()
{
struct node *cur_ptr;
int count = 0;
cur_ptr = head;
while(cur_ptr != NULL) {
cur_ptr = cur_ptr->next;
count++;
}
return (count);
}
void pos(int num, int loc)
{
struct node *temp, *cur_ptr, *prev_ptr;
int i;
cur_ptr=head;
if(loc > (length()+1) || loc<= 0) {
printf("it is illegal call:");
} else {
if(loc == 1) {
addbegin(num);
} else {
for(i=1; i<loc; i++) {
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->next;
}
temp = malloc(sizeof(struct node));
temp->data=num;
prev_ptr->next=temp;
temp->next=cur_ptr;
}
}
}
void display()
{
struct node *temp = head;
if(temp == NULL) {
printf("List is empty:");
}
printf("The list contains the following values:\n");
while(temp!=NULL) {
printf("%d\n",temp->data);
temp=temp->next;
}
}
int main()
{
int choice, num, loc;
head = NULL;
while(1) {
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert at begin\n");
printf("2.insert at end\n");
printf("3.Insert at selected position\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&choice)<=0) {
printf("Enter only an Integer\n");
}
switch(choice) {
case 1:
printf("Enter the number to insert at begin : ");
scanf("%d",&num);
addbegin(num);
break;
case 2:
printf("Enter the number to insert at end: ");
scanf("%d",&num);
addend(num);
break;
case 3:
printf("Enter the number to insert at selected position: ");
scanf("%d",&num);
printf("Enter the position: ");
scanf("%d",&loc);
pos(num,loc);
break;
case 4:
printf("Display the values\n");
display();
break;
case 5:
printf("exit");
exit(0); // maybe you should exit here.
display();
}
}
return 0;
}
void addbegin(int num)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(head==NULL)
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
and
void display()
{
struct node *temp=NULL;
temp = head;
if(temp==NULL)
{
printf("list is empty:");
}
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
int loc;
void addbegin(int num)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(head==NULL) //this not assign, you need == to compare
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addend(int num)
{
struct node *temp1,*temp2;
temp1=(struct node *)malloc(sizeof(struct node));
temp1->data=num;
temp2=head;
if(head==NULL)
{
head=temp1;
head->next=NULL;
}
else
{
while(temp2->next != NULL)
temp2=temp2->next;
temp1->next=NULL;
temp2->next=temp1;
}
}
int length()
{
struct node *cur_ptr;
int count = 0;
cur_ptr=head;
while(cur_ptr!=NULL)
{
cur_ptr=cur_ptr->next;
count++;
}
return(count);
}
void pos(int num,int loc)
{
struct node *temp,*cur_ptr,*prev_ptr;
int i;
cur_ptr=head;
if(loc > (length()+1) || loc<= 0)
{
printf("it is illegal call:");
}
else
{
if(loc == 1)
{
addbegin(num);
}
else
{
for(i=1;i<loc;i++)
{
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->next;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
prev_ptr->next=temp;
temp->next=cur_ptr;
}
}
}
void display()
{
struct node *temp=head;
if(temp==NULL)
{
printf("list is empty:");
}
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
int main()
{
int num;
int choice;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert at begin\n");
printf("2.insert at end\n");
printf("3.insert at selected position\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&choice)<=0)
{
printf("Enter only an Integer\n");
}
switch(choice)
{
case 1: printf("Enter the number to insert at begin : ");
scanf("%d",&num);
addbegin(num);
break;
case 2: printf("Enter the number to insert at end: ");
scanf("%d",&num);
addend(num);
break;
case 3: printf("Enter the number to insert at selected position: ");
scanf("%d",&num);
pos(num,loc);
break;
case 4: printf("\ndisplay the values: ");
display();
break;
case 5: printf("exit");
display();
}
}
return 0;
}
Try this
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*head;
int loc;
void addbegin(int num)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
if(head==NULL) //this not assign, you need == to compare
{
head=temp;
head->next=NULL;
}
else
{
temp->next=head;
head=temp;
}
}
void addend(int num)
{
struct node *temp1,*temp2;
temp1=(struct node *)malloc(sizeof(struct node));
temp1->data=num;
temp2=head;
if(head==NULL)
{
head=temp1;
head->next=NULL;
}
else
{
while(temp2->next != NULL)
temp2=temp2->next;
temp1->next=NULL;
temp2->next=temp1;
}
}
int length()
{
struct node *cur_ptr;
int count = 0;
cur_ptr=head;
while(cur_ptr!=NULL)
{
cur_ptr=cur_ptr->next;
count++;
}
return(count);
}
void pos(int num,int loc)
{
struct node *temp,*cur_ptr,*prev_ptr;
int i;
cur_ptr=head;
if(loc > (length()+1) || loc<= 0)
{
printf("it is illegal call:");
}
else
{
if(loc == 1)
{
addbegin(num);
}
else
{
for(i=1;i<loc;i++)
{
prev_ptr=cur_ptr;
cur_ptr=cur_ptr->next;
}
temp=(struct node*)malloc(sizeof(struct node));
temp->data=num;
prev_ptr->next=temp;
temp->next=cur_ptr;
}
}
}
void display()
{
struct node *temp=head;
if(temp==NULL)
{
printf("list is empty:");
}
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
int main()
{
int num;
int choice;
while(1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert at begin\n");
printf("2.insert at end\n");
printf("3.insert at selected position\n");
printf("4.Display\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d",&choice)<=0)
{
printf("Enter only an Integer\n");
}
switch(choice)
{
case 1: printf("Enter the number to insert at begin : ");
scanf("%d",&num);
addbegin(num);
break;
case 2: printf("Enter the number to insert at end: ");
scanf("%d",&num);
addend(num);
break;
case 3: printf("Enter the number to insert at selected position: ");
scanf("%d",&num);
printf("Enter the position to insert: 1 for insert at begin, so on: ");
scanf("%d",&loc);
pos(num,loc);
break;
case 4: printf("\ndisplay the values: ");
display();
break;
case 5:
display();
printf("\n exiting program \n");
exit(0);
}
}
return 0;
}
Try this:
#include<iostream>
using namespace std;
struct stu{
int id;
stu *next = NULL;
};
stu *first = NULL;
stu *last = NULL;
int opt;
void insert_end();
void display();
int main(){
do{
cout<<"\n\n0.Exit";
cout<<"\n1.Insert at end in linked list";
cout<<"\n2.Display linked list";
cout<<"\n\nEnter your choice: ";
cin>>opt;
switch(opt){
case 1:{
insert_end_list1();
break;
}
case 2:{
display_list1();
break;
}
}
}
while(opt != 0);
return 0;
}
void insert_end(){
stu *current = new stu;
cout<<"\n\nEnter the student id:";
cin>>current->id;
if(first == NULL){
cout<<"\n\nEmpty linked list";
first = last = current;
}
else{
last->next = current;
last = current;
}
}
void display(){
stu *p = first;
while(p != NULL){
cout<<p->id<<" ";
p = p->next;
}
}
This code uses structure for creation of a new node in singly linked list.

Linked list implementation in C?

The following code is a single linked list implementation in c. every time the function addtoqueue is being called, it creates a node and appends the node to the end of the list. The pointer list points to the first node of the linked list, but every time I update the value of node using input (the values are read from client connection), all the previous nodes in the linked list gets the last filename that has been inputed. i.e:
after 1st node creation (abc.txt as input): linked list has one node with value abc.txt;
after 2nd node (xyz.txt as input): linked list has two nodes with same filename xyz.txt.(instead of one node with abc and one node with xyz)
There's my implementation below, what end where is the logical failure?
struct listdata
{
char *filename;
struct listdata *next;
}*list;
void addtoqueue(int client,char *value)
{
char buffer[512];
char filepath[100];
struct listdata *temp,*input;
input=(struct listdata *)malloc(sizeof(struct listdata));
read(client,buffer,sizeof(buffer));
d = sscanf(buffer,"%s",filepath);
input->filename=&filepath;
if(list == NULL)
{
list=input;
list->next=NULL;
}
else if((list->next)==NULL)
{
list->next=input;
input->next=NULL;
}
else
{
temp=list->next;
while((temp->next)!=NULL)
{
temp=temp->next;
}
temp->next=input;
input->next=NULL;
}
//list points to the first node
}
This is simpler
void addtoqueue(int client,char *value)
{
char buffer[512];
char filepath[100];
struct listdata *temp=NULL,*input=NULL;
input=(struct listdata *)malloc(sizeof(struct listdata));
read(client,buffer,sizeof(buffer));
d=sscanf(buffer,"%s",filepath);
input->filename=&filepath;
input->next = NULL;
if(list == NULL)
{
list=input;
}
else
{
temp=list;
while(temp->next != NULL)
{
temp=temp->next;
}
temp->next=input;
}
}
Here is the full code for linked list..
this will definately help you
visit http://codingloverlavi.blogspot.in/2013/12/singly-linked-list.html for more details
//Single LinkList
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node*next;
};
typedef struct Node Node;
Node*sort2(Node*,Node**);
Node*sort(Node*start,Node**);
Node*reverseList(Node*,Node**);
void insertAfter(Node*,Node**,int,int);
Node* insertBefore(Node*,int,int);
void printList(Node*);
Node*insertLast(Node*,Node**,Node*);
Node*createNode(int);
int search(Node*,int);
Node*Delete(Node*,Node**,int);
Node*deleteLast(Node*,Node**);
int main()
{
int temp,ch,num;
Node *newNode,*start,*last;
start=last=NULL;
while(1)
{
printf("\n_________________menu__________________\n");
printf("1. insert at the end of list...\n");
printf("2. print the list...\n");
printf("3. search a speific item...\n");
printf("4. insert after a specific node...\n");
printf("5. insert a node before a specific node...\n");
printf("6. delete a specific node...\n");
printf("7. delete a last node...\n");
printf("8. reverse the link list...\n");
printf("9. sort the link list...\n");
printf("10. sort the link list(another method)...\n");
printf("11. exit...\n");
printf("\nenter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the data part of the node that you want to insert :\n");
scanf("%d",&temp);
newNode=createNode(temp);
start=insertLast(start,&last,newNode);
break;
case 2:
printList(start);
break;
case 3:
printf("enter the data that you want to search : ");
scanf("%d",&temp);
if(search(start,temp))
printf("the number you entered was in the list");
else
printf("the number you entered was not in the list");
break;
case 4:
printf("enter the data of the node after which you want to insert new node : ");
scanf("%d",&temp);
printf("enter the data part of the node that you want to insert : ");
scanf("%d",&num);
insertAfter(start,&last,temp,num);
break;
case 5:
printf("enter the data of the node before which you want to insert new node : ");
scanf("%d",&temp);
printf("enter the data part of the node that you want to insert : ");
scanf("%d",&num);
start=insertBefore(start,temp,num);
break;
case 6:
printf("enter the data part of the node that you want to delete : ");
scanf("%d",&temp);
start=Delete(start,&last,temp);
break;
case 7:
if(last==NULL)
{
printf("the list is empty...you can't delete any node...");
break;
}
start=deleteLast(start,&last);
break;
case 8:
start=reverseList(start,&last);
break;
case 9:
start=sort(start,&last);
break;
case 10:
start=sort2(start,&last);
break;
case 11:
exit(1);
default:
printf("you have entered a wrong choice...enter a valid choice");
}
}
}
Node* createNode(int data)
{
Node*newNode;
newNode=(Node*)malloc(sizeof(Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
Node*insertLast(Node*start,Node**p2last,Node*newNode)
{
if(*p2last==NULL)
{
*p2last=newNode;
return newNode;
}
(*p2last)->next=newNode;
*p2last=newNode;
return start;
}
void printList(Node*start)
{
printf("your list is as follows : \n");
while(start)
{
printf("%d\t",start->data);
start=start->next;
}
}
int search(Node*start,int data)
{
while(start)
{
if(start->data==data)
return 1;
start=start->next;
}
return 0;
}
void insertAfter(Node*start,Node**p2last,int data,int num)
{
Node*newNode,*tmp;
newNode=createNode(num);
if(*p2last==NULL)
printf("the list is empty...");
tmp=start;
while(tmp)
{
if(tmp->data==data)
break;
tmp=tmp->next;
}
if(!tmp)
printf("the number you enter was not in the list\n");
else
{
newNode->next=tmp->next;
tmp->next=newNode;
if(tmp==*p2last)
*p2last=newNode;
}
}
Node*insertBefore(Node*start,int data,int num)
{
Node *newNode,*prev,*tmp;
prev=NULL;
newNode=createNode(num);
if(start==NULL)
{
printf("the list is empty...");
return start;
}
tmp=start;
while(tmp)
{
if(tmp->data==data)
break;
prev=tmp;
tmp=tmp->next;
}
if(!tmp)
printf("the number you enter was not in the list\n");
else if(prev==NULL)
{
newNode->next=start;
start=newNode;
}
else
{
newNode->next=prev->next;
prev->next=newNode;
}
return start;
}
Node*Delete(Node*start,Node**p2last,int data)
{
Node*prev,*tmp;
tmp=start;
prev=NULL;
while(tmp)
{
if(tmp->data==data)
break;
prev=tmp;
tmp=tmp->next;
}
if(!tmp)
{
printf("the item you entered was not in the list...\n");
return start;
}
if(tmp==start)
{
if((*p2last)==start)
*p2last=NULL;
return NULL;
}
prev->next=tmp->next;
if(tmp==(*p2last))
*p2last=prev;
free(tmp);
return start;
}
Node*deleteLast(Node*start,Node**p2last)
{
return Delete(start,p2last,(*p2last)->data);
}
Node*reverseList(Node*start,Node**p2last)
{
Node*ptr,*tmp,*prev;
(*p2last)=start;
prev=NULL;
for(ptr=start;ptr;)
{
tmp=ptr->next;
ptr->next=prev;
prev=ptr;
ptr=tmp;
}
return prev;
}
Node*sort(Node*start,Node**p2last)
{
Node*ptr,*newNode,*tmp,*start1,*last1;
start1=last1=NULL;
while(start!=NULL)
{
tmp=ptr=start;
while(ptr)
{
if(tmp->data > ptr->data)
tmp=ptr;
ptr=ptr->next;
}
newNode=createNode(tmp->data);
start1=insertLast(start1,&last1,newNode);
start=Delete(start,p2last,tmp->data);
}
*p2last=last1;
return start1;
}
Node*sort2(Node*start,Node**p2last)
{
int *arr,count=0,i,tmp,j;
Node*ptr,*start1,*last1,*newNode;
ptr=start;
start1=last1=NULL;
while(ptr)
{
count++;
ptr=ptr->next;
}
arr=(int*)malloc(sizeof(int)*count);
ptr=start;
for(i=0;i<count;i++)
{
arr[i]=ptr->data;
ptr=ptr->next;
}
/* sorting the array bubble */
for(i=1;i<count;i++)
for(j=0;j<count-i;j++)
if(arr[j]>arr[j+1])
{
tmp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;
}
for(i=0;i<count;i++)
start1=insertLast(start1,&last1,createNode(arr[i]));
*p2last=last1;
return start1;
}

Resources