I was trying to write a function ReverseRange(struct Node **head, int x, int y) which will reverse a linked list in given range of indices int x and int y. I used a previously defined function Reverse() in one of condition in ReverseRange() but it is not reversing the given list, only printing only one Node with data 'Q'. I don't know if the error is in Print() or Reverse() or ReverseRange() or elsewhere. Please help, Thank you.
#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
struct Node *next;
};
//insert data in the node
void Insert(struct Node **Head, char data) {
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = data;
temp->next = *Head;
*Head = temp;
}
//find length of linked list
int LengthRec(struct Node *head) {
if (head == NULL)
return 0;
return 1 + LengthRec(head->next);
}
//Reverse a linked list when head is given;
void Reverse(struct Node **head) {
struct Node *prev = NULL;
struct Node *curr = *head;
struct Node *next = NULL;
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
//Reverse list in range x to y;
int ReverseRange(struct Node **H, int x, int y) {
struct Node *Head = *H;
if (Head == NULL)
return -1;
else if (Head->next == NULL)
return -1;
else if (x == y)
return -1;
else if (x > y)
return -1;
else if (LengthRec(Head) >= y) {
if (x == 1 && y == LengthRec(Head)) {
Reverse(&Head);
return 1;
}
/* NOTE::
Code is incomplete, because I found error before
the entire code is written,
*/
}
}
void Print(struct Node **H) {
struct Node *head = *H;
if (head == NULL) {
printf("Head=NULL");
return;
}
printf("\n %c", head->data);
while (head->next != NULL) {
head = head->next;
printf("\t%c", head->data);
}
}
int main() {
struct Node *Head = NULL;
Insert(&Head, 'Q');
Insert(&Head, 'W');
Insert(&Head, 'E');
Insert(&Head, 'R');
Insert(&Head, 'T');
Print(&Head);
Reverse(&Head);
Print(&Head);
ReverseRange(&Head, 1, 5);
Print(&Head);
}
Output:
T R E W Q
Q W E R T
Q
The Reverse function seems fine, but it should be called with H as an argument from ReverseRange(), not &Head which is a local variable.
Some of the explicit tests at the beginning of the function correspond to legitimate arguments, and should not return an error value.
Note also that you should document the precise semantics for x and y: it is not idiomatic in C to use 1 to designate the first element of a collection, 0 is mote common. y seems included, which is not idiomatic either but consistent with using 1 for the first element.
Your LengthRec function is very inefficient and may cause a stack overflow for very long lists. Use a loop instead of recursing as this recursion is not a tail recursion.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
struct Node *next;
};
//insert data in the node
void Insert(struct Node **Head, char data) {
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = data;
temp->next = *Head;
*Head = temp;
}
//find length of linked list
int ListLength(const struct Node *head) {
int length = 0;
while (head != NULL) {
length++;
head = head->next;
}
return length;
}
//Reverse a linked list when head is given;
void Reverse(struct Node **head) {
struct Node *prev = NULL;
struct Node *curr = *head;
while (curr != NULL) {
struct Node *next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
*head = prev;
}
//Reverse list in range x to y;
int ReverseRange(struct Node **H, int x, int y) {
int length = ListLength(*H);
if (x < 1 || x > length || x > y || y > length)
return -1;
if (x == y)
return 1;
if (x == 1 && y == length) {
Reverse(H);
return 1;
} else {
struct Node **head = H;
struct Node *prev = NULL;
struct Node *curr = *head;
struct Node *last;
while (x > 1) {
head = &curr->next;
curr = *head;
x--;
y--;
}
last = curr;
while (x <= y) {
struct Node *next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
x++;
}
last->next = curr;
*head = prev;
return 1;
}
}
void Print(const char *msg, const struct Node *head) {
if (msg) {
printf("%s", msg);
}
if (head == NULL) {
printf("Head=NULL\n");
return;
}
printf("%c", head->data);
while (head->next != NULL) {
head = head->next;
printf("\t%c", head->data);
}
printf("\n");
}
int main() {
struct Node *Head = NULL;
Insert(&Head, 'E');
Insert(&Head, 'D');
Insert(&Head, 'C');
Insert(&Head, 'B');
Insert(&Head, 'A');
Print(" Initial list:\t", Head);
Reverse(&Head);
Print(" Reverse(&Head):\t", Head);
ReverseRange(&Head, 1, 5);
Print("ReverseRange(&Head,1,5):\t", Head);
ReverseRange(&Head, 1, 1);
Print("ReverseRange(&Head,1,1):\t", Head);
ReverseRange(&Head, 2, 4);
Print("ReverseRange(&Head,2,4):\t", Head);
return 0;
}
Output:
Initial list: A B C D E
Reverse(&Head): E D C B A ReverseRange(&Head,1,5): A B C D E
ReverseRange(&Head,1,1): A B C D E
ReverseRange(&Head,2,4): A D C B E
Related
Every k nodes form a segment. If the last few nodes are less than K, then you can ignore them.
Write a reverseKnodes() which reserves every segment in the linked list.
The function prototype is given as follow: void reversekNodes(ListNode** head, int k);
Input format:
The 1st line is the k
The 2nd line is the data to create the linked list and ends with a non-digit symbol Example:
Input: 3 1 2 3 4 5 6 7 8 9 10 a
Output: 3 2 1 6 5 4 9 8 7 10
#include <stdio.h>
#include <stdlib.h>
struct _listNode
{
int item;
struct _listNode *next;
};
typedef struct _listNode ListNode;
void printList (ListNode * head);
void deleteList (ListNode ** ptrHead);
void reverseKNodes (ListNode ** head, int K);
int
main ()
{
ListNode *head = NULL, *temp;
int i = 0;
int K = 0;
scanf ("%d", &K);
while (scanf ("%d", &i))
{
if (head == NULL)
{
head = (ListNode *) malloc (sizeof (ListNode));
temp = head;
}
else
{
temp->next = (ListNode *) malloc (sizeof (ListNode));
temp = temp->next;
}
temp->item = i;
}
temp->next = NULL;
reverseKNodes (&head, K);
printList (head);
deleteList (&head);
return 0;
}
void
printList (ListNode * head)
{
while (head != NULL)
{
printf ("%d ", head->item);
head = head->next;
}
printf ("\n");
}
void
deleteList (ListNode ** ptrHead)
{
ListNode *cur = *ptrHead;
ListNode *temp;
while (cur != NULL)
{
temp = cur->next;
free (cur);
cur = temp;
}
*ptrHead = NULL;
}
void
reverseKNodes (ListNode ** head, int K)
{
struct _listNode *reverse (struct _listNode *head, int k){
if (!head)
return NULL;
struct _listNode* cur = head;
struct _listNode* next = NULL;
struct _listNode* prev = NULL;
int count = 0;
while (cur != NULL && count < K)
{
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
count++;
}
if (next != NULL)
head->next = reverse(next, K);
return prev;
}
}
I am not allowed to change anything else other than the void function for reverseKNodes and I know I have done it wrong but I dont know where I went wrong. Could someone help me please?
A few issues with nested functions ...
Nested functions aren't standard C.
Are of limited benefit.
They add extra overhead to invoke.
So [please] don't use them.
And, your usage of one is incorrect. In reverseKNodes, you define reverse but never call it. So, reverseKNodes is a no-op.
So, to fix, move reverse out of the body of reverseKNodes and place it above. Then, actually invoke it in reverseKNodes:
void
reverseKNodes(ListNode **head, int K)
{
*head = reverse(*head, K);
}
Here is the full refactored code. For aid in debug (e.g. with gdb), I added code to allow the program to take an input file as an argument.
#include <stdio.h>
#include <stdlib.h>
struct _listNode {
int item;
struct _listNode *next;
};
typedef struct _listNode ListNode;
void printList(ListNode *head);
void deleteList(ListNode **ptrHead);
void reverseKNodes(ListNode **head, int K);
int
main(int argc,char **argv)
{
ListNode *head = NULL, *temp;
int i = 0;
int K = 0;
--argc;
++argv;
FILE *xf;
do {
if (argc != 1) {
xf = stdin;
break;
}
xf = fopen(*argv,"r");
if (xf != NULL)
break;
perror(*argv);
exit(1);
} while (0);
fscanf(xf,"%d", &K);
while (fscanf(xf,"%d", &i)) {
if (head == NULL) {
head = (ListNode *) malloc(sizeof(ListNode));
temp = head;
}
else {
temp->next = (ListNode *) malloc(sizeof(ListNode));
temp = temp->next;
}
temp->item = i;
}
temp->next = NULL;
reverseKNodes(&head, K);
printList(head);
deleteList(&head);
return 0;
}
void
printList(ListNode *head)
{
while (head != NULL) {
printf("%d ", head->item);
head = head->next;
}
printf("\n");
}
void
deleteList(ListNode **ptrHead)
{
ListNode *cur = *ptrHead;
ListNode *temp;
while (cur != NULL) {
temp = cur->next;
free(cur);
cur = temp;
}
*ptrHead = NULL;
}
struct _listNode *
reverse(struct _listNode *head, int K)
{
if (! head)
return NULL;
struct _listNode *cur = head;
struct _listNode *next = NULL;
struct _listNode *prev = NULL;
int count = 0;
while (cur != NULL && count < K) {
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
count++;
}
if (next != NULL)
head->next = reverse(next, K);
return prev;
}
void
reverseKNodes(ListNode **head, int K)
{
*head = reverse(*head, K);
}
Here is the program output:
3 2 1 6 5 4 9 8 7 10
I made some polynomial code with a doubly-linked list. for example, if
you write 1 and 2 then 1 is a degree and 2 is coefficient. 1x^2 insert
to doubly linked list. the problem is that when I check my code, the Node
head->degree is changing. if I write 1x^2 then head->degree is 1 next,
I write 2x^1 then head-> degree should maintain 1 but head-> degree
change to 2 I think there is some problem in the head pointer.
#include <stdio.h>
#include <stdlib.h>
// struct
struct Node {
int degree;
int coefficient;
struct Node* next;
struct Node* prev;
};
// global variables
int de; // degree
int co; // coefficient
int flag;
Node** head = (Node**)malloc(sizeof(Node)); //
Node** head1 = (Node**)malloc(sizeof(Node)); //
Node** head2 = (Node**)malloc(sizeof(Node)); //
Node** head3 = (Node**)malloc(sizeof(Node)); //
Node* newNode = (Node*)malloc(sizeof(Node)); //
// function
Node* inputpoly(void);
void printNode(Node* inp);
Node* multiply(Node* a, Node* b);
// main
int main() {
// head null
(*head1) = NULL;
(*head2) = NULL;
(*head3) = NULL;
while (1) {
printf("Input (degree) (coefficient) : ");
scanf_s("%d %d", &de, &co);
if (de * co < 0) { continue; }
if (de < 0 && co < 0) {
printf("Done!\n");
break;
}
*head = inputpoly();
}
printNode(*head);
//multiply(*head1, *head2);
free(head1);
free(head2);
free(head3);
free(newNode);
free(head);
}
Node* inputpoly(void) {
// create Node
newNode->degree = de;
newNode->coefficient = co;
newNode->next = NULL;
newNode->prev = NULL;
// case1
if (flag == 0) {
// list
if ((*head1) == NULL) {
*head1 = newNode;
}
// list x
else {
Node* horse = (*head1);
// front of head
// ------------------There is some problem
printf("%d\n", 1);
printf("--%d\n", newNode->degree);
printf("--%d\n", horse->degree);
if (horse->degree > newNode->degree) {
newNode->next = horse;
horse->prev = newNode;
*head1 = newNode;
}
// barward of head
else {
int num = 0;
while (horse->next != NULL) {
horse = horse->next;
if (horse->degree > newNode->degree) {
horse->prev->next = newNode;
newNode->next = horse;
newNode->prev = horse->prev;
horse->prev = newNode;
num = 1;
break;
}
}
// behind tail
if (num == 0) {
horse->next = newNode;
newNode->prev = horse;
}
}
}
return *head1;
}
}
void printNode(Node* inp) {
Node* horse = inp;
if (horse == NULL)
{
return;
}
while (horse != NULL) {
if (horse->prev == NULL) {
if (horse->degree == 1) {
printf("%d", horse->coefficient);
}
else {
printf("%d x^%d", horse->coefficient, horse->degree);
}
}
else {
if (horse->degree == 1) {
printf(" + %d", horse->coefficient);
}
else {
printf(" + %d x^%d", horse->coefficient, horse->degree);
}
}
}
printf("\n");
}
"i think there is some head pointer problem, and if I fixed it I can this problem. so I want to maintain this code as possible. I want some
advice or solution to my head pointer"
The code posted in your example does not compile:
Before you can fix a head pointer problem the code must compile. This list of errors is detailing 2 things:
first, functions cannot be called outside of a function, eg:
Node** head = (Node**)malloc(sizeof(Node)); //
Node** head1 = (Node**)malloc(sizeof(Node)); //
Node** head2 = (Node**)malloc(sizeof(Node)); //
Node** head3 = (Node**)malloc(sizeof(Node)); //
Node* newNode = (Node*)malloc(sizeof(Node)); //
should be called from within main(void){...} or some other function.
second, every occurrence of Node should be prepended with struct. eg:
struct Node** head = malloc(sizeof(struct Node *));
(have also removed the cast, and modified the size of what you are creating memory for, i.e. a pointer)
Rather then fix these and other problems, here is an example of a doubly linked list that can demonstrate the structure of a simple working program. You can adapt the following to match your needs:
struct Node {
int deg;
int coef;
struct Node* next; // Pointer to next node in DLL
struct Node* prev; // Pointer to previous node in DLL
};
void inputpoly(struct Node** head_ref, int deg, int coef)
{
//allocate node
struct Node *new_node = malloc(sizeof(*new_node));
//assign data
new_node->deg = deg;
new_node->coef = coef;
//set next as new head and prev to null
new_node->next = (*head_ref);
new_node->prev = NULL;
//change prev of head to new */
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
//point head to the new node */
(*head_ref) = new_node;
}
void printList(struct Node* node)
{
struct Node* last;
printf("\nread forward\n");
while (node != NULL) {
printf(" %d,%d ", node->deg,node->coef);
last = node;
node = node->next;
}
printf("\nread reverse\n");
while (last != NULL) {
printf(" %d,%d ", last->deg,last->coef);
last = last->prev;
}
}
int main(void)
{
//start with empty list
struct Node* head = NULL;
//create and populate new nodes
inputpoly(&head, 7, 2);
inputpoly(&head, 1, 4);
inputpoly(&head, 4, 6);
//ouput list
printList(head);
getchar();
return 0;
}
Note that this code is offered as a basic demonstration of creating doubly linked list, and illustrate how to traverse both directions. Because it does not free allocated memory, it is not recommended that it be used for any production purpose without addressing that omission.
I'm basically trying to implement LinkedList in C but I still can't get it I will leave my code below so you can see what I'm missing:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int val;
struct Node *Next;
};
struct Node *head;
void insert(int x);
void print();
int main() {
head = NULL;
int d, i = 0, f;
printf("How many number you want to insert:\n");
scanf("%d", &d);
for (i; i < d; i++) {
printf("Enter your number:\n");
scanf("%d", &f);
insert(f);
print();
}
return 0;
}
void insert(int x) {
struct Node *A = (struct Node *)malloc(sizeof(struct Node));
if (head == NULL) {
A->val = x;
A->Next = head;
head = A;
return;
}
A->val = x;
A->Next = head->Next;
}
void print() {
printf("[");
struct Node *B = head;
while (B->Next != NULL) {
B = B->Next;
printf("%d", B->val);
}
printf("]\n");
}
I need some pointers to where I missed up.
Here are the two ways to insert, either at the front/head of the list or the back/tail of the list:
void
insert_front(int x)
{
struct Node *A = malloc(sizeof(*A));
A->val = x;
A->Next = head;
head = A;
}
void
insert_back(int x)
{
struct Node *A = malloc(sizeof(*A));
A->val = x;
A->Next = NULL;
struct Node *prev = NULL;
for (struct Node *cur = head; cur != NULL; cur = cur->Next)
prev = cur;
if (prev != NULL)
prev->Next = A;
else
head = A;
}
The print() function is incorrect: it misses the first node and would crash on an empty list:
void print() {
printf("[");
struct Node *B = head;
while (B != NULL) {
printf(" %d", B->val);
B = B->Next;
}
printf(" ]\n");
}
The insert() function also has problems: only the first node is correctly inserted. Here is a modified version:
// insert a node in front of the list.
void insert(int x) {
struct Node *A = (struct Node *)malloc(sizeof(struct Node));
if (A != NULL) {
A->val = x;
A->Next = head;
head = A;
}
}
I have the following linked list implementation:
struct _node {
char *string;
struct _node *next;
}
struct _list {
struct _node *head;
struct _node *tail;
}
I want to make the following function:
void deleteList(struct _list *list, int from, int to) {
int i;
assert(list != NULL);
// I skipped error checking for out of range parameters for brevity of code
for (i = from; i <= to; i++) {
deleteNode(list->head, i);
}
}
// I ran this function with this linked list: [First]->[Second]->NULL
like this deleteNodes(list, 1, 1) to delete the second line and got
[First]->[Second]->NULL but when I run it like this deleteList(list, 0, 1) with this input [First]->[Second]->[Third]->NULL I get a seg fault.
Here is my deleteNode function
void deleteNode(struct _node *head, int index) {
if (head == NULL) {
return;
}
int i;
struct _node *temp = head;
if (index == 0) {
if (head->next == NULL) {
return;
}
else {
head = head->next;
free(head);
return;
}
}
for (i = 0; temp!=NULL && i<index-1; i++) {
temp = temp->next;
}
if (temp == NULL || temp->next == NULL) {
return;
}
Link next = temp->next->next;
free(temp->next);
temp->next = next;
}
I wrote a separate function to delete the head of the linked list if from or to = 0:
void pop(struct _node *head) {
if (head == NULL) {
return;
}
struct _node *temp = head;
head = head->next;
free(temp);
}
but it gives me seg fault or memory error Abort trapL 6.
It's all good to use just one struct, a node for your purpose.
struct node {
char *string;
struct node *next;
};
Then your loop for removing elements between two indices will not delete the right elements if you don't adjust the index according to the changing length of the list. And you must also return the new head of the list.
struct node *deleteList(struct node *head, unsigned from, unsigned to) {
unsigned i;
unsigned count = 0;
for (i = from; i <= to; i++) {
head = delete_at_index(head, i - count);
count++;
}
return head;
}
The help function delete_at_index looks as follows.
struct node *delete_at_index(struct node *head, unsigned i) {
struct node *next;
if (head == NULL)
return head;
next = head->next;
return i == 0
? (free(head), next) /* If i == 0, the first element needs to die. Do it. */
: (head->next = delete_at_index(next, i -
1), head); /* If it isn't the first element, we recursively check the rest. */
}
Complete program below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node {
char *string;
struct node *next;
};
void freeList(struct node *head) {
struct node *tmp;
while (head != NULL) {
tmp = head;
head = head->next;
free(tmp->string);
free(tmp);
}
}
struct node *delete_at_index(struct node *head, unsigned i) {
struct node *next;
if (head == NULL)
return head;
next = head->next;
return i == 0
? (free(head), next) /* If i == 0, the first element needs to die. Do it. */
: (head->next = delete_at_index(next, i -
1), head); /* If it isn't the first element, we recursively check the rest. */
}
struct node *deleteList(struct node *head, unsigned from, unsigned to) {
unsigned i;
unsigned count = 0;
for (i = from; i <= to; i++) {
head = delete_at_index(head, i - count);
count++;
}
return head;
}
void pushvar1(struct node **head_ref, char *new_data) {
struct node *new_node = malloc(sizeof(struct node));
new_node->string = strdup(new_data);
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printListvar1(struct node *node) {
while (node != NULL) {
printf(" %s ", node->string);
node = node->next;
}
printf("\n");
}
int main(int argc, char **argv) {
struct node *head = NULL;
for (int i = 0; i < 5; i++) {
char str[2];
sprintf(str, "node%d", i);
pushvar1(&head, str);
}
puts("Created Linked List: ");
printListvar1(head);
head = deleteList(head, 0, 2);
puts("Linked list after deleted nodes from index 0 to index 2: ");
printListvar1(head);
freeList(head);
return 0;
}
Test
Created Linked List:
node4 node3 node2 node1 node0
Linked list after deleted nodes from index 0 to index 2:
node1 node0
every programming problem can be solved by adding an extra level of indirection: use a pointer to pointer ...
unsigned deletefromto(struct node **head, unsigned from, unsigned to)
{
unsigned pos,ret;
struct node *this;
for (pos=ret=0; this = *head;pos++) {
if (pos < from) { head = &(*head)->next; continue; }
if (pos > to) break;
*head = this->next;
free(this);
ret++;
}
return ret; /* nuber of deleted nodes */
}
I am trying to sort a Linked list, but not able to do it. Below is my code. Can anyone help me. I have seen some programs too, which sort linked list and their approach is also like this only.
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int push(struct node **h, int x)
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->data = x;
temp->next = *h;
*h = temp;
return 0;
}
void print(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
void sort(struct node **h)
{
int i,j,a;
struct node *temp1;
struct node *temp2;
for(temp1=*h;temp1!=NULL;temp1=temp1->next)
{
for(temp2=temp1->next;temp2!=NULL;temp2=temp2->next)
{
a = temp1->data;
temp1->data = temp2->data;
temp2->data = a;
}
}
}
int main()
{
struct node * head = NULL;
push(&head,5);
push(&head,4);
push(&head,6);
push(&head,2);
push(&head,9);
printf("List is : ");
print(head);
sort(&head);
printf("after sorting list is : ");
print(head);
return 0;
}
Below is the output which i am getting :
List is : 9 2 6 4 5
after sorting list is : 5 4 6 2 9
You're switching the elements no matter what. Compare them first and then swap them if temp2 is less than temp1:
void sort(struct node **h)
{
int i,j,a;
struct node *temp1;
struct node *temp2;
for(temp1=*h;temp1!=NULL;temp1=temp1->next)
{
for(temp2=temp1->next;temp2!=NULL;temp2=temp2->next)
{
if(temp2->data < temp1->data)
{
a = temp1->data;
temp1->data = temp2->data;
temp2->data = a;
}
}
}
}
In your bubble sort, you forget the swap condition.
In my opinion, I suggest insertion sort
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
int push(struct node **h, int x)
{
struct node *temp = (struct node*)malloc(sizeof(struct node));
temp->data = x;
if (*h == NULL) {
temp->next = *h;
*h = temp;
} else {
struct node *tmp = *h;
struct node *prev = NULL;
while (1) {
if (tmp == NULL || tmp->data >= temp->data)
break;
prev = tmp;
tmp = tmp->next;
}
temp->next = tmp;
if (prev != NULL)
prev->next = temp;
else
*h = temp;
}
return 0;
}
void print(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
struct node * head = NULL;
push(&head,5);
push(&head,4);
push(&head,6);
push(&head,2);
push(&head,9);
printf("List is : ");
print(head);
//sort(&head);
printf("after sorting list is : ");
print(head);
return 0;
}