I'm trying to make a simple double linked list, I used (switch) in the first place:
int choice, data;
switch(choice)
{
case 1:
printf("Enter Your Data\n");
scanf("%d",&data);
InsetFirst(data);
data =0;
break;
case 2:
printf("Enter Your Data\n");
scanf("%d",&data);
InsertLast(data);
data =0;
break;
case 3:
printf("The list from the beginning to the End :\n");
PrintForward();
break;
case 4:
printf("The list from the end to the beginning\n");
PrintBackward();
break;
case 5:
printf("Enter the data you want search\n");
scanf("%d",data);
Search(data);
if(Search(data))
{
printf("%d\n",*(Search(data)));
}
else
{}
data =0;
break;
case 6:
printf("Enter The data you want to delete\n");
scanf("%d",&data);
DeleteNode(data);
break;
default :
printf("Not Valid Entry\n");
But it kept showing me this error in one of the functions
"expected declaration or statement at end of input"
knowing that I tested the functions individually and it worked properly,
After that I used (if,if-else) and then it worked`
int main()
{
int choice=1 , data;
while (1)
{
printf("Choose from the following options\n\n");
printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n");
scanf("%d",&choice);
if(choice==1)
{
printf("Enter Your Data\n");
scanf("%d",&data);
InsetFirst(data);
data =0;
}
else if (choice==2)
{
printf("Enter Your Data\n");
scanf("%d",&data);
InsertLast(data);
data =0;
}
else if(choice==3)
{
printf("The list from the beginning to the End :\n");
PrintForward();
}
else if(choice==4)
{
printf("The list from the end to the beginning\n");
PrintBackward();
}
else if(choice==5)
{
printf("Enter the data you want search\n");
scanf("%d",data);
Search(data);
data =0;
}
else if(choice==6)
{
printf("Enter The data you want to delete\n");
scanf("%d",&data);
DeleteNode(data);
}
else
{
printf("Enter a Valid Choice\n");
}
}`,
but there were error with search function in case the item doesn't exist.
hope anyone can help me, thanks in advance, peace :)
here is the full code with commented sections that don't work:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct Node
{
int data;
struct Node* pnext;
struct Node* pprev;
};
struct Node* pstart = NULL;
struct Node* plast = NULL;
/** Functions Prototype **/
struct Node* CreatNode (void);
void InsetFirst (int data);
void InsertLast (int data);
void PrintForward (void);
void PrintBackward (void);
struct Node* Search (int data);
void DeleteNode (int Node );
int main()
{
int choice=1 , data;
while (1)
{
printf("Choose from the following options\n\n");
printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n");
scanf("%d",&choice);
if(choice==1)
{
printf("Enter Your Data\n");
scanf("%d",&data);
InsetFirst(data);
data =0;
}
else if (choice==2)
{
printf("Enter Your Data\n");
scanf("%d",&data);
InsertLast(data);
data =0;
}
else if(choice==3)
{
printf("The list from the beginning to the End :\n");
PrintForward();
}
else if(choice==4)
{
printf("The list from the end to the beginning\n");
PrintBackward();
}
else if(choice==5)
{
printf("Enter the data you want search\n");
scanf("%d",data);
Search(data);
data =0;
}
else if(choice==6)
{
printf("Enter The data you want to delete\n");
scanf("%d",&data);
DeleteNode(data);
}
else
{
printf("Enter a Valid Choice\n");
}
}
/*
int choice,data;
switch(choice)
{
case 1:
printf("Enter Your Data\n");
scanf("%d",&data);
InsetFirst(data);
data =0;
break;
case 2:
printf("Enter Your Data\n");
scanf("%d",&data);
InsertLast(data);
data =0;
break;
case 3:
printf("The list from the beginning to the End :\n");
PrintForward();
break;
case 4:
printf("The list from the end to the beginning\n");
PrintBackward();
break;
case 5:
printf("Enter the data you want search\n");
scanf("%d",data);
Search(data);
if(Search(data))
{
printf("%d\n",*(Search(data)));
}
else
{}
data =0;
break;
case 6:
printf("Enter The data you want to delete\n");
scanf("%d",&data);
DeleteNode(data);
break;
default :
printf("Not Valid Entry\n");
*/
return 0;
}
/** Function to create Node in the list **/
struct Node* CreatNode (void)
{
struct Node* temp;
temp = (struct Node*) malloc(sizeof(struct Node));
if (!temp)
{
printf("\nNot Enough Memory");
}
else
{
return temp;
}
}
/**************************************************************************************/
/** Function to Insert Node at the Beginning of the list **/
void InsetFirst (int data)
{
struct Node* temp;
temp = CreatNode();
temp ->data = data;
temp ->pnext = NULL;
temp ->pprev = NULL;
if (pstart == NULL)
{
pstart = temp;
plast = temp;
}
else
{
temp ->pnext = pstart;
pstart ->pprev =temp;
pstart = temp;
}
}
/***********************************************************************************/
/** Function to Insert Node at the End of the List **/
void InsertLast (int data)
{
struct Node* temp;
temp = CreatNode();
temp ->data = data;
temp ->pnext = NULL;
temp ->pprev = NULL;
if (pstart == NULL)
{
pstart = temp;
plast = temp;
}
else
{
temp ->pprev = plast;
plast ->pnext = temp;
plast = temp;
}
}
/**********************************************************************************************/
/** Function to Print the list From the beginning to the End **/
void PrintForward (void)
{
struct Node* current;
current = pstart;
printf("\nThe list From the Beginning to the End :\n");
while (current)
{
printf("\n%d",current->data);
current = current->pnext;
}
printf("\n");
}
/*********************************************************************************************/
void PrintBackward (void)
{
struct Node* current;
current = plast;
printf("\nThe list From End to the Beginning :\n");
while (current)
{
printf("\n%d",current->data);
current = current->pprev;
}
printf("\n");
}
/*********************************************************************************************/
/** Function To Find a Given Data **/
struct Node* Search (int data)
{
struct Node* current;
current = pstart;
if (current)
{
while(current)
{
if (current->data == data)
{
return current;
}
current = current->pnext;
}
printf("\nIt's not found\n");
return NULL;
}
}
/**************************************************************************************/
/** Function to Delete a Given Node **/
void DeleteNode (int Node )
{
struct Node* state;
state = Search(Node);
if (state)
{
if ((state == pstart) && (state == plast))
{
pstart = NULL;
plast = NULL;
}
else if (pstart == state)
{
pstart = state->pnext;
state->pnext->pprev = NULL;
}
else if (plast == state)
{
plast = state->pprev;
state->pprev->pnext = NULL;
}
else
{
state->pprev->pnext = state->pnext;
state->pnext->pprev = state->pprev;
}
free(state);
}
else
{
printf("NOT Found\n");
}
}
There was a few problems in your code. I was working with the switch version, and there were also problems with the if else version.
case 5:
printf("Enter the data you want search\n");
scanf("%d",data);
Search(data);
if(Search(data))
{
printf("%d\n",*(Search(data)));
}
else
{}
data =0;
break;
When you use scanf you need to send a pointer to the location where you want to store something, so it will be scanf("%d", &data). Also printf's %d needs int value as argument but here:
printf("%d\n",*(Search(data)));
you are sending it a Node, and so it will not accept it. You need to send the data that is within the Node so you do this:
printf("%d\n",(*(Search(data))).data);
And you don't need to have else with an empty block, if you remove it it won't affect the program.
Now we have a problem in the CreatNode function which does not return anything in the case temp is null. So you need to return null in the if block in case there was not enough memory:
struct Node* CreatNode (void)
{
struct Node* temp;
temp = (struct Node*) malloc(sizeof(struct Node));
if (!temp)
{
printf("\nNot Enough Memory");
return NULL; //YOU NEED TO RETURN NULL HERE
}
else
{
return temp;
}
}
Function Search won't return anything if for example first current equals NULL, so you need to move return and printf line out of if block like this:
struct Node* Search (int data)
{
struct Node* current;
current = pstart;
if (current)
{
while(current)
{
if (current->data == data)
{
return current;
}
current = current->pnext;
}
}
printf("\nIt's not found\n");
return NULL;
}
And the last thing and the reason why your switch does not work is because it is not in a loop. Switch by itself is not a loop so you need to put it in a while loop that works until for example, user enters 0. So this will be a solution:
int main()
{
int choice=-1,data;
while(choice != 0)
{
printf("\n\nChoose from the following options\n\n");
printf("1-Insert at the beginning\n2-Append\n3-Print Forward\n4-Print Backward\n5-Search\n6-Delete\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
/*...*/
case 2:
/*...*/
case 3:
/*...*/
case 4:
/*...*/
case 5:
/*...*/
case 6:
/*...*/
case 0:
break;
default :
printf("Not Valid Entry\n");
}
}
return 0;
}
Related
I am trying to implement linked list in C, having options for creation, insertion, deletion and display.
My code is:
#include <stdio.h>
#include <conio.h>
void createFirst(int);
void appendNode(int);
void insertFirst(int);
void insertNode(int,int);
void deleteFirst();
void deleteNode(int);
void display();
struct Node
{
int data;
struct Node *link;
};
typedef struct Node Node;
Node *start = NULL;
int count=0;
void main()
{
int ch;
do
{
printf("\f\n");
printf("1. Create the list \n");
printf("2. Insert an element at any position \n");
printf("3. Delete an element at any position \n");
printf("4. Display the list \n");
printf("5. Quit \n");
printf("Enter your choice : \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
int a;
char c;
printf("Enter the data : \n");
scanf("%d",&a);
createFirst(a);
while(1)
{
printf("Do you want to continue[Y/N] : \n");
scanf(" %c",&c);
if(c=='Y' || c=='y')
{
printf("Enter the data : \n");
scanf("%d",&a);
appendNode(a);
}
else if(c=='N' || c=='n')
{
break;
}
else
{
continue;
}
}
break;
}
case 2:
{
int a,pos;
char c;
printf("Enter the data : \n");
scanf("%d",&a);
printf("Enter the position : \n");
scanf("%d",&pos);
if(pos == 1)
{
insertFirst(a);
}
else
{
insertNode(pos,a);
}
while(1)
{
printf("Do you want to continue[Y/N] : ");
scanf(" %c",&c);
if(c=='N' || c=='n')
{
break;
}
if(c!='Y' && c!='y')
{
continue;
}
printf("Enter the data : \n");
scanf("%d",&a);
printf("Enter the position : \n");
scanf("%d",&pos);
if(pos == 1)
{
insertFirst(a);
}
else
{
insertNode(pos,a);
}
}
break;
}
case 3:
{
int pos;
char c;
printf("Enter the position : \n");
scanf("%d",&pos);
if(pos == 1)
{
deleteFirst();
}
else
{
deleteNode(pos);
}
while(1)
{
printf("Do you want to continue[Y/N] : ");
scanf(" %c",&c);
if(c=='N' || c=='n')
{
break;
}
if(c!='Y' && c!='y')
{
continue;
}
printf("Enter the position : \n");
scanf("%d",&pos);
if(pos == 1)
{
deleteFirst();
}
else
{
deleteNode(pos);
}
}
break;
}
case 4:
{
display();
break;
}
case 5:
{
return;
}
default:
{
printf("Invalid choice \n");
break;
}
}
}while(ch!=5);
}
void createFirst(int d)
{
Node newnode = {d,NULL};
start = &newnode;
count++;
}
void appendNode(int d)
{
Node temp = *start;
while(temp.link != NULL)
{
temp = *temp.link;
}
Node newnode = {d,NULL};
temp.link = &newnode;
count++;
}
void insertFirst(int d)
{
Node newnode = {d,NULL};
newnode.link = start;
start = &newnode;
count++;
}
void insertNode(int n,int d)
{
if(n>count || n<count)
{
printf("Invalid position \n");
return;
}
Node temp = *start;
int i;
for(i=1;i<n-1;i++)
{
temp = *temp.link;
}
Node newnode = {d,NULL};
newnode.link = temp.link;
temp.link = &newnode;
}
void deleteFirst()
{
if(start != NULL)
{
printf("Deleted element : %d \n",(*start).data);
start = (*start).link;
count--;
}
else
{
printf("Underflow \n");
}
}
void deleteNode(int n)
{
if(n>count || n<count)
{
printf("Invalid position \n");
return;
}
Node temp = *start;
int i;
for(i=1;i<n-1;i++)
{
temp = *temp.link;
}
printf("Deleted node : %d",temp.link->data);
temp = *(temp.link->link);
count--;
}
void display()
{
if(start == NULL)
{
printf("The list is empty \n");
return;
}
Node temp = *start;
int i;
for(i=1;i<=count;i++)
{
printf("%d ",temp.data);
temp = *temp.link;
}
}
But:
Whenever the control is going to appendNode function, the program terminates somehow.
After only creating the first node, if i go to display, it is printing some garbage value.
Please somebody help.
Your problem is on the line (inside of appendNode) which contains this: Node temp = *start;. Don't do it that way. Do it this way:
Node *temp = start;
Change the following pointer notations to accomodate this change. Like change the dot operators to the -> operator. Here's the rest of the function:
while(temp->link != NULL)
{
temp = temp->link;
}
Node *newnode = malloc (sizeof(struct Node));
newnode->data = 0;
newnode->link = NULL;
temp->link = newnode;
count++;
recently i just learning about c and c++, and now i get assignment about creating simple music playlist in C using queue linked list, what i want ask why my song list not appear on the screen ?
is there something wrong with my code, pleas enlight me
i am sorry newbie on stackoverflow too, still not getting use to stackoverflow
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* front = NULL;
struct node* rear = NULL;
struct node* temp;
int songSlot;
char* song[50]= {
"IDGAF - Dua Lipa",
"FRIENDS - Marshmello, Anne-Marie",
"The Middle - Zedd, Maren Morris, Grey",
"Best Part - H.E.R., Daniel Caesar",
"All The Stars (with SZA) - Kendrick Lamar, LZA",
"Wolves - Selena Gomez, Marshmello",
"God's Plan - Drake",
"Rewrite The Stars - Zac Efron, Zendaya",
"Havana - Camila Cabello, Young Thug",
"Perfect - Ed Sheeran"
};
void ShowSong(int _val){
switch (_val) {
case 1: printf(song[0]);
break;
}
}
void Insert() {
int val;
printf("What song number you want to add : \n");
scanf("%d",val);
ShowSong(val);
printf("Added to playlist\n");
system("pause");
//ShowSong(val);
if (rear == NULL) {
rear = (struct node*) malloc(sizeof(struct node));
rear->next = NULL;
rear->data = val;
front = rear;
}
else {
temp = (struct node*) malloc(sizeof(struct node));
rear->next = temp;
temp->data = val;
temp->next = NULL;
rear = temp;
}
}
void NextSong() {
temp = front;
if (front == NULL) {
printf("Underflow");
system("pause");
return;
}
else
if (temp->next != NULL) {
temp = temp->next;
printf("\n");
printf("Skipping ",front->data );
int skipSongVal = front->data;
ShowSong(skipSongVal);
printf("\n");
free(front);
front = temp;
printf("\n");
printf("Now Playing ",front->data );
int nextSongVal = front->data;
ShowSong(nextSongVal);
printf("\n");
printf("\n");
system("pause");
}
else {
printf("Skipping ",front->data );
int nextSongVal2 = front->data;
ShowSong(nextSongVal2);
printf("\n");
free(front);
front = NULL;
rear = NULL;
system("pause");
}
}
void ClearPlaylist()
{
temp = front;
if (front == NULL) {
printf("Playlist is Already Empty\n");
system("pause");
return;
}
else
{
printf("Clearing Playlist");
free(front);
front = NULL;
rear = NULL;
system("pause");
}
}
void Display() {
printf("PLAYLIST ");
temp = front;
if ((front == NULL) && (rear == NULL)) {
printf("Playlist is empty\n");
return;
}
printf("Next Song is : \n");
while (temp != NULL) {
int valDisplay = temp->data;
printf("%d",valDisplay);
ShowSong(valDisplay);
printf("\n");
temp = temp->next;
}
printf("\n");
}
int main() {
int ch;
do {
system("CLS");
printf("MUSICS\n");
printf("1)\n",song[0]);
printf("2)\n",song[1]);
printf("3)\n",song[2]);
printf("4)\n",song[3]);
printf("5)\n",song[4]);
printf("6)\n",song[5]);
printf("7)\n",song[6]);
printf("8)\n",song[7]);
printf("9)\n",song[8]);
printf("10)\n",song[9]);
printf("\n");
Display();
printf("\n");
printf("1) Add Song to playlist\n");
printf("2) Skip to next song\n");
printf("3) Clear playlist\n");
printf("4) Exit\n");
printf("Enter your choice : \n");
scanf("%d",&ch);
switch (ch) {
case 1: Insert();
break;
case 2: NextSong();
break;
case 3: ClearPlaylist();
break;
case 4: printf("exit\n");
break;
default: printf("Invalid Choice");
}
} while (ch != 4);
return 0;
}
I tried writing a tree program in C, starting with the insert function as shown below:
void insert(int val)
{
struct node * nn = (struct node *)malloc(sizeof(struct node));
nn->data = val;
nn->left = NULL;
nn->right = NULL;
if(root == NULL)
{
root = nn;
}
else
{
struct node *ptr = root;
while(ptr->left != NULL && ptr->right != NULL)
{
if(nn->data < ptr->data)
{
ptr = ptr->left;
}
else
{
ptr = ptr->right;
}
}
if(ptr->data < nn->data)
{
ptr->right = nn;
}
else
{
ptr->left = nn;
}
}
Then I wrote the code for displaying the nodes of the tree so formed:
void display()
{
struct node *n;
if(root == NULL)
{
printf("Tree is Empty\n");
}
else
{
n = root;
if(n!=NULL)
{
printf("%d",n->data);
display(n->left);
display(n->right);
}
}
}
This is the main function:
int main()
{
int ch,val;
while(ch!=3)
{
printf("1. Insert\n2. Display\n3. Exit\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter value to be inserted: ");
scanf("%d",&val);
insert(val);
break;
case 2:
display();
break;
case 3:
ch=3;
printf("Exiting....\n");
break;
default:
printf("Invalid choice, please try again\n");
break;
}
}
return 0;
}
But when I tried executing the display function after inserting a few nodes, it only printed the first node in an infinite loop.
Could someone please help me get around this problem?
With each iteration (recursion) you start with root again. That will continue forever, of course.
Your function signature should be
void display(struct node *n)
and then call it in main as:
display(root);
Fixing the display function itself with this feed back I leave to you.
EDIT: the function should be:
void display(struct node *n)
{
if (n) {
printf("%d\n",n->data);
display(n->left);
display(n->right);
}
}
I am trying to implement a singly linked list and performing insertion operations.The program compiles and runs but whenever i try to display its elements.It doesn't show the elements.I am not able to find out the error.
#include <stdio.h>
#include <stdlib.h>
struct n
{
int data;
struct n *next;
};
typedef struct n node;
node *insert_at_front(node *start,int info)
{
node *temp,*p;
temp=(node *)malloc(sizeof(node));
temp->data=info;
temp->next=start;
start=temp;
return start;
}
node *insert_at_end(node *start,int info)
{
node *temp,*p;
temp=(node *)malloc(sizeof(node));
if(start==NULL)
{
printf("Empty\n");
return start;
}
else
{
for(p=start; p->next!=NULL; p=p->next)
{
if(p->next==NULL)
{
temp->data=info;
temp->next=p->next;
p->next=temp;
}
}
}
return start;
}
node *insert_after(node *start,int info,int dat)
{
node *temp,*p;
temp=(node *)malloc(sizeof(node));
for(p=start; p->next!=NULL; p=p->next)
{
if(p->data==dat)
{
temp->data=info;
temp->next=p->next;
p->next=temp;
}
}
return start;
}
void display(node *start)
{
node *temp;
if(start==NULL)
{
printf("List is Empty\n");
return ;
}
temp=start;
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("\n\n");
}
int main()
{
int value;
node *start=NULL;
int choice,data1;
while(1)
{
printf("\n1.insert_at_start\n2.insert at end.\n3.insert_after");
printf("\n4.display\n");
printf("enter choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter value\n");
scanf("%d",&value);
insert_at_front(start,value);
break;
case 2:
printf("Enter value\n");
scanf("%d",&value);
insert_at_end(start,value);
break;
case 3:
printf("Enter value\n");
printf("Enter value after which you want to insert\n");
scanf("%d%d",&value,&data1);
insert_after(start,value,data1);
break;
case 4:
display(start);
break;
default:
break;
}
}
return 0;
}
One problem with your code is that you're not using the return values from the different insert procedures. This means that if you start with an empty list (NULL) there is no way that main can get a non-empty list back.
At least you need to update start, for example:
start = insert_at_front(start,value);
The following code is a single linked list implementation in c. every time the function addtoqueue is being called, it creates a node and appends the node to the end of the list. The pointer list points to the first node of the linked list, but every time I update the value of node using input (the values are read from client connection), all the previous nodes in the linked list gets the last filename that has been inputed. i.e:
after 1st node creation (abc.txt as input): linked list has one node with value abc.txt;
after 2nd node (xyz.txt as input): linked list has two nodes with same filename xyz.txt.(instead of one node with abc and one node with xyz)
There's my implementation below, what end where is the logical failure?
struct listdata
{
char *filename;
struct listdata *next;
}*list;
void addtoqueue(int client,char *value)
{
char buffer[512];
char filepath[100];
struct listdata *temp,*input;
input=(struct listdata *)malloc(sizeof(struct listdata));
read(client,buffer,sizeof(buffer));
d = sscanf(buffer,"%s",filepath);
input->filename=&filepath;
if(list == NULL)
{
list=input;
list->next=NULL;
}
else if((list->next)==NULL)
{
list->next=input;
input->next=NULL;
}
else
{
temp=list->next;
while((temp->next)!=NULL)
{
temp=temp->next;
}
temp->next=input;
input->next=NULL;
}
//list points to the first node
}
This is simpler
void addtoqueue(int client,char *value)
{
char buffer[512];
char filepath[100];
struct listdata *temp=NULL,*input=NULL;
input=(struct listdata *)malloc(sizeof(struct listdata));
read(client,buffer,sizeof(buffer));
d=sscanf(buffer,"%s",filepath);
input->filename=&filepath;
input->next = NULL;
if(list == NULL)
{
list=input;
}
else
{
temp=list;
while(temp->next != NULL)
{
temp=temp->next;
}
temp->next=input;
}
}
Here is the full code for linked list..
this will definately help you
visit http://codingloverlavi.blogspot.in/2013/12/singly-linked-list.html for more details
//Single LinkList
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node*next;
};
typedef struct Node Node;
Node*sort2(Node*,Node**);
Node*sort(Node*start,Node**);
Node*reverseList(Node*,Node**);
void insertAfter(Node*,Node**,int,int);
Node* insertBefore(Node*,int,int);
void printList(Node*);
Node*insertLast(Node*,Node**,Node*);
Node*createNode(int);
int search(Node*,int);
Node*Delete(Node*,Node**,int);
Node*deleteLast(Node*,Node**);
int main()
{
int temp,ch,num;
Node *newNode,*start,*last;
start=last=NULL;
while(1)
{
printf("\n_________________menu__________________\n");
printf("1. insert at the end of list...\n");
printf("2. print the list...\n");
printf("3. search a speific item...\n");
printf("4. insert after a specific node...\n");
printf("5. insert a node before a specific node...\n");
printf("6. delete a specific node...\n");
printf("7. delete a last node...\n");
printf("8. reverse the link list...\n");
printf("9. sort the link list...\n");
printf("10. sort the link list(another method)...\n");
printf("11. exit...\n");
printf("\nenter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the data part of the node that you want to insert :\n");
scanf("%d",&temp);
newNode=createNode(temp);
start=insertLast(start,&last,newNode);
break;
case 2:
printList(start);
break;
case 3:
printf("enter the data that you want to search : ");
scanf("%d",&temp);
if(search(start,temp))
printf("the number you entered was in the list");
else
printf("the number you entered was not in the list");
break;
case 4:
printf("enter the data of the node after which you want to insert new node : ");
scanf("%d",&temp);
printf("enter the data part of the node that you want to insert : ");
scanf("%d",&num);
insertAfter(start,&last,temp,num);
break;
case 5:
printf("enter the data of the node before which you want to insert new node : ");
scanf("%d",&temp);
printf("enter the data part of the node that you want to insert : ");
scanf("%d",&num);
start=insertBefore(start,temp,num);
break;
case 6:
printf("enter the data part of the node that you want to delete : ");
scanf("%d",&temp);
start=Delete(start,&last,temp);
break;
case 7:
if(last==NULL)
{
printf("the list is empty...you can't delete any node...");
break;
}
start=deleteLast(start,&last);
break;
case 8:
start=reverseList(start,&last);
break;
case 9:
start=sort(start,&last);
break;
case 10:
start=sort2(start,&last);
break;
case 11:
exit(1);
default:
printf("you have entered a wrong choice...enter a valid choice");
}
}
}
Node* createNode(int data)
{
Node*newNode;
newNode=(Node*)malloc(sizeof(Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
Node*insertLast(Node*start,Node**p2last,Node*newNode)
{
if(*p2last==NULL)
{
*p2last=newNode;
return newNode;
}
(*p2last)->next=newNode;
*p2last=newNode;
return start;
}
void printList(Node*start)
{
printf("your list is as follows : \n");
while(start)
{
printf("%d\t",start->data);
start=start->next;
}
}
int search(Node*start,int data)
{
while(start)
{
if(start->data==data)
return 1;
start=start->next;
}
return 0;
}
void insertAfter(Node*start,Node**p2last,int data,int num)
{
Node*newNode,*tmp;
newNode=createNode(num);
if(*p2last==NULL)
printf("the list is empty...");
tmp=start;
while(tmp)
{
if(tmp->data==data)
break;
tmp=tmp->next;
}
if(!tmp)
printf("the number you enter was not in the list\n");
else
{
newNode->next=tmp->next;
tmp->next=newNode;
if(tmp==*p2last)
*p2last=newNode;
}
}
Node*insertBefore(Node*start,int data,int num)
{
Node *newNode,*prev,*tmp;
prev=NULL;
newNode=createNode(num);
if(start==NULL)
{
printf("the list is empty...");
return start;
}
tmp=start;
while(tmp)
{
if(tmp->data==data)
break;
prev=tmp;
tmp=tmp->next;
}
if(!tmp)
printf("the number you enter was not in the list\n");
else if(prev==NULL)
{
newNode->next=start;
start=newNode;
}
else
{
newNode->next=prev->next;
prev->next=newNode;
}
return start;
}
Node*Delete(Node*start,Node**p2last,int data)
{
Node*prev,*tmp;
tmp=start;
prev=NULL;
while(tmp)
{
if(tmp->data==data)
break;
prev=tmp;
tmp=tmp->next;
}
if(!tmp)
{
printf("the item you entered was not in the list...\n");
return start;
}
if(tmp==start)
{
if((*p2last)==start)
*p2last=NULL;
return NULL;
}
prev->next=tmp->next;
if(tmp==(*p2last))
*p2last=prev;
free(tmp);
return start;
}
Node*deleteLast(Node*start,Node**p2last)
{
return Delete(start,p2last,(*p2last)->data);
}
Node*reverseList(Node*start,Node**p2last)
{
Node*ptr,*tmp,*prev;
(*p2last)=start;
prev=NULL;
for(ptr=start;ptr;)
{
tmp=ptr->next;
ptr->next=prev;
prev=ptr;
ptr=tmp;
}
return prev;
}
Node*sort(Node*start,Node**p2last)
{
Node*ptr,*newNode,*tmp,*start1,*last1;
start1=last1=NULL;
while(start!=NULL)
{
tmp=ptr=start;
while(ptr)
{
if(tmp->data > ptr->data)
tmp=ptr;
ptr=ptr->next;
}
newNode=createNode(tmp->data);
start1=insertLast(start1,&last1,newNode);
start=Delete(start,p2last,tmp->data);
}
*p2last=last1;
return start1;
}
Node*sort2(Node*start,Node**p2last)
{
int *arr,count=0,i,tmp,j;
Node*ptr,*start1,*last1,*newNode;
ptr=start;
start1=last1=NULL;
while(ptr)
{
count++;
ptr=ptr->next;
}
arr=(int*)malloc(sizeof(int)*count);
ptr=start;
for(i=0;i<count;i++)
{
arr[i]=ptr->data;
ptr=ptr->next;
}
/* sorting the array bubble */
for(i=1;i<count;i++)
for(j=0;j<count-i;j++)
if(arr[j]>arr[j+1])
{
tmp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tmp;
}
for(i=0;i<count;i++)
start1=insertLast(start1,&last1,createNode(arr[i]));
*p2last=last1;
return start1;
}