تو کدنویسی هاش مشکل دارم!!
یعنی باید لیست رو مرتب کنم.
ولی فقط با عوض کردن لینک.
اون کدهایی که مربوط به عوض کردن 2 تا لینک گره هست رو مشکل دارم.
Printable View
تو کدنویسی هاش مشکل دارم!!
یعنی باید لیست رو مرتب کنم.
ولی فقط با عوض کردن لینک.
اون کدهایی که مربوط به عوض کردن 2 تا لینک گره هست رو مشکل دارم.
فرض کن در پست قبلیم توی اون شکلی که کشیدم سطر اول به ترتیب A,B,C و سطر دوم D,E,F باشه میخوایم جای B و E رو عوض کنیم من کدش و اینجوری نوشتم
کد:node *temp;
temp=A->next->next;
A->next->next=D->next->next;
D->next->next=temp
temp=A->next;
A->next=D->next;
D->next=temp;
ممنونم .
این کدهایی که شما نوشتید رو متوجه شدم. ولی نتونستم تو برنامه ای که نوشتم(شما زحمتشو کشیدید) استفاده کنم.
این برنامه رو ببینید:(برای مرتب کردن لیست پیوندی بدون تغییر info)
کد:#include<iostream.h>
class node
{
friend class linkedList;
private:
int info;
node *next;
};
class linkedList
{
public:
linkedList();
void enterLast(int x);
void display();
void sort();
private:
node *root;
};
linkedList::linkedList()
{
root=NULL;
}
void linkedList::display()
{
node* temp=root;
while(temp!=NULL)
{
cout<<temp->info<<" ";
temp=temp->next;
}
cout<<endl;
}
void linkedList::sort()
{
node* temp1=root;
node* temp2=root;
while(temp1!=NULL)
temp2=temp1->next;
while(temp2!=NULL)
{ if (temp1->info<temp2->info)
{
}
temp2=temp2->next;
}
temp1=temp1->next;
}
}
void linkedList::enterLast(int x)
{
node* temp=new node,*temp2=root;
temp->next=NULL;
temp->info=x;
if(root==NULL)
root=temp;
else
{
while(temp2->next!=NULL)
{
temp2=temp2->next;
}
temp2->next=temp;
}
}
int main()
{
linkedList a;
int n;
while(1)
{
cout<<"Enter in last:";
cin>>n;
if (n==0)
break;
else
a.enterLast(n);
}
a.sort();
a.display();
return 0;
}
به تابع sort نگاه کنید:
نمیدونم بعدش چیکار کنم؟؟؟کد:void linkedList::sort()
{
node* temp1=root;
node* temp2=root;
while(temp1!=NULL)
temp2=temp1->next;
while(temp2!=NULL)
{ if (temp1->info<temp2->info)
{
مراجل تعویض لینک ها
}
temp2=temp2->next;
}
temp1=temp1->next;
}
}
ببینید چون لیست خطی هستش شمل نباید temp رو برابر خونه ای بکنید که می خواید مرتب بشه بلکه باید در یک خونه قبل از اون قرار بگیره مثلا یه چیزی مثل کد پایین ولی با اضافه کردن کد پایین هم برنامه درست کار نمی کنه
اگه هنوز هم با این چیزی که گفتم نمی تونید برنامه رو درست کنید با همین روشی که مرتب سازی رو دارید باهاش انجام میدید یه نمونش رو که روی ارایه پیاده سازی شده و بدون باگ هم باشه (دقیقا به همینصورت باشه همراه while , ...... ) رو بهم بدید تا توی دیباگش کمکتون کنم
کد:void linkedList::sort()
{
node *chn;
node* temp1=root;
node* temp2=root;
while(temp1!=NULL)
{
temp2=temp1->next;
while(temp2!=NULL)
{
if (temp1->next->info < temp2->next->info)
{
chn=temp1->next->next;
temp1->next->next=temp2->next->next;
temp2->next->next=chn;
chn=temp1->next;
temp1->next=temp2->next;
temp2->next=chn;
}
temp2=temp2->next;
}
temp1=temp1->next;
}
}
ا سلام
دوستان من بک گراند مهندسی کامپیوتر و برنامه نویسی با C رو ندارم. به دلایلی مجبور شدم درس Operating Systems رو بگیرم. متاسفانه 40 درصد نمره اختصاص به پروژه هایی داره که باید با C بنویسم. اولین پروژه مربوط به لیست های پیوندی میشه ظاهراً که اینجا کپی می کنم. انتظار ندارم که پروژه را برام انجام بدید اما به دلیل اینکه هیچی نمی دونم خواهش می کنم به صورت قدم به قدم بگید که باید چی کار کنم. الان دارم در مورد لیست های پیوندی مطالعه می کنم. لطفاً هر کی می تونه کمک کنه.
Project 1 description
For this homework, you will be constructing a linked list
implementation in C using the templates provides above.
You may need to modify the data structure (or add new data
structures) defined in list.h. However, you MAY NOT modify the
function defined in list.h. You may need to complete the function
bodies in list.c. You can add new functions into list.h.
Create a test program to generate, populate and manipulate S
(S<M) linked list.
Test data in the format of an M x N matrix will be provided.
Toy example (10 x 11):
0 0 0 6 8 4 8 1 5 7 4
8 7 7 5 7 3 0 0 0 0 3
3 5 7 3 4 7 0 0 0 0 1
0 0 8 2 7 6 2 4 4 5 0
0 0 0 1 7 7 3 4 5 6 1
0 0 7 4 4 8 5 7 3 5 2
0 8 1 5 5 1 8 8 4 7 5
5 3 4 7 2 0 0 0 0 0 8
4 3 7 8 2 2 6 4 0 0 3
0 0 0 8 6 7 4 6 8 5 0
Project 1 Task
Task Breaking down
1. Read the data into memory using file operations.
2. For the first S rows, put every element from the same row of the matrix into
a new linked list, so you will have S linked lists.
3. For every link list, implement the f function to calculate the walking
distances and record this distances into a linked list d.
4. Repeat the same operations for the rest rows (M-S).
5. Plot the curve with matlab about the values of d.
6. A written report is required.
Due date: Sep. 28th, 2009.
Project 1 (Intlist.h)
#ifndef __intlist_h__
#define __intlist_h__
/* each entry in the list contains an int */
typedef struct intlist {
int datum;
struct intlist *next;
} INTLIST;
INTLIST *init_intlist( int n ); /* initializes the intlist with initial datum n */
int insert_intlist( INTLIST *lst, int n ); /* Inserts an int (n) into an intlist from the beginning*/
void list_append(INTLIST *list, void *datum); /* Inserts entry to the end of the list */
Void list_front(INTLIST *list); /*return the element at the front of the list, and remove it from the
list*/
void list_map( INTLIST *list, void (*f)(void *) ); /*Applies a function to each element of the list */
void list_delete( INTLIST *list ); /* Deletes (and frees) all entries in the list */
#endif
Project 1
Intlist.c
#include <stdlib.h>
#include “intlist.h”
/* initializes the intlist with initial datum n */
INTLIST *init_intlist( int n ) {
INTLIST *lst;
lst = (INTLIST *)malloc(sizeof(INTLIST));
lst->datum = n;
lst->next = NULL;
return lst;
}
Project 1
myprogram.c
#include <stdlib.h>
#include “intlist.h”
/* execution starts here */
int main() {
INTLIST *lst;
lst = init_intlist(1);
insert_intlist( lst, 2 );
free( lst );
return 0;
}
Project 1
Makefile: putting everything together
CC=gcc
CFLAGS=-Wall -g
LIBS=-lreadline -lm
OBJS=intlist.o
INCLUDES=common.h
all: $(OBJS)
$(CC) $(CFLAGS) $(OBJS) $(LIBS) -o myprogram
.c.o: $*.h $(INCLUDES)
$(CC) $(CFLAGS) -c $*.c
clean:
rm -f *.o myprogram