Question

C LANGUAGE I just need the void push() function, which inserts new node to the front...

C LANGUAGE

I just need the void push() function, which inserts new node to the front of the list


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

typedef struct node
{
int data;
struct node *next;
} Node;

//Creating head a as a global Node*
Node *head;

/* Given a node prev_node, insert a new node after the given
prev_node */
void
insertAfter (Node * prev_node, int new_data)
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
printf ("the given previous node cannot be NULL");
return;
}

/* 2. allocate new node */
Node *new_node = (Node *) malloc (sizeof (Node));

/* 3. put in the data */
new_node->data = new_data;

/* 4. Make next of new node as next of prev_node */
new_node->next = prev_node->next;

/* 5. move the next of prev_node as new_node */
prev_node->next = new_node;
}


/* Given an int, appends a new node at the end */
void
append (int new_data)
{
/* 1. allocate node */
Node *new_node = (Node *) malloc (sizeof (Node));

Node *last = head;       /* used in step 5 */

/* 2. put in the data */
new_node->data = new_data;

/* 3. This new node is going to be the last node, so make next of
it as NULL */
new_node->next = NULL;

/* 4. If the Linked List is empty, then make the new node as head */
if (head == NULL)
{
head = new_node;
return;
}

/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;

/* 6. Change the next of last node */
last->next = new_node;
return;
}

/* Given an int, inserts a new node on the front of the list. */
void
push (int new_data)
{


//complete code
  


}


// This function prints contents of linked list starting from head
void
printList ()
{
Node *node;
node = head;
while (node != NULL)
{
printf (" %d ", node->data);
node = node->next;
}
printf ("\n");

}

// This function deletes the first occurance of num in the linked list starting from head
// Delete function returns 1 if the num is in the list and deleted othewise returns 0
int delete(int num)
{
struct node *temp, *prev;
// start at first node
temp=head;
// loop until end of list
while(temp!=NULL)
{ // does the data match?
if(temp->data==num)
{ // yes, then is start or other node?
if(temp==head)
{ // start node, so delete it
head=temp->next;
free(temp);
return 1;
}
else
{ // node in list, so delete it
prev->next=temp->next;
free(temp);
return 1;
}
}
else
{ //continue search on down list
prev=temp;
temp= temp->next;
}
}
return 0;
}


