creation of linkedlist: unable to print linked list


#include<stdio.h>
#include<stdlib.h>

int main()
{
struct node{
int data;
struct node *next;
};

	struct node *start=NULL, *last;
	int i,item;
	struct node *p= (struct node *) malloc(sizeof (struct node));	
	
			

	creation( struct node *start, struct node *p)
	{
	for(int i=0;i<7;i++)
	{
		printf("enter the item you want to insert\n");
		scanf("%d",&item);
		p->data= item;
		p->next=NULL;
		if( start==NULL)
		{
			start=p;
			//last=p;
		}
		else
		{
			//last=p;
			start->next= p;
			p->next=NULL;
		}
	}


	
	p=start;
	while(p!=NULL)
	{
		printf("\n inserted value%d\n ", p->data);
		p=p->next;
	}
}

#include <bits/stdc++.h>
using namespace std;
typedef struct node
{
int num;
struct node *ptr;
}node;
node *head,*first,*temp=0;
void insert(int n)
{
first=0;
for (int i=0;i<n;i++)
{
head=new node;
cin >> head->num;
if(first!=0){temp->ptr=head;temp=head;}
else first=temp=head;
}
temp->ptr=0;
}
void print()
{
temp=first;
while(temp!=0){cout << temp->num <<" ";temp=temp->ptr;}
cout << endl;
}
void delete1(int x)
{
temp=first;
head=first;
if(temp->num==x) { first=temp->ptr; return ;}
while(temp!=0 && temp->num!=x)
{
head=temp;
temp=temp->ptr;
}
head->ptr=temp->ptr;

    free(temp);
}
void add(int b)
{
    temp=first;
    while(temp->ptr !=0)
    temp=temp->ptr;
head=new node;
head->num=b;
temp->ptr=head;
temp=head;
temp->ptr=0;

}
int main() 
{
int n;
cin >> n;
insert(n);
print();
int m;
cin >> m;
delete1(m);
print();
add(8);
print();
return 0;
}

this might help you !

Hey i have done some changes to your program. Have a look…

#include <stdlib.h>
#include <stdio.h>
struct node{ int data; struct node *next; };
//void creation(struct node *start, struct node *p);
int main() {

struct node *start=NULL, *start2=NULL, *last;
int i,item;
//struct node *p= (struct node *) malloc(sizeof (struct node));

// creation( struct node *start, struct node *p){
for(int i=0;i<7;i++)
{
struct node *p= (struct node *) malloc(sizeof (struct node));
printf(“enter the item you want to insert\n”);
scanf("%d",&item);
p->data= item;
p->next=NULL;
if( start==NULL)
{
start=p;
start2=p;
//last=p;
}
else
{
//last=p;
start->next= p;
p->next=NULL;
start=p;
}
}
//}

struct node *p;
p=start2;
while(p!=NULL)
{
    printf("\n inserted value%d\n ", p->data);
    p=p->next;
}

}

Why didn’t you declare pointer start in the function creation.

@rajni @honeyyadav did it later " struct node *start=NULL ", u can declare it at the blueprint also, both of them are same :XD.

//