answersLogoWhite

0

C program for linked list

Updated: 8/10/2023
User Avatar

Wiki User

11y ago

Best Answer

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

struct node

{

int n;

struct node *ptr;

};

int main(void)

{

struct node *n1,*strt,*temp;

char a;

int cnt=0,i=0;

//char a[50],temp[100],line[100],word[50],e[50],final[100];

//FILE *fp,*fp1,*fp2;

strt=malloc(sizeof(struct node));

strt->n=90;

strt->ptr=NULL;

for(a='y';a!='n';)

{

n1=malloc(sizeof(struct node));

n1->n=++cnt;

n1->ptr=strt;

strt=n1;

scanf("%c",&a);

}

// ptr[1]=40;

// ptr[2]=30;

n1=strt;

while(n1!=NULL)

{ printf("\n %d at loc %p",n1->n,n1);

printf(" locaa %p \n",n1->ptr);

n1=n1->ptr;

}

return 0;

}

~

~

~

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

//implementation of doubly linked list.

#include<stdio.h>

#include<conio.h>

#include<malloc.h>

void addnode();

void traverse();

struct node *new(int);

struct node

{

int info;

struct node *prev;

struct node *next;

};

//typedef struct node z;

struct node *start,*p,*temp;

void main()

{

int a,ch;

clrscr();

while(ch!=0)

{

printf("Enter the number");

scanf("%d",&a);

p=new(a);

addnode(p);

printf("press 1 for continue and 0 for end");

scanf("%d",&ch);

}

printf("your list is as follows");

traverse(start);

getch();

}

struct node *new(int a)

{

//start=p;

p=(struct node*)malloc(sizeof(struct node));

p->info=a;

p->prev=0;

p->next=0;

return p;

}

void addnode()

{

temp=start;

if(start==0)

{

start=p;

}

else

{

while(temp->next!=0)

temp=temp->next;

temp->next=p;

p->prev=temp;

}

}

void traverse()

{

temp=start;

while(temp!=0)

{

printf("%d",temp->info);

temp=temp->next;

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include <stdio.h>

#include <conio.h>

#include <alloc.h>

void create();

void insert();

void delet();

void display();

struct node

{

int data;

struct node *link;

};

struct node *first=NULL,*last=NULL,*next,*prev,*cur;

void create()

{

cur=(struct node*)malloc(sizeof(struct node));

printf("\nENTER THE DATA: ");

scanf("%d",&cur->data);

cur->link=NULL;

first=cur;

last=cur;

}

void insert()

{

int pos,c=1;

cur=(struct node*)malloc(sizeof(struct node));

printf("\nENTER THE DATA: ");

scanf("%d",&cur->data);

printf("\nENTER THE POSITION: ");

scanf("%d",&pos);

if((pos==1) &&(first!=NULL))

{

cur->link = first;

first=cur;

}

else

{

next=first;

while(c<pos)

{

prev=next;

next=prev->link;

c++;

}

if(prev==NULL)

{

printf("\nINVALID POSITION\n");

}

else

{

cur->link=prev->link;

prev->link=cur;

}

}

}

void delet()

{

int pos,c=1;

printf("\nENTER THE POSITION : ");

scanf("%d",&pos);

if(first==NULL)

{

printf("\nLIST IS EMPTY\n");

}

else if(pos==1 && first->link==NULL)

{

printf("\n DELETED ELEMENT IS %d\n",first->data);

free(first);

first=NULL;

}

else if(pos==1 && first->link!=NULL)

{

cur=first;

first=first->link;

cur->link=NULL;

printf("\n DELETED ELEMENT IS %d\n",cur->data);

free(cur);

}

else

{

next=first;

while(c<pos)

{

cur=next;

next=next->link;

c++;

}

cur->link=next->link;

next->link=NULL;

if(next==NULL)

{

printf("\nINVALID POSITION\n");

}

else

{

printf("\n DELETED ELEMENT IS %d\n",next->data);

free(next);

}

}

}

void display()

{

cur=first;

while(cur!=NULL)

{

printf("\n %d",cur->data);

cur=cur->link;

}

}

void main()

{

int ch;

clrscr();

printf("\n\nSINGLY LINKED LIST");

do

{

printf("\n\n1.CREATE\n2.INSERT\n3.DELETE\n4.EXIT");

printf("\n\nENTER YOUR CHOICE : ");

scanf("%d",&ch);

switch(ch)

{

case 1:

create();

display();

break;

case 2:

insert();

display();

break;

case 3:

delet();

display();

break;

case 4:

exit(0);

default:

printf("Invalid choice...");

}

}while(1);

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

What is linked list? Write a program to represent a linked list in C.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Linked list is a great advantage over array & the both are just opposite.So why don't you check out the problem properly.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program for linked list
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program in C to create a Circular Linked list?

#include&lt;iostream.h&gt;


How to write aC program to merge two singly linked list?

write a c program to circular queue


C program to implement deque using circular linked list?

You'll need to use a doubly-linked circular list, since otherwise when you pop off the tail element you'll need to whizz all the way round the list to find its predecessor. See the links section for an implementation of a doubly-linked circular list.


What is meant by doubly linked list?

In C programming, a double linked-list refers to a linked data structure that contains a set of links that have been linked sequentially.


Consider a linked list with n integers Each node of the list is numbered from 1 to n Develop a program using C language to split this list into 4 lists so that.?

list any six consteations


Write a Linked list program for insertion and deletion of a node using structureswitch case and in c language?

for all datastructure ,c,c++ programs just visit codingdatastructure.blogspot.com and found on blog archive..


32 can you create a linked list with out structure pointer?

Not in C, no.


Write a algorithm for doubly linked list in c?

sorry


What is linked list in C langauage?

A linked list is a collection of items, often nodes, that are sequentially linked by some kind of index or pointer contained within each item.


Which of the following data structures can be randomly accessed giving loc A. linked list implemented using array B. singly linked list C. double linked list D. both single and double linked list?

Which of the following data structures can be randomly accessed giving loc?A. linked list implemented using arrayB. singly linked listC. double linked listD. both single and double linked listThe answer is A.


Is there a c program to multiply two polynomials using linked lists?

yes


What does the word 'node' mean in c plus plus?

A node is an object in some kind of linked structure, such as a linked list or tree. Nodes can be allocated off of the free list and added to the linked list or tree at hand, and then filled in with the data needed to represent some piece of information. Conversely, they can be unlinked from the linked list or tree, and then returned to the free list when they are no longer needed. Often, there is no direct variable of pointer in the program's scope that represents the node; rather, the node can be found by traversing a list of linkages, list or tree, in the applicable data structure. There are other definitions of "node", but this is the most common.