Drpogue's Profile
Reputation: 1
Apprentice
- Group:
- Restricted Editor
- Active Posts:
- 19 (0.04 per day)
- Joined:
- 20-February 12
- Profile Views:
- 96
- Last Active:
Feb 10 2013 09:13 AM- Currently:
- Offline
Previous Fields
- Country:
- Who Cares
- OS Preference:
- Who Cares
- Favorite Browser:
- Who Cares
- Favorite Processor:
- Who Cares
- Favorite Gaming Platform:
- Who Cares
- Your Car:
- Who Cares
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: Sorting a doubly linked list in C
Posted 9 Feb 2013
I figured out the solution. Here is the code for any future users:
#include<stdio.h> #include<stdlib.h> #include"list.h" int i; node *p; node *n; void insert(node *pointer, int data){ //go through list till ya find the last node while(pointer->next!=NULL){ pointer = pointer->next; } //allocate memory for new node and put data in it pointer->next = (node *)malloc(sizeof(node)); (pointer->next)->prev= pointer; pointer = pointer->next; pointer->data=data; pointer->next = NULL; } void print(node *pointer){ if(pointer==NULL){ return;} printf("%d ",pointer->data); print(pointer->next); } int init(node *pointer,int find){ pointer =pointer->next; while(pointer!=NULL){ if(pointer->data== find)//found find { printf("The data is in the list."); return 1; } pointer = pointer->next;// Search in the next node. } //find is not found printf("The data is not in the list."); return 0; } void swap (node *x, node *y){ int temp= x->data; x->data=y->data; y->data=temp; } void sort(node*pointer){ int i; while(pointer->next!=NULL){ if(pointer->data>pointer->next->data){ swap(pointer,pointer->next); } pointer=pointer->next; sort(pointer); } } int main(){ //new is used to point to the first node //temp is for the last node node *new, *temp; int z; new=(node *)malloc(sizeof(node)); temp= new; temp ->next = NULL; temp ->prev = NULL; for(z=0;z<10;z++){ insert(new,(3*10)-z); } init(new,12); init(new,3); init(new,27); init(new,7); print(new); sort(new); print(new); } -
In Topic: Sorting a doubly linked list in C
Posted 9 Feb 2013
macosxnerd101, on 10 February 2013 - 03:05 AM, said:I wouldn't describe this as a "Java" design. A bubblesort is a bubblesort, and it's fairly language agnostic. A note about spacing conventions though- you should work on them to make your code more legible.
Your algorithm really isn't a proper bubblesort. As baavgai advocates, [url=http://www.dreamincode.net/forums/topic/231304-the-real-bubble-sort/]it's not a bubblesort without a boolean[/url]. You also are missing the nested loops aspect.
Edit: Editing out your posts is a big no-no, by the way.
All I was saying is that I tried to follow plans in a different language and apply them in one I don't know.Thank you for debating the finer points of data structures with me. -
In Topic: Sorting a doubly linked list in C
Posted 9 Feb 2013
EDIT -
In Topic: [C] Reading in from a file in command line and outputting line numbers
Posted 6 Feb 2013
Thank you for being exceedingly patient with me, and not just handing me the code.
The following code is the solved code for future users.
#include<stdio.h> #include<string.h> int main(int argc, char *argv[]) { size_t len; char line[120]; int i=1; if(argc!=2 ) // argc should be three { printf("Usage: %s filename", argv[0]); } else{ FILE *file =fopen(argv[1],"r"); if (file==0){ printf("Could not open file\n"); } while(fgets(line,sizeof(line),file)){ if (strstr(line,"brown")) { printf("%d \n",i); i++; } else { i++; } } } return 0; } -
In Topic: [C] Reading in from a file in command line and outputting line numbers
Posted 6 Feb 2013
SO I've changed it up a bit.
I declared an int i at the beginning for a for loop and wrote this
while(fgets(line,sizeof(line),file)){ for(i=0;i<sizeof(line);i++){ if(strstr(line,"brown")) { printf("%d",i); }
But when I run that it gives me a massive wall of numbers. Where did I go so wrong?
My Information
- Member Title:
- New D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
-
Contact Information
- E-mail:
- Private
Friends
Drpogue hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
Drpogue has no profile comments yet. Why not say hello?