Add to Favorites    Make Home Page 4880 Online  
 Language Categories  
 Our Services  

Home » C Home » C on Unix Home » complete single linked list program

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title complete single linked list program
Author D.Raja
Author Email raja2_d [at] yahoo.co.in
Description Single linked list inplementation using different functions

1.INSERT A NUMBER AT THE BEGINNING;

2.INSERT A NUMBER AT LAST

3.INSERT A NUMBER AT A PARTICULAR LOCATION IN LIST

4.PRINT THE ELEMENTS IN THE LIST

5.PRINT THE TOTAL NUMBER OF ELEMENTS IN THE LIST

6.DELETE A NODE IN THE LINKED LIST:

7.REVERSE A LINKED LIST :

8.GET OUT OF LINKED LIST (BYEE BYEE):


Category C » C on Unix
Hits 492660
Code Select and Copy the Code
Code : /* PROGRAM IMPLEMENTATION OF SINGLE LINKED LIST */ #include"stdio.h" //#define NULL 0 /* STRUCTURE CONTANING A DATA PART AND A LINK PART */ struct node { int data; struct node *next; }*p; /* P IS A GLOBAL POINTER CONTAINS THE ADRESS OF THE FIRST NODE IN LIST */ /*THIS FUNCTION DELETES A NODE */ delnode(int num) { struct node *temp, *m; temp=p; while(temp!=NULL) { if(temp->data==num) { if(temp==p) { p=temp->next; free(temp); return; } else { m->next=temp->next; free(temp); return; } }else { m=temp; temp= temp->next; } } printf(" ELEMENT %d NOT FOUND ", num); }/*THIS FUNCTION ADDS A NODE AT THE LAST OF LINKED LIST */ append( int num ) { struct node *temp,*r; /* CREATING A NODE AND ASSIGNING A VALUE TO IT */ temp= (struct node *)malloc(sizeof(struct node)); temp->data=num; r=(struct node *)p; if (p == NULL) /* IF LIST IS EMPTY CREATE FIRST NODE */ { p=temp; p->next =NULL; } else { /* GO TO LAST AND ADD*/ while( r->next != NULL) r=r->next; r->next =temp; r=temp; r->next=NULL; } }/* ADD A NEW NODE AT BEGINNING */ addbeg( int num ) { /* CREATING A NODE AND INSERTING VALUE TO IT */ struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->data=num; /* IF LIST IS NULL ADD AT BEGINNING */ if ( p== NULL) { p=temp; p->next=NULL; } else { temp->next=p; p=temp; } } /* ADD A NEW NODE AFTER A SPECIFIED NO OF NODES */ addafter(int num, int loc) { int i; struct node *temp,*t,*r; r=p; /* here r stores the first location */ if(loc > count()+1 || loc <= 0) { printf(" insertion is not possible : "); return; } if (loc == 1)/* if list is null then add at beginning */ { addbeg(num); return; } else { for(i=1;i<loc;i++) { t=r; /* t will be holding previous value */ r=r->next; } temp=(struct node *)malloc(sizeof(struct node)); temp->data=num; t->next=temp; t=temp; t->next=r; return; } }/* THIS FUNCTION DISPLAYS THE CONTENTS OF THE LINKED LIST */ display(struct node *r) { r=p; if(r==NULL) { printf("NO ELEMENT IN THE LIST :"); return; } /* traverse the entire linked list */ while(r!=NULL) { printf(" -> %d ",r->data); r=r->next; } printf(" "); } //THIS FUNCTION COUNTS THE NUMBER OF ELEMENTS IN THE LIST count() { struct node *n; int c=0; n=p; while(n!=NULL) { n=n->next; c++; } return(c); } //THIS FUNCTION REVERSES A LINKED LIST reverse(struct node *q) { struct node *m, *n,*l,*s; m=q; n=NULL; while(m!=NULL) { s=n; n=m; m=m->next; n->next=s; } p=n; } /* THIS IS THE MAIN PROGRAM */ main() { int i; p=NULL; while(1) /* this is an indefinite loop */ { printf(" 1.INSERT A NUMBER AT BEGINNING; "); printf(" 2.INSERT A NUMBER AT LAST: "); printf(" 3.INSERT A NUMBER AT A PARTICULAR LOCATION INlIST: "); printf(" 4.PRINT THE ELEMENTS IN THE LIST : "); printf(" 5.PRINT THE NUMBER OF ELEMENTS IN THE LIST "); printf(" 6.DELETE A NODE IN THE LINKED LIST: "); printf(" 7.REVERSE A LINKED LIST : "); printf(" 8.GET OUT OF LINKED LIST (BYEE BYEE): "); printf(" PLEASE, ENTER THE NUMBER:"); scanf("%d",&i); /* ENTER A VALUE FOR SWITCH */ switch(i) { case 1: { int num; printf(" PLEASE ENTER THE NUMBER :-"); scanf("%d",&num); addbeg(num); break; } case 2: { int num; printf(" PLEASE ENTER THE NUMBER :-"); scanf("%d",&num); append(num); break; } case 3: { int num, loc,k; printf(" PLEASE ENTER THE NUMBER :-"); scanf("%d",&num); printf(" PLEASE ENTER THE LOCATION NUMBER :-"); scanf("%d",&loc); addafter(num,loc); break; } case 4: { struct node *n; printf(" THE ELEMENTS IN THE LIST ARE : "); display(n); break; } case 5: { struct node *n; display(n); printf(" TOTAL NO OF ELEMENTS IN THE LSIT ARE %d",count()); break; } case 6: { int num; printf(" PLEASE ENTER A NUMBER FROM THE LIST :"); scanf("%d",&num); delnode(num); break; } case 7: { reverse(p); display(p); break; } case 8: { exit(); } }/* end if switch */ }/* end of while */ }/* end of main */

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi ¦Õ¤ ?¤Ð Ãß??ÝÐÝ
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=891


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review