// This function counts the number of elements in the linked list and returns the count   
int count()
{ // scan list and count how many nodes
struct node *n;
int c=0;
n=head;
while(n!=NULL)
{
n=n->next;
c++;
}
return c;
}
int
main ()
{
int i, num;
Node *n;
head = NULL;
while (1)
{
printf ("\nList Operations\n");
printf ("===============\n");
printf ("1.Append\n");
printf ("2.Push\n");
printf ("3.Display\n");
printf ("4.Size\n");
printf ("5.Delete\n");
printf ("6.Exit\n");
printf ("Enter your choice : ");
if (scanf ("%d", &i) <= 0)
   {
   printf ("Enter only an Integer\n");
   exit (0);
   }
else
   {
   switch (i)
   {
   case 1:
   printf ("Enter the number to append : ");
   scanf ("%d", &num);
   append (num);
   break;

   case 2:
   printf ("Enter the number to push : ");
   scanf ("%d", &num);
   push (num);
   break;

   case 3:
   if (head == NULL)
       {

       printf ("List is Empty\n");
       }
   else
       {

       printf ("Element(s) in the list are : ");

       printList ();
       }
   break;

   case 4:
   printf ("Size of the list is %d\n", count ());
   break;

   case 5:
   if (head == NULL)

       printf ("List is Empty\n");
   else
       {

       printf ("Enter the number to delete : ");

       scanf ("%d", &num);

       if (delete (num))

       printf ("%d deleted successfully\n", num);

       else

       printf ("%d not found in the list\n", num);
       }
   break;

   case 6:
   return 0;

   default:
   printf ("Invalid option\n");
   }
   }
}
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<stdlib.h>

typedef struct node

{

int data;

struct node *next;

} Node;

//Creating head a as a global Node*

Node *head;

/* Given a node prev_node, insert a new node after the given

prev_node */

void

insertAfter (Node * prev_node, int new_data)

{

/*1. check if the given prev_node is NULL */

if (prev_node == NULL)

{

printf ("the given previous node cannot be NULL");

return;

}

/* 2. allocate new node */

Node *new_node = (Node *) malloc (sizeof (Node));

/* 3. put in the data */

new_node->data = new_data;

/* 4. Make next of new node as next of prev_node */

new_node->next = prev_node->next;

/* 5. move the next of prev_node as new_node */

prev_node->next = new_node;

}


/* Given an int, appends a new node at the end */

void

append (int new_data)

{

/* 1. allocate node */

Node *new_node = (Node *) malloc (sizeof (Node));

Node *last = head; /* used in step 5 */

/* 2. put in the data */

new_node->data = new_data;

/* 3. This new node is going to be the last node, so make next of

it as NULL */

new_node->next = NULL;

/* 4. If the Linked List is empty, then make the new node as head */

if (head == NULL)

{

head = new_node;

return;

}

/* 5. Else traverse till the last node */

while (last->next != NULL)

last = last->next;

/* 6. Change the next of last node */

last->next = new_node;

return;

}

/* Given an int, inserts a new node on the front of the list. */

void

push (int new_data)

{


Node *new_node = (Node *) malloc (sizeof (Node));

new_node->data = new_data;

new_node->next = NULL;


if (head == NULL)

{

head = new_node;

}else {

new_node->next = head;

head = new_node;

}


}


// This function prints contents of linked list starting from head

void

printList ()

{

Node *node;

node = head;

while (node != NULL)

{

printf (" %d ", node->data);

node = node->next;

}

printf ("\n");

}

// This function deletes the first occurance of num in the linked list starting from head

// Delete function returns 1 if the num is in the list and deleted othewise returns 0

int delete(int num)

{

struct node *temp, *prev;

// start at first node

temp=head;

// loop until end of list

while(temp!=NULL)

{ // does the data match?

if(temp->data==num)

{ // yes, then is start or other node?

if(temp==head)

{ // start node, so delete it

head=temp->next;

free(temp);

return 1;

}

else

{ // node in list, so delete it

prev->next=temp->next;

free(temp);

return 1;

}

}

else

{ //continue search on down list

prev=temp;

temp= temp->next;

}

}

return 0;

}


// This function counts the number of elements in the linked list and returns the count

int count()

{ // scan list and count how many nodes

struct node *n;

int c=0;

n=head;

while(n!=NULL)

{

n=n->next;

c++;

}

return c;

}

int

main ()

{

int i, num;

Node *n;

head = NULL;

while (1)

{

printf ("\nList Operations\n");

printf ("===============\n");

printf ("1.Append\n");

printf ("2.Push\n");

printf ("3.Display\n");

printf ("4.Size\n");

printf ("5.Delete\n");

printf ("6.Exit\n");

printf ("Enter your choice : ");

if (scanf ("%d", &i) <= 0)

{

printf ("Enter only an Integer\n");

exit (0);

}

else

{

switch (i)

{

case 1:

printf ("Enter the number to append : ");

scanf ("%d", &num);

append (num);

break;

case 2:

printf ("Enter the number to push : ");

scanf ("%d", &num);

push (num);

break;

case 3:

if (head == NULL)

{

printf ("List is Empty\n");

}

else

{

printf ("Element(s) in the list are : ");

printList ();

}

break;

case 4:

printf ("Size of the list is %d\n", count ());

break;

case 5:

if (head == NULL)

printf ("List is Empty\n");

else

{

printf ("Enter the number to delete : ");

scanf ("%d", &num);

if (delete (num))

printf ("%d deleted successfully\n", num);

else

printf ("%d not found in the list\n", num);

}

break;

case 6:

return 0;

default:

printf ("Invalid option\n");

}

}

}

return 0;

}


------------------------------------------------------------------------------------------

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern. please UPVOTE

Add a comment
Know the answer?
Add Answer to:
C LANGUAGE I just need the void push() function, which inserts new node to the front...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C, I need help debugging this program. I have a few error messages that I'm...

    Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h>...

    Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • ^^^ Q3. I am trying to implement double linked list but I was failed to write...

    ^^^ Q3. I am trying to implement double linked list but I was failed to write the code so anyone gives the main Code in the main function   THANK YOU FOR ADVANCE #include<stdio.h> #include<stdlib.h> #include<alloc.h> struct node {      int info;      struct node *lptr,*rptr; }; typedef struct node DL; DL *delete( ) , *insert ( ); void display(); DL *delete(DL *start,int x) {      DL *left,*right,*curr;      curr = start;      if( start == NULL)       {                 printf("\nDoubly...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT