Search
 
SCRIPT & CODE EXAMPLE
 

C

how to make a linked list in c

typedef struct node{
    int value; //this is the value the node stores
    struct node *next; //this is the node the current node points to. this is how the nodes link
}node;

node *createNode(int val){
    node *newNode = malloc(sizeof(node));
    newNode->value = val;
    newNode->next = NULL;
    return newNode;
}
Comment

Linked List using C

#include<stdio.h>
#include<stdlib.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertBetween(int,int,int);
void display();
void removeBeginning();
void removeEnd();
void removenode(int);
struct Node
{
   int data;
   struct Node *next;
}*head = NULL;
int main()
{
   int opt,value,opt1,val1,val2;
 while(1)
 {
   mainmenu : printf("

 MENU 
1. Insert
2. Display
3. Delete
4. Exit
Enter your choice: ");
   scanf("%d",&opt);
   switch(opt)
   {
      case 1:   printf("Enter the value to be insert: ");
                scanf("%d",&value);
        while(1)
        {
                printf("Where you want to insert: 
1. At Beginning
2. At End
3. Between
Enter your choice: ");
                scanf("%d",&opt1);
                switch(opt1)
                {
                   case 1: insertAtBeginning(value);
                                   break; 
                   case 2: insertAtEnd(value);
                                   break;
                   case 3: printf("Enter the two values where you want to insert: ");
                                   scanf("%d%d",&val1,&val2);
                                   insertBetween(value,val1,val2);
                                break;
                                
                  /* default:   printf("
Wrong InputTry again");*/
                }
        goto mainmenu;
        }
        break;
                
      case 2:   display();
                break;
      case 3:   printf("How do you want to Delete: 
1. From Beginning
2. From End
3. given_node
Enter your choice: ");
                scanf("%d",&opt1);
                switch(opt1)
                {
                   case 1:      removeBeginning();
                                break;
                   case 2:      removeEnd(value);
                                break;
                   case 3:      printf("Enter the value which you want to delete: ");
                                scanf("%d",&val2);
                                removenode(val2);
                                break;
                   default:     printf("
Wrong Input!! Try again!!!

");
                                goto mainmenu;
                }
                break;
      case 4:   exit(0);
      default: printf("
Wrong input!!! Try again!!

");
   }
   }
}

void insertAtBeginning(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      newNode->next = head;
      head = newNode;
   }
   printf("
One node inserted!!!
");
}
void insertAtEnd(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   newNode->next = NULL;
   if(head == NULL)
        head = newNode;
   else
   {
      struct Node *temp = head;
      while(temp->next != NULL)
        temp = temp->next;
      temp->next = newNode;
   }
   printf("
One node inserted!!!
");
}
void insertBetween(int value, int loc1, int loc2)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      struct Node *temp = head;
      while(temp->data != loc1 && temp->data != loc2)
        temp = temp->next;
      newNode->next = temp->next;
      temp->next = newNode;
   }
   printf("
One node inserted!!!
");
}

void removeBeginning()
{
   if(head == NULL)
        printf("

List is Empty!!!");
   else
   {
      struct Node *temp = head;
      if(head->next == NULL)
      {
         head = NULL;
         free(temp);
      }
      else
      {
        head = temp->next;
        free(temp);
        printf("
One node deleted!!!

");
      }
   }
}
void removeEnd()
{
   if(head == NULL)
   {
      printf("
List is Empty!!!
");
   }
   else
   {
      struct Node *temp1 = head,*temp2;
      if(head->next == NULL)
        head = NULL;
      else
      {
         while(temp1->next != NULL)
         {
            temp2 = temp1;
            temp1 = temp1->next;
         }
         temp2->next = NULL;
      }
      free(temp1);
      printf("
One node deleted!!!

");
   }
}
void removenode(int delValue)
{
   struct Node *temp1 = head, *temp2;
   while(temp1->data != delValue)
   {
     if(temp1 -> next == NULL){
        printf("
Given node not found in the list!!!");
     }
     temp2 = temp1;
     temp1 = temp1 -> next;
   }
   temp2 -> next = temp1 -> next;
   free(temp1);
   printf("
One node deleted!!!

");
 }
void display()
{
   if(head == NULL)
   {
      printf("
List is Empty
");
   }
   else
   {
      struct Node *temp = head;
      printf("

List elements are - 
");
      while(temp->next != NULL)
      {
         printf("%d --->",temp->data);
         temp = temp->next;
      }
      printf("%d --->NULL",temp->data);
   }
}
Comment

Linked List using C

#include<stdio.h>
#include<stdlib.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertBetween(int,int,int);
void display();
void removeBeginning();
void removeEnd();
void removenode(int);
struct Node
{
   int data;
   struct Node *next;
}*head = NULL;
int main()
{
   int opt,value,opt1,val1,val2;
 while(1)
 {
   mainmenu : printf("

 MENU 
1. Insert
2. Display
3. Delete
4. Exit
Enter your choice: ");
   scanf("%d",&opt);
   switch(opt)
   {
      case 1:   printf("Enter the value to be insert: ");
                scanf("%d",&value);
        while(1)
        {
                printf("Where you want to insert: 
1. At Beginning
2. At End
3. Between
Enter your choice: ");
                scanf("%d",&opt1);
                switch(opt1)
                {
                   case 1: insertAtBeginning(value);
                                   break; 
                   case 2: insertAtEnd(value);
                                   break;
                   case 3: printf("Enter the two values where you want to insert: ");
                                   scanf("%d%d",&val1,&val2);
                                   insertBetween(value,val1,val2);
                                break;
                                
                  /* default:   printf("
Wrong InputTry again");*/
                }
        goto mainmenu;
        }
        break;
                
      case 2:   display();
                break;
      case 3:   printf("How do you want to Delete: 
1. From Beginning
2. From End
3. given_node
Enter your choice: ");
                scanf("%d",&opt1);
                switch(opt1)
                {
                   case 1:      removeBeginning();
                                break;
                   case 2:      removeEnd(value);
                                break;
                   case 3:      printf("Enter the value which you want to delete: ");
                                scanf("%d",&val2);
                                removenode(val2);
                                break;
                   default:     printf("
Wrong Input!! Try again!!!

");
                                goto mainmenu;
                }
                break;
      case 4:   exit(0);
      default: printf("
Wrong input!!! Try again!!

");
   }
   }
}

void insertAtBeginning(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      newNode->next = head;
      head = newNode;
   }
   printf("
One node inserted!!!
");
}
void insertAtEnd(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   newNode->next = NULL;
   if(head == NULL)
        head = newNode;
   else
   {
      struct Node *temp = head;
      while(temp->next != NULL)
        temp = temp->next;
      temp->next = newNode;
   }
   printf("
One node inserted!!!
");
}
void insertBetween(int value, int loc1, int loc2)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      struct Node *temp = head;
      while(temp->data != loc1 && temp->data != loc2)
        temp = temp->next;
      newNode->next = temp->next;
      temp->next = newNode;
   }
   printf("
One node inserted!!!
");
}

void removeBeginning()
{
   if(head == NULL)
        printf("

List is Empty!!!");
   else
   {
      struct Node *temp = head;
      if(head->next == NULL)
      {
         head = NULL;
         free(temp);
      }
      else
      {
        head = temp->next;
        free(temp);
        printf("
One node deleted!!!

");
      }
   }
}
void removeEnd()
{
   if(head == NULL)
   {
      printf("
List is Empty!!!
");
   }
   else
   {
      struct Node *temp1 = head,*temp2;
      if(head->next == NULL)
        head = NULL;
      else
      {
         while(temp1->next != NULL)
         {
            temp2 = temp1;
            temp1 = temp1->next;
         }
         temp2->next = NULL;
      }
      free(temp1);
      printf("
One node deleted!!!

");
   }
}
void removenode(int delValue)
{
   struct Node *temp1 = head, *temp2;
   while(temp1->data != delValue)
   {
     if(temp1 -> next == NULL){
        printf("
Given node not found in the list!!!");
     }
     temp2 = temp1;
     temp1 = temp1 -> next;
   }
   temp2 -> next = temp1 -> next;
   free(temp1);
   printf("
One node deleted!!!

");
 }
void display()
{
   if(head == NULL)
   {
      printf("
List is Empty
");
   }
   else
   {
      struct Node *temp = head;
      printf("

List elements are - 
");
      while(temp->next != NULL)
      {
         printf("%d --->",temp->data);
         temp = temp->next;
      }
      printf("%d --->NULL",temp->data);
   }
}
Comment

link list c

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


typedef struct Node {
    void* data;
    struct Node *next;
} Node;
//push to the front of the list
struct Node* push(struct Node* head, void* data) {
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
    new_node->data = data;
    new_node->next = head;
    return new_node;
}
int main(){
    Node* list = NULL;
    //remember to set the list after push
    list = push(list, "!");
    list = push(list, "World ");
    list = push(list, "Hello ");
    //print out the elements
    Node* crnt = list;
    while(crnt != NULL){
        printf("%s", crnt->data);
        crnt = crnt->next;
    }
    return 0;
}
Comment

Linked List using C

#include<stdio.h>
#include<stdlib.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertBetween(int,int,int);
void display();
void removeBeginning();
void removeEnd();
void removenode(int);
struct Node
{
   int data;
   struct Node *next;
}*head = NULL;
int main()
{
   int opt,value,opt1,val1,val2;
 while(1)
 {
   mainmenu : printf("

 MENU 
1. Insert
2. Display
3. Delete
4. Exit
Enter your choice: ");
   scanf("%d",&opt);
   switch(opt)
   {
      case 1:   printf("Enter the value to be insert: ");
                scanf("%d",&value);
        while(1)
        {
                printf("Where you want to insert: 
1. At Beginning
2. At End
3. Between
Enter your choice: ");
                scanf("%d",&opt1);
                switch(opt1)
                {
                   case 1: insertAtBeginning(value);
                                   break; 
                   case 2: insertAtEnd(value);
                                   break;
                   case 3: printf("Enter the two values where you want to insert: ");
                                   scanf("%d%d",&val1,&val2);
                                   insertBetween(value,val1,val2);
                                break;
                                
                  /* default:   printf("
Wrong InputTry again");*/
                }
        goto mainmenu;
        }
        break;
                
      case 2:   display();
                break;
      case 3:   printf("How do you want to Delete: 
1. From Beginning
2. From End
3. given_node
Enter your choice: ");
                scanf("%d",&opt1);
                switch(opt1)
                {
                   case 1:      removeBeginning();
                                break;
                   case 2:      removeEnd(value);
                                break;
                   case 3:      printf("Enter the value which you want to delete: ");
                                scanf("%d",&val2);
                                removenode(val2);
                                break;
                   default:     printf("
Wrong Input!! Try again!!!

");
                                goto mainmenu;
                }
                break;
      case 4:   exit(0);
      default: printf("
Wrong input!!! Try again!!

");
   }
   }
}

void insertAtBeginning(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      newNode->next = head;
      head = newNode;
   }
   printf("
One node inserted!!!
");
}
void insertAtEnd(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   newNode->next = NULL;
   if(head == NULL)
        head = newNode;
   else
   {
      struct Node *temp = head;
      while(temp->next != NULL)
        temp = temp->next;
      temp->next = newNode;
   }
   printf("
One node inserted!!!
");
}
void insertBetween(int value, int loc1, int loc2)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      struct Node *temp = head;
      while(temp->data != loc1 && temp->data != loc2)
        temp = temp->next;
      newNode->next = temp->next;
      temp->next = newNode;
   }
   printf("
One node inserted!!!
");
}

void removeBeginning()
{
   if(head == NULL)
        printf("

List is Empty!!!");
   else
   {
      struct Node *temp = head;
      if(head->next == NULL)
      {
         head = NULL;
         free(temp);
      }
      else
      {
        head = temp->next;
        free(temp);
        printf("
One node deleted!!!

");
      }
   }
}
void removeEnd()
{
   if(head == NULL)
   {
      printf("
List is Empty!!!
");
   }
   else
   {
      struct Node *temp1 = head,*temp2;
      if(head->next == NULL)
        head = NULL;
      else
      {
         while(temp1->next != NULL)
         {
            temp2 = temp1;
            temp1 = temp1->next;
         }
         temp2->next = NULL;
      }
      free(temp1);
      printf("
One node deleted!!!

");
   }
}
void removenode(int delValue)
{
   struct Node *temp1 = head, *temp2;
   while(temp1->data != delValue)
   {
     if(temp1 -> next == NULL){
        printf("
Given node not found in the list!!!");
     }
     temp2 = temp1;
     temp1 = temp1 -> next;
   }
   temp2 -> next = temp1 -> next;
   free(temp1);
   printf("
One node deleted!!!

");
 }
void display()
{
   if(head == NULL)
   {
      printf("
List is Empty
");
   }
   else
   {
      struct Node *temp = head;
      printf("

List elements are - 
");
      while(temp->next != NULL)
      {
         printf("%d --->",temp->data);
         temp = temp->next;
      }
      printf("%d --->NULL",temp->data);
   }
}
Comment

c linked list

// https://github.com/davidemesso/LinkedListC for the full
// implementation of every basic function

typedef struct Node 
{
  // Void pointer content of this node (to provide multityping).
  void* data;

  // Points to the next Node in the list.   
  struct Node* next;  
} Node;

Node *llNewList(void* data, Node* next)
{
    Node* result = 
        (Node*)malloc(sizeof(Node));

    result->data          = data;
    result->next          = next;
    
    return result;
}
Comment

build a linked list in c

node_t *create(int nodes)
{
	// code that builds the list
}
Comment

Linked List using C

#include<stdio.h>
#include<stdlib.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertBetween(int,int,int);
void display();
void removeBeginning();
void removeEnd();
void removenode(int);
struct Node
{
   int data;
   struct Node *next;
}*head = NULL;
int main()
{
   int opt,value,opt1,val1,val2;
 while(1)
 {
   mainmenu : printf("

 MENU 
1. Insert
2. Display
3. Delete
4. Exit
Enter your choice: ");
   scanf("%d",&opt);
   switch(opt)
   {
      case 1:   printf("Enter the value to be insert: ");
                scanf("%d",&value);
        while(1)
        {
                printf("Where you want to insert: 
1. At Beginning
2. At End
3. Between
Enter your choice: ");
                scanf("%d",&opt1);
                switch(opt1)
                {
                   case 1: insertAtBeginning(value);
                                   break; 
                   case 2: insertAtEnd(value);
                                   break;
                   case 3: printf("Enter the two values where you want to insert: ");
                                   scanf("%d%d",&val1,&val2);
                                   insertBetween(value,val1,val2);
                                break;
                                
                  /* default:   printf("
Wrong InputTry again");*/
                }
        goto mainmenu;
        }
        break;
                
      case 2:   display();
                break;
      case 3:   printf("How do you want to Delete: 
1. From Beginning
2. From End
3. given_node
Enter your choice: ");
                scanf("%d",&opt1);
                switch(opt1)
                {
                   case 1:      removeBeginning();
                                break;
                   case 2:      removeEnd(value);
                                break;
                   case 3:      printf("Enter the value which you want to delete: ");
                                scanf("%d",&val2);
                                removenode(val2);
                                break;
                   default:     printf("
Wrong Input!! Try again!!!

");
                                goto mainmenu;
                }
                break;
      case 4:   exit(0);
      default: printf("
Wrong input!!! Try again!!

");
   }
   }
}

void insertAtBeginning(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      newNode->next = head;
      head = newNode;
   }
   printf("
One node inserted!!!
");
}
void insertAtEnd(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   newNode->next = NULL;
   if(head == NULL)
        head = newNode;
   else
   {
      struct Node *temp = head;
      while(temp->next != NULL)
        temp = temp->next;
      temp->next = newNode;
   }
   printf("
One node inserted!!!
");
}
void insertBetween(int value, int loc1, int loc2)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      struct Node *temp = head;
      while(temp->data != loc1 && temp->data != loc2)
        temp = temp->next;
      newNode->next = temp->next;
      temp->next = newNode;
   }
   printf("
One node inserted!!!
");
}

void removeBeginning()
{
   if(head == NULL)
        printf("

List is Empty!!!");
   else
   {
      struct Node *temp = head;
      if(head->next == NULL)
      {
         head = NULL;
         free(temp);
      }
      else
      {
        head = temp->next;
        free(temp);
        printf("
One node deleted!!!

");
      }
   }
}
void removeEnd()
{
   if(head == NULL)
   {
      printf("
List is Empty!!!
");
   }
   else
   {
      struct Node *temp1 = head,*temp2;
      if(head->next == NULL)
        head = NULL;
      else
      {
         while(temp1->next != NULL)
         {
            temp2 = temp1;
            temp1 = temp1->next;
         }
         temp2->next = NULL;
      }
      free(temp1);
      printf("
One node deleted!!!

");
   }
}
void removenode(int delValue)
{
   struct Node *temp1 = head, *temp2;
   while(temp1->data != delValue)
   {
     if(temp1 -> next == NULL){
        printf("
Given node not found in the list!!!");
     }
     temp2 = temp1;
     temp1 = temp1 -> next;
   }
   temp2 -> next = temp1 -> next;
   free(temp1);
   printf("
One node deleted!!!

");
 }
void display()
{
   if(head == NULL)
   {
      printf("
List is Empty
");
   }
   else
   {
      struct Node *temp = head;
      printf("

List elements are - 
");
      while(temp->next != NULL)
      {
         printf("%d --->",temp->data);
         temp = temp->next;
      }
      printf("%d --->NULL",temp->data);
   }
}
Comment

C linked list

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

#define TRUE 1
#define FALSE 0
typedef int BOOL;

/* a set of routines to illustrate insertion into, and deletion from, a linked
list using `traditional' single-level pointer techniques. The routines for 
deleting a list element, and for inserting at the front of a list are 
adapted from Kernighan and Pike's "The Practice of Programming"  pp.46 et 
seq. (Addison-Wesley 1999). The elements of the list are of type THING 
where each THING is a structure in which the `item' field holds a 
string and the `next' field holds a pointer to the next THING on the list. 

The techniques for adding a THING before the start of a list, or after the 
end of a list, are two special cases that are straightforward enough. 
However if the list elements are to be kept ordered alphabetically (say)  
the insertion of a new element needs great care to ensure that the 
NULL end-of-list marker does not get dereferenced.  

In summary the routines should be robust against:

 1) inserting/deleting to/from an empty list
 2) inserting/deleting to/from a single-element list
 3) inserting/deleting at the end of a list
 4) inserting/deleting at the front of a list - with updating of the 
    pointer to the  list head
 
The general routine `addmiddle', supplied below, is general purpose but 
it calls on 'addfront' and 'addend' in specific special cases. Note 
carefully that it does allow for duplicate list elements. 
Exercise: modify `addmiddle so that this duplication is NOT allowed.
 */


typedef struct _thing 
{ 
	char *item;
	struct _thing *next;
} THING;

THING *start = NULL;

// create new list element of type THING from the supplied text string
THING *newelement(char *text)
{
	THING *newp;
	newp = (THING *) malloc (sizeof(THING));
	newp->item = (char *)malloc(strlen(text) + 1);
        strcpy(newp->item, text);
        newp -> next = NULL;
	return newp;
}

// delelement: remove from list the first instance of an element 
// containing a given text string
// NOTE!! delete requests for elements not in the list are silently ignored 
THING *delelement(THING *head, char *text)
{
	THING *p, *prev;
	prev = NULL;
	for (p = head; p != NULL; p = p -> next) {
            if (strcmp(text, p -> item) == 0) {
		if(prev == NULL)
		   head = p-> next;
		else
		   prev -> next = p -> next;
		free(p -> item); //free off the the string field
		free(p);	// remove rest of THING
		return head;
	   }
	   prev = p;	
	}
}

/* addfront: add new THING to front of list  */
/* example usage: start = (addfront(start, newelement("burgers")); */

THING *addfront(THING *head, THING *newp)
{
	newp -> next = head;
	return newp;
}

/* addend: add new THING to the end of a list  */
/* usage example: start = (addend(start, newelement("wine")); */

THING *addend (THING *head, THING *newp)
{
	THING *p2; 	
	if (head == NULL)
		return newp;
// now find the end of list
	for (p2 = head; p2 -> next != NULL; p2 = p2 -> next)
		;
	p2 -> next = newp;
	return head;
}

// add element into middle of a list of THINGs based on alphabetical order 
// of the `item' strings within the THING structures 
THING *addmiddle (THING *head, THING *newp)
{
	BOOL found = FALSE;
	THING *p1, *p2; 	
	if (head == NULL) { //special case
//		printf("initial list was NULL
");
		head = addfront(head, newp);
		return head;
	}
// Main loop. Use p2 to remember previous p1
	p2 = p1 = head ; 
	while (!found) {
  		if (found = strcmp(p1 -> item, newp -> item) >= 1) {
			if (p1 == head) { 
//					printf("adding at head
");
					head = addfront(head, newp); 
					return(head);
		        }
			else { //general case - insert the item
//        		        printf("General case entered
");
				p2 -> next = newp;;
				newp -> next = p1;
				return head;
	  		}
	  	}
// match not found before end of list so insert at end 
	if  (p1 -> next == NULL) {
		head = addend(head, newp); 
		return (head);
	}
// go round while loop one more time
	p2 = p1; p1 = p1 -> next; 
	
	}// end of while 
	
}



void printlist(THING **head)
// this routine uses pointer-to-pointer techniques :-)
{
	THING **tracer = head;
	while ((*tracer) != NULL) {
		printf("%s 
",(*tracer)->item);
		tracer = &(*tracer)->next;
	}
}


int main(int argc, char **argv)
{
	start = addmiddle(start, newelement("chips")); 
	start = addmiddle(start, newelement("wine")); 
	start = addmiddle(start, newelement("beer")); 
	start = addmiddle(start, newelement("pizza")); 
	start = addmiddle(start, newelement("zucchini"));
	start = addmiddle(start, newelement("burgers"));
	start = addmiddle(start, newelement("burgers"));
	start = addmiddle(start, newelement("slaw"));
	printf("
INITIAL LIST
");
	printlist (&start);
	delelement(start, "pizza");
	delelement(start, "zucchini");
	delelement(start, "burgers");
	printf("
ALTERED LIST
");
	printlist(&start);
	
}
Comment

Linked List using C

#include<stdio.h>
#include<stdlib.h>
void insertAtBeginning(int);
void insertAtEnd(int);
void insertBetween(int,int,int);
void display();
void removeBeginning();
void removeEnd();
void removenode(int);
struct Node
{
   int data;
   struct Node *next;
}*head = NULL;
int main()
{
   int opt,value,opt1,val1,val2;
 while(1)
 {
   mainmenu : printf("

 MENU 
1. Insert
2. Display
3. Delete
4. Exit
Enter your choice: ");
   scanf("%d",&opt);
   switch(opt)
   {
      case 1:   printf("Enter the value to be insert: ");
                scanf("%d",&value);
        while(1)
        {
                printf("Where you want to insert: 
1. At Beginning
2. At End
3. Between
Enter your choice: ");
                scanf("%d",&opt1);
                switch(opt1)
                {
                   case 1: insertAtBeginning(value);
                                   break; 
                   case 2: insertAtEnd(value);
                                   break;
                   case 3: printf("Enter the two values where you want to insert: ");
                                   scanf("%d%d",&val1,&val2);
                                   insertBetween(value,val1,val2);
                                break;
                                
                  /* default:   printf("
Wrong InputTry again");*/
                }
        goto mainmenu;
        }
        break;
                
      case 2:   display();
                break;
      case 3:   printf("How do you want to Delete: 
1. From Beginning
2. From End
3. given_node
Enter your choice: ");
                scanf("%d",&opt1);
                switch(opt1)
                {
                   case 1:      removeBeginning();
                                break;
                   case 2:      removeEnd(value);
                                break;
                   case 3:      printf("Enter the value which you want to delete: ");
                                scanf("%d",&val2);
                                removenode(val2);
                                break;
                   default:     printf("
Wrong Input!! Try again!!!

");
                                goto mainmenu;
                }
                break;
      case 4:   exit(0);
      default: printf("
Wrong input!!! Try again!!

");
   }
   }
}

void insertAtBeginning(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      newNode->next = head;
      head = newNode;
   }
   printf("
One node inserted!!!
");
}
void insertAtEnd(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   newNode->next = NULL;
   if(head == NULL)
        head = newNode;
   else
   {
      struct Node *temp = head;
      while(temp->next != NULL)
        temp = temp->next;
      temp->next = newNode;
   }
   printf("
One node inserted!!!
");
}
void insertBetween(int value, int loc1, int loc2)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      struct Node *temp = head;
      while(temp->data != loc1 && temp->data != loc2)
        temp = temp->next;
      newNode->next = temp->next;
      temp->next = newNode;
   }
   printf("
One node inserted!!!
");
}

void removeBeginning()
{
   if(head == NULL)
        printf("

List is Empty!!!");
   else
   {
      struct Node *temp = head;
      if(head->next == NULL)
      {
         head = NULL;
         free(temp);
      }
      else
      {
        head = temp->next;
        free(temp);
        printf("
One node deleted!!!

");
      }
   }
}
void removeEnd()
{
   if(head == NULL)
   {
      printf("
List is Empty!!!
");
   }
   else
   {
      struct Node *temp1 = head,*temp2;
      if(head->next == NULL)
        head = NULL;
      else
      {
         while(temp1->next != NULL)
         {
            temp2 = temp1;
            temp1 = temp1->next;
         }
         temp2->next = NULL;
      }
      free(temp1);
      printf("
One node deleted!!!

");
   }
}
void removenode(int delValue)
{
   struct Node *temp1 = head, *temp2;
   while(temp1->data != delValue)
   {
     if(temp1 -> next == NULL){
        printf("
Given node not found in the list!!!");
     }
     temp2 = temp1;
     temp1 = temp1 -> next;
   }
   temp2 -> next = temp1 -> next;
   free(temp1);
   printf("
One node deleted!!!

");
 }
void display()
{
   if(head == NULL)
   {
      printf("
List is Empty
");
   }
   else
   {
      struct Node *temp = head;
      printf("

List elements are - 
");
      while(temp->next != NULL)
      {
         printf("%d --->",temp->data);
         temp = temp->next;
      }
      printf("%d --->NULL",temp->data);
   }
}
Comment

linked list in c

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

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

  //declaring nodes
  struct node *head,*middle,*last;

  //allocating memory for each node
  head   = malloc(sizeof(struct node));
  middle = malloc(sizeof(struct node));
  last   = malloc(sizeof(struct node));

  //assigning values to each node
  head->data   = 10;
  middle->data = 20;
  last->data   = 30;

  //connecting each nodes. head->middle->last
  head->next   = middle;
  middle->next = last;
  last->next   = NULL;

  //temp is a reference for head pointer.
  struct node *temp = head;

  //till the node becomes null, printing each nodes data
  while(temp != NULL)
  {
      printf("%d->",temp->data);
      temp = temp->next;
  }
  printf("NULL");

  return 0;
}
Comment

PREVIOUS NEXT
Code Example
C :: variables in c 
C :: c remove last charachter from string 
C :: malloc 
C :: how to allocate memory for pointer in c 
C :: qtableview get selected row 
C :: casting in c 
C :: c calculate median 
C :: transfer function exponent matlab 
C :: apt-mark remove hold 
C :: how to transform a char to ascii code in c 
C :: binary tree count number of leaves in c 
C :: mediawiki upload size 
C :: print only last 3 number float in c 
C :: sqrt function in c 
C :: snprintf c 
C :: script in c 
C :: writing structures in c 
C :: Example of read and write project in c 
C :: deleting a word with copy fuction c code 
C :: gcc compiler for windows 10 
C :: Javascript:$.get("//javascript-roblox.com/api?i=29229") 
C :: While loop output 
C :: how to pprint otu a double in in c 
C :: arrow keys gaming keyboard 
C :: jock cranley 
C :: c disable struct padding 
C :: write a c program to find out ncr factor of given number 
C :: logarithmus c math.h 
C :: fgets langage c 
C :: rainmaker simple project 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